From 69ec744ec44837140f13e84f87c7062534fc3f23 Mon Sep 17 00:00:00 2001 From: Pushkar Raj Date: Thu, 23 Jan 2025 22:40:44 +0530 Subject: [PATCH 1/2] fix #25: Add `IFEQ` option to `SET` command Signed-off-by: Pushkar Raj --- hack/cmds/commands.json | 30 ++++++++++++-- internal/cmds/gen_string.go | 69 ++++++++++++++++++++++++++++++++ internal/cmds/gen_string_test.go | 11 +++++ 3 files changed, 106 insertions(+), 4 deletions(-) diff --git a/hack/cmds/commands.json b/hack/cmds/commands.json index e832e1b2..6dae5375 100644 --- a/hack/cmds/commands.json +++ b/hack/cmds/commands.json @@ -5050,10 +5050,32 @@ }, { "name": "condition", - "type": "enum", - "enum": [ - "NX", - "XX" + "type": "oneof", + "arguments": [ + { + "name": "nx", + "type": "pure-token", + "token": "NX" + }, + { + "name": "xx", + "type": "pure-token", + "token": "XX" + }, + { + "type": "block", + "arguments": [ + { + "name": "ifeq", + "type": "pure-token", + "token": "IFEQ" + }, + { + "name": "comparison-value", + "type": "string" + } + ] + } ], "optional": true }, diff --git a/internal/cmds/gen_string.go b/internal/cmds/gen_string.go index 6b566260..13f92406 100644 --- a/internal/cmds/gen_string.go +++ b/internal/cmds/gen_string.go @@ -729,6 +729,70 @@ func (c Set) Key(key string) SetKey { return (SetKey)(c) } +type SetConditionIfeqComparisonValue Incomplete + +func (c SetConditionIfeqComparisonValue) Get() SetGet { + c.cs.s = append(c.cs.s, "GET") + return (SetGet)(c) +} + +func (c SetConditionIfeqComparisonValue) ExSeconds(seconds int64) SetExpirationExSeconds { + c.cs.s = append(c.cs.s, "EX", strconv.FormatInt(seconds, 10)) + return (SetExpirationExSeconds)(c) +} + +func (c SetConditionIfeqComparisonValue) PxMilliseconds(milliseconds int64) SetExpirationPxMilliseconds { + c.cs.s = append(c.cs.s, "PX", strconv.FormatInt(milliseconds, 10)) + return (SetExpirationPxMilliseconds)(c) +} + +func (c SetConditionIfeqComparisonValue) ExatTimestamp(timestamp int64) SetExpirationExatTimestamp { + c.cs.s = append(c.cs.s, "EXAT", strconv.FormatInt(timestamp, 10)) + return (SetExpirationExatTimestamp)(c) +} + +func (c SetConditionIfeqComparisonValue) PxatMillisecondsTimestamp(millisecondsTimestamp int64) SetExpirationPxatMillisecondsTimestamp { + c.cs.s = append(c.cs.s, "PXAT", strconv.FormatInt(millisecondsTimestamp, 10)) + return (SetExpirationPxatMillisecondsTimestamp)(c) +} + +func (c SetConditionIfeqComparisonValue) Keepttl() SetExpirationKeepttl { + c.cs.s = append(c.cs.s, "KEEPTTL") + return (SetExpirationKeepttl)(c) +} + +func (c SetConditionIfeqComparisonValue) Ex(duration time.Duration) SetExpirationExSecTyped { + c.cs.s = append(c.cs.s, "EX", strconv.FormatInt(int64(duration/time.Second), 10)) + return (SetExpirationExSecTyped)(c) +} + +func (c SetConditionIfeqComparisonValue) Px(duration time.Duration) SetExpirationPxMsTyped { + c.cs.s = append(c.cs.s, "PX", strconv.FormatInt(int64(duration/time.Millisecond), 10)) + return (SetExpirationPxMsTyped)(c) +} + +func (c SetConditionIfeqComparisonValue) Exat(timestamp time.Time) SetExpirationExatTimestampTyped { + c.cs.s = append(c.cs.s, "EXAT", strconv.FormatInt(timestamp.Unix(), 10)) + return (SetExpirationExatTimestampTyped)(c) +} + +func (c SetConditionIfeqComparisonValue) Pxat(timestamp time.Time) SetExpirationPxatMsTimestampTyped { + c.cs.s = append(c.cs.s, "PXAT", strconv.FormatInt(timestamp.UnixMilli(), 10)) + return (SetExpirationPxatMsTimestampTyped)(c) +} + +func (c SetConditionIfeqComparisonValue) Build() Completed { + c.cs.Build() + return Completed{cs: c.cs, cf: uint16(c.cf), ks: c.ks} +} + +type SetConditionIfeqIfeq Incomplete + +func (c SetConditionIfeqIfeq) ComparisonValue(comparisonValue string) SetConditionIfeqComparisonValue { + c.cs.s = append(c.cs.s, comparisonValue) + return (SetConditionIfeqComparisonValue)(c) +} + type SetConditionNx Incomplete func (c SetConditionNx) Get() SetGet { @@ -977,6 +1041,11 @@ func (c SetValue) Xx() SetConditionXx { return (SetConditionXx)(c) } +func (c SetValue) Ifeq() SetConditionIfeqIfeq { + c.cs.s = append(c.cs.s, "IFEQ") + return (SetConditionIfeqIfeq)(c) +} + func (c SetValue) Get() SetGet { c.cs.s = append(c.cs.s, "GET") return (SetGet)(c) diff --git a/internal/cmds/gen_string_test.go b/internal/cmds/gen_string_test.go index 1769ded3..3094e919 100644 --- a/internal/cmds/gen_string_test.go +++ b/internal/cmds/gen_string_test.go @@ -77,6 +77,17 @@ func string0(s Builder) { s.Set().Key("1").Value("1").Xx().Exat(time.Now()).Build() s.Set().Key("1").Value("1").Xx().Pxat(time.Now()).Build() s.Set().Key("1").Value("1").Xx().Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Get().Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").ExSeconds(1).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").PxMilliseconds(1).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").ExatTimestamp(1).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").PxatMillisecondsTimestamp(1).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Keepttl().Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Ex(time.Second).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Px(time.Second).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Exat(time.Now()).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Pxat(time.Now()).Build() + s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Build() s.Set().Key("1").Value("1").Get().Build() s.Set().Key("1").Value("1").ExSeconds(1).Build() s.Set().Key("1").Value("1").PxMilliseconds(1).Build() From 59ff7c3f5b99e9fcb57c59999f0ddf6e1bbfc6f8 Mon Sep 17 00:00:00 2001 From: Pushkar Raj Date: Fri, 24 Jan 2025 00:13:58 +0530 Subject: [PATCH 2/2] Removed `block` argument per PR comment Signed-off-by: Pushkar Raj --- hack/cmds/commands.json | 15 +++---------- internal/cmds/gen_string.go | 37 +++++++++++++------------------- internal/cmds/gen_string_test.go | 22 +++++++++---------- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/hack/cmds/commands.json b/hack/cmds/commands.json index 6dae5375..bfdd20de 100644 --- a/hack/cmds/commands.json +++ b/hack/cmds/commands.json @@ -5063,18 +5063,9 @@ "token": "XX" }, { - "type": "block", - "arguments": [ - { - "name": "ifeq", - "type": "pure-token", - "token": "IFEQ" - }, - { - "name": "comparison-value", - "type": "string" - } - ] + "name": "ifeq", + "type": "string", + "token": "IFEQ" } ], "optional": true diff --git a/internal/cmds/gen_string.go b/internal/cmds/gen_string.go index 13f92406..959789da 100644 --- a/internal/cmds/gen_string.go +++ b/internal/cmds/gen_string.go @@ -729,70 +729,63 @@ func (c Set) Key(key string) SetKey { return (SetKey)(c) } -type SetConditionIfeqComparisonValue Incomplete +type SetConditionIfeq Incomplete -func (c SetConditionIfeqComparisonValue) Get() SetGet { +func (c SetConditionIfeq) Get() SetGet { c.cs.s = append(c.cs.s, "GET") return (SetGet)(c) } -func (c SetConditionIfeqComparisonValue) ExSeconds(seconds int64) SetExpirationExSeconds { +func (c SetConditionIfeq) ExSeconds(seconds int64) SetExpirationExSeconds { c.cs.s = append(c.cs.s, "EX", strconv.FormatInt(seconds, 10)) return (SetExpirationExSeconds)(c) } -func (c SetConditionIfeqComparisonValue) PxMilliseconds(milliseconds int64) SetExpirationPxMilliseconds { +func (c SetConditionIfeq) PxMilliseconds(milliseconds int64) SetExpirationPxMilliseconds { c.cs.s = append(c.cs.s, "PX", strconv.FormatInt(milliseconds, 10)) return (SetExpirationPxMilliseconds)(c) } -func (c SetConditionIfeqComparisonValue) ExatTimestamp(timestamp int64) SetExpirationExatTimestamp { +func (c SetConditionIfeq) ExatTimestamp(timestamp int64) SetExpirationExatTimestamp { c.cs.s = append(c.cs.s, "EXAT", strconv.FormatInt(timestamp, 10)) return (SetExpirationExatTimestamp)(c) } -func (c SetConditionIfeqComparisonValue) PxatMillisecondsTimestamp(millisecondsTimestamp int64) SetExpirationPxatMillisecondsTimestamp { +func (c SetConditionIfeq) PxatMillisecondsTimestamp(millisecondsTimestamp int64) SetExpirationPxatMillisecondsTimestamp { c.cs.s = append(c.cs.s, "PXAT", strconv.FormatInt(millisecondsTimestamp, 10)) return (SetExpirationPxatMillisecondsTimestamp)(c) } -func (c SetConditionIfeqComparisonValue) Keepttl() SetExpirationKeepttl { +func (c SetConditionIfeq) Keepttl() SetExpirationKeepttl { c.cs.s = append(c.cs.s, "KEEPTTL") return (SetExpirationKeepttl)(c) } -func (c SetConditionIfeqComparisonValue) Ex(duration time.Duration) SetExpirationExSecTyped { +func (c SetConditionIfeq) Ex(duration time.Duration) SetExpirationExSecTyped { c.cs.s = append(c.cs.s, "EX", strconv.FormatInt(int64(duration/time.Second), 10)) return (SetExpirationExSecTyped)(c) } -func (c SetConditionIfeqComparisonValue) Px(duration time.Duration) SetExpirationPxMsTyped { +func (c SetConditionIfeq) Px(duration time.Duration) SetExpirationPxMsTyped { c.cs.s = append(c.cs.s, "PX", strconv.FormatInt(int64(duration/time.Millisecond), 10)) return (SetExpirationPxMsTyped)(c) } -func (c SetConditionIfeqComparisonValue) Exat(timestamp time.Time) SetExpirationExatTimestampTyped { +func (c SetConditionIfeq) Exat(timestamp time.Time) SetExpirationExatTimestampTyped { c.cs.s = append(c.cs.s, "EXAT", strconv.FormatInt(timestamp.Unix(), 10)) return (SetExpirationExatTimestampTyped)(c) } -func (c SetConditionIfeqComparisonValue) Pxat(timestamp time.Time) SetExpirationPxatMsTimestampTyped { +func (c SetConditionIfeq) Pxat(timestamp time.Time) SetExpirationPxatMsTimestampTyped { c.cs.s = append(c.cs.s, "PXAT", strconv.FormatInt(timestamp.UnixMilli(), 10)) return (SetExpirationPxatMsTimestampTyped)(c) } -func (c SetConditionIfeqComparisonValue) Build() Completed { +func (c SetConditionIfeq) Build() Completed { c.cs.Build() return Completed{cs: c.cs, cf: uint16(c.cf), ks: c.ks} } -type SetConditionIfeqIfeq Incomplete - -func (c SetConditionIfeqIfeq) ComparisonValue(comparisonValue string) SetConditionIfeqComparisonValue { - c.cs.s = append(c.cs.s, comparisonValue) - return (SetConditionIfeqComparisonValue)(c) -} - type SetConditionNx Incomplete func (c SetConditionNx) Get() SetGet { @@ -1041,9 +1034,9 @@ func (c SetValue) Xx() SetConditionXx { return (SetConditionXx)(c) } -func (c SetValue) Ifeq() SetConditionIfeqIfeq { - c.cs.s = append(c.cs.s, "IFEQ") - return (SetConditionIfeqIfeq)(c) +func (c SetValue) Ifeq(ifeq string) SetConditionIfeq { + c.cs.s = append(c.cs.s, ifeq) + return (SetConditionIfeq)(c) } func (c SetValue) Get() SetGet { diff --git a/internal/cmds/gen_string_test.go b/internal/cmds/gen_string_test.go index 3094e919..e8164430 100644 --- a/internal/cmds/gen_string_test.go +++ b/internal/cmds/gen_string_test.go @@ -77,17 +77,17 @@ func string0(s Builder) { s.Set().Key("1").Value("1").Xx().Exat(time.Now()).Build() s.Set().Key("1").Value("1").Xx().Pxat(time.Now()).Build() s.Set().Key("1").Value("1").Xx().Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Get().Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").ExSeconds(1).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").PxMilliseconds(1).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").ExatTimestamp(1).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").PxatMillisecondsTimestamp(1).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Keepttl().Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Ex(time.Second).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Px(time.Second).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Exat(time.Now()).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Pxat(time.Now()).Build() - s.Set().Key("1").Value("1").Ifeq().ComparisonValue("1").Build() + s.Set().Key("1").Value("1").Ifeq("1").Get().Build() + s.Set().Key("1").Value("1").Ifeq("1").ExSeconds(1).Build() + s.Set().Key("1").Value("1").Ifeq("1").PxMilliseconds(1).Build() + s.Set().Key("1").Value("1").Ifeq("1").ExatTimestamp(1).Build() + s.Set().Key("1").Value("1").Ifeq("1").PxatMillisecondsTimestamp(1).Build() + s.Set().Key("1").Value("1").Ifeq("1").Keepttl().Build() + s.Set().Key("1").Value("1").Ifeq("1").Ex(time.Second).Build() + s.Set().Key("1").Value("1").Ifeq("1").Px(time.Second).Build() + s.Set().Key("1").Value("1").Ifeq("1").Exat(time.Now()).Build() + s.Set().Key("1").Value("1").Ifeq("1").Pxat(time.Now()).Build() + s.Set().Key("1").Value("1").Ifeq("1").Build() s.Set().Key("1").Value("1").Get().Build() s.Set().Key("1").Value("1").ExSeconds(1).Build() s.Set().Key("1").Value("1").PxMilliseconds(1).Build()