Skip to content

Commit

Permalink
Merge branch 'master' into add-helpers-to-set-libinfo-without-panic
Browse files Browse the repository at this point in the history
  • Loading branch information
ofekshenawa committed Feb 14, 2024
2 parents 149dae0 + 7b9e81f commit d2798b1
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
26 changes: 26 additions & 0 deletions example/scan-struct/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type Model struct {
Str1 string `redis:"str1"`
Str2 string `redis:"str2"`
Bytes []byte `redis:"bytes"`
Int int `redis:"int"`
Bool bool `redis:"bool"`
Ignored struct{} `redis:"-"`
Expand All @@ -22,13 +23,15 @@ func main() {
rdb := redis.NewClient(&redis.Options{
Addr: ":6379",
})
_ = rdb.FlushDB(ctx).Err()

// Set some fields.
if _, err := rdb.Pipelined(ctx, func(rdb redis.Pipeliner) error {
rdb.HSet(ctx, "key", "str1", "hello")
rdb.HSet(ctx, "key", "str2", "world")
rdb.HSet(ctx, "key", "int", 123)
rdb.HSet(ctx, "key", "bool", 1)
rdb.HSet(ctx, "key", "bytes", []byte("this is bytes !"))
return nil
}); err != nil {
panic(err)
Expand All @@ -47,5 +50,28 @@ func main() {
}

spew.Dump(model1)
// Output:
// (main.Model) {
// Str1: (string) (len=5) "hello",
// Str2: (string) (len=5) "world",
// Bytes: ([]uint8) (len=15 cap=16) {
// 00000000 74 68 69 73 20 69 73 20 62 79 74 65 73 20 21 |this is bytes !|
// },
// Int: (int) 123,
// Bool: (bool) true,
// Ignored: (struct {}) {
// }
// }

spew.Dump(model2)
// Output:
// (main.Model) {
// Str1: (string) (len=5) "hello",
// Str2: (string) "",
// Bytes: ([]uint8) <nil>,
// Int: (int) 123,
// Bool: (bool) false,
// Ignored: (struct {}) {
// }
// }
}
53 changes: 53 additions & 0 deletions internal/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package internal

import (
"strings"
"testing"

. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
)

func BenchmarkToLowerStd(b *testing.B) {
str := "AaBbCcDdEeFfGgHhIiJjKk"
for i := 0; i < b.N; i++ {
_ = strings.ToLower(str)
}
}

// util.ToLower is 3x faster than strings.ToLower.
func BenchmarkToLowerInternal(b *testing.B) {
str := "AaBbCcDdEeFfGgHhIiJjKk"
for i := 0; i < b.N; i++ {
_ = ToLower(str)
}
}

func TestToLower(t *testing.T) {
It("toLower", func() {
str := "AaBbCcDdEeFfGg"
Expect(ToLower(str)).To(Equal(strings.ToLower(str)))

str = "ABCDE"
Expect(ToLower(str)).To(Equal(strings.ToLower(str)))

str = "ABCDE"
Expect(ToLower(str)).To(Equal(strings.ToLower(str)))

str = "abced"
Expect(ToLower(str)).To(Equal(strings.ToLower(str)))
})
}

func TestIsLower(t *testing.T) {
It("isLower", func() {
str := "AaBbCcDdEeFfGg"
Expect(isLower(str)).To(BeFalse())

str = "ABCDE"
Expect(isLower(str)).To(BeFalse())

str = "abcdefg"
Expect(isLower(str)).To(BeTrue())
})
}
8 changes: 0 additions & 8 deletions package.json

This file was deleted.

0 comments on commit d2798b1

Please sign in to comment.