-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export some commands in internal/cmds #355
Comments
Hi @tamayika, I understand that it may be annoying for some people. However, I don't think making these intermediate builder public is a worthwhile solution. Because:
var fieldValue rueidis.XaddFieldValue
fieldValue.Build() IMHO, the following code style, without branching and merging while constructing a command, is actually preferable: func xadd(ctx context.Context, conn rueidis.Client, key string, limit int, attributes map[string]string) error {
if limit > 0 {
fieldValue := conn.B().Xadd().Key(key).Maxlen().Almost().Threshold(strconv.Itoa(limit)).Id("*").FieldValue()
for k, v := range attributes {
fieldValue = fieldValue.FieldValue(k, v)
}
return conn.Do(ctx, fieldValue.Build()).Error()
} else {
fieldValue := conn.B().Xadd().Key(key).Id("*").FieldValue()
for k, v := range attributes {
fieldValue = fieldValue.FieldValue(k, v)
}
return conn.Do(ctx, fieldValue.Build()).Error()
}
} If you are concerned about the duplicated func xadd(ctx context.Context, conn rueidis.Client, key string, limit int, attributes map[string]string) error {
if limit > 0 {
return conn.Do(ctx, conn.B().Xadd().Key(key).Maxlen().Almost().Threshold(strconv.Itoa(limit)).Id("*").FieldValueWith(attributes).Build()).Error()
} else {
return conn.Do(ctx, conn.B().Xadd().Key(key).Id("*").FieldValueWith(attributes).Build()).Error()
}
} |
How about adding sub package like
Sorry, I don't understand what you mean. This happens without exported type. conn.B().Xadd().Key(key).Id("*").FieldValue().Build()
It is effective only when to set // internal/cmds
package cmds
func (c XaddFieldValue) FieldValueIter(iter iter.Seq2[string, string]) XaddFieldValueIter {
for key, value := range iter {
c.cs.s = append(c.cs.s, key, value)
}
return (XaddFieldValueIter)(c)
}
type XaddFieldValueIter Completed
func (c XaddFieldValueIter) Build() Completed {
c.cs.Build()
return Completed(c)
}
func xadd(ctx context.Context, conn rueidis.Client, key string, limit int, attributes map[string]string) error {
xadd := conn.B().Xadd().Key(key)
iter := func(yield func(string, string) bool) bool {
for key, value := range attributes {
if !yield(key, value) {
return false
}
}
return true
}
var completed rueidis.Completed
if limit > 0 {
completed = xadd.Maxlen().Almost().Threshold(strconv.Itoa(limit)).Id("*").FieldValueIter(iter).Build()
} else {
completed = xadd.Id("*").FieldValueIter(iter).Build()
}
return conn.Do(ctx, completed).Error()
}
func xadd(ctx context.Context, conn rueidis.Client, key string, limit int, attributes map[string]int) error {
xadd := conn.B().Xadd().Key(key)
iter := func(yield func(string, string) bool) bool {
for key, value := range attributes {
if !yield(key, strconv.Itoa(value)) {
return false
}
}
return true
}
var completed rueidis.Completed
if limit > 0 {
completed = xadd.Maxlen().Almost().Threshold(strconv.Itoa(limit)).Id("*").FieldValueIter(iter).Build()
} else {
completed = xadd.Id("*").FieldValueIter(iter).Build()
}
return conn.Do(ctx, completed).Error()
} struct func xadd(ctx context.Context, conn rueidis.Client, key string, limit int, person Person) error {
xadd := conn.B().Xadd().Key(key)
iter := func(yield func(string, string) bool) bool {
if !yield("name", person.Name) {
return false
}
if !yield("age", strconv.Itoa(person.Age)) {
return false
}
return true
}
var completed rueidis.Completed
if limit > 0 {
completed = xadd.Maxlen().Almost().Threshold(strconv.Itoa(limit)).Id("*").FieldValueIter(iter).Build()
} else {
completed = xadd.Id("*").FieldValueIter(iter).Build()
}
return conn.Do(ctx, completed).Error()
} |
I know Builder respects Redis command argument order, but this is a little annoying in some situation.
For example, please consider maxlen is set only when limit > 0 for XADD.
Currently I have to write like blow.
If XaddFieldValue is exported, I can write like below.
The text was updated successfully, but these errors were encountered: