From 8926f2992ac1cc9ec72ba196e18934bae0579df4 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Fri, 29 Jan 2021 12:04:39 +0200 Subject: [PATCH] Cleanup --- internal/hscan/hscan.go | 15 +++++---------- internal/hscan/structmap.go | 21 ++++++++------------- 2 files changed, 13 insertions(+), 23 deletions(-) diff --git a/internal/hscan/hscan.go b/internal/hscan/hscan.go index a2b3c92..1c46f2c 100644 --- a/internal/hscan/hscan.go +++ b/internal/hscan/hscan.go @@ -34,7 +34,7 @@ var ( reflect.Interface: decodeUnsupported, reflect.Map: decodeUnsupported, reflect.Ptr: decodeUnsupported, - reflect.Slice: decodeStringSlice, + reflect.Slice: decodeSlice, reflect.String: decodeString, reflect.Struct: decodeUnsupported, reflect.UnsafePointer: decodeUnsupported, @@ -69,12 +69,7 @@ func Scan(vals []interface{}, dest interface{}) error { // iterating through the fields of a struct type every time values are // scanned into it. typ := v.Type() - fMap, ok := structSpecs.get(typ) - - if !ok { - fMap = makeStructSpecs(v, "redis") - structSpecs.set(typ, fMap) - } + fMap := structSpecs.get(typ) // Iterate through the (key, value) sequence. for i := 0; i < len(vals); i += 2 { @@ -143,7 +138,7 @@ func decodeString(f reflect.Value, s string) error { return nil } -func decodeStringSlice(f reflect.Value, s string) error { +func decodeSlice(f reflect.Value, s string) error { // []byte slice ([]uint8). if f.Type().Elem().Kind() == reflect.Uint8 { f.SetBytes([]byte(s)) @@ -151,6 +146,6 @@ func decodeStringSlice(f reflect.Value, s string) error { return nil } -func decodeUnsupported(f reflect.Value, s string) error { - return fmt.Errorf("redis.Scan(unsupported type %v)", f) +func decodeUnsupported(v reflect.Value, s string) error { + return fmt.Errorf("redis.Scan(unsupported %s)", v.Type()) } diff --git a/internal/hscan/structmap.go b/internal/hscan/structmap.go index 130b07b..d66f76f 100644 --- a/internal/hscan/structmap.go +++ b/internal/hscan/structmap.go @@ -24,22 +24,17 @@ type structMap struct { } func newStructMap() *structMap { - return &structMap{ - m: sync.Map{}, - } + return new(structMap) } -func (s *structMap) get(t reflect.Type) (*structFields, bool) { - m, ok := s.m.Load(t) - if !ok { - return nil, ok +func (s *structMap) get(t reflect.Type) *structFields { + if v, ok := s.m.Load(t); ok { + return m.(*structFields), ok } - return m.(*structFields), true -} - -func (s *structMap) set(t reflect.Type, sf *structFields) { - s.m.Store(t, sf) + fMap := getStructFields(v, "redis") + s.m.Store(t, fMap) + return fmap, true } func newStructFields() *structFields { @@ -57,7 +52,7 @@ func (s *structFields) get(tag string) (*structField, bool) { return f, ok } -func makeStructSpecs(ob reflect.Value, fieldTag string) *structFields { +func getStructFields(ob reflect.Value, fieldTag string) *structFields { var ( num = ob.NumField() out = newStructFields()