Skip to content
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

Add RedisResult.AsBoolSlice() #499 #500

Merged
merged 5 commits into from
Mar 17, 2024
Merged

Add RedisResult.AsBoolSlice() #499 #500

merged 5 commits into from
Mar 17, 2024

Conversation

ali-assar
Copy link
Contributor

Hi. I added .AsBoolSlice() as requested

message.go Outdated
Comment on lines 759 to 761
if m.typ != '*' {
panic(fmt.Sprintf("redis message type %c is not an array", m.typ))
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if m.typ != '*' {
panic(fmt.Sprintf("redis message type %c is not an array", m.typ))
}

This check is already done by m.ToArray()

message.go Outdated
Comment on lines 768 to 775
if len(v.string) != 0 {
s[i], err = strconv.ParseBool(v.string)
if err != nil {
s[i] = false
}
} else {
s[i] = v.integer != 0
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(v.string) != 0 {
s[i], err = strconv.ParseBool(v.string)
if err != nil {
s[i] = false
}
} else {
s[i] = v.integer != 0
}
s[i], _ = v.AsBool()

We can simply re-use .AsBool() here and ignore its error.

message_test.go Outdated
Comment on lines 302 to 309
values := []RedisMessage{{string: "true", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{true}) {
t.Fatal("AsBoolSlice not get value as expected")
}
values = []RedisMessage{{string: "false", typ: '+'}}
if ret, _ := (RedisResult{val: RedisMessage{typ: '*', values: values}}).AsBoolSlice(); !reflect.DeepEqual(ret, []bool{false}) {
t.Fatal("AsBoolSlice not get value as expected")
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true/false strings are good use cases. Could you make AsBool() support them as well?

message_test.go Outdated
Comment on lines 1419 to 1426
defer func() {
if r := recover(); r == nil {
t.Fatal("AsBoolSlice did not panic as expected")
} else if !strings.Contains(r.(string), "redis message type t is not an array") {
t.Fatal("AsBoolSlice did not panic with the expected message")
}
}()
(&RedisMessage{typ: 't'}).AsBoolSlice()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defer func() {
if r := recover(); r == nil {
t.Fatal("AsBoolSlice did not panic as expected")
} else if !strings.Contains(r.(string), "redis message type t is not an array") {
t.Fatal("AsBoolSlice did not panic with the expected message")
}
}()
(&RedisMessage{typ: 't'}).AsBoolSlice()

duplicated.

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.58%. Comparing base (236216b) to head (ae55d90).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #500   +/-   ##
=======================================
  Coverage   95.58%   95.58%           
=======================================
  Files          80       80           
  Lines       33090    33107   +17     
=======================================
+ Hits        31628    31645   +17     
  Misses       1261     1261           
  Partials      201      201           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

message.go Outdated
}
s := make([]bool, len(values))
for i, v := range values {
s[i] = v.string == "1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your original use of strconv.ParseBool was good. Why did you remove it completely? I believe we better add it to AsBool() and re-use AsBool() here.

@ali-assar
Copy link
Contributor Author

This time I used AsBool function within AsBoolSlice and I think this approach is fine and better than strconv.ParseBool as the logic for parsing a boolean value is centralized and consistent across the codebase.

Please let me now if there is any other feedback

@rueian rueian self-requested a review March 17, 2024 16:51
Copy link
Collaborator

@rueian rueian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rueian rueian merged commit 4c95efa into redis:main Mar 17, 2024
1 check passed
@rueian rueian added the feature label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants