MCPcopy
hub / github.com/redis/go-redis / Scan

Method Scan

internal/hscan/structmap.go:89–127  ·  view source on GitHub ↗
(key string, value string)

Source from the content-addressed store, hash-verified

87}
88
89func (s StructValue) Scan(key string, value string) error {
90 field, ok := s.spec.m[key]
91 if !ok {
92 return nil
93 }
94
95 v := s.value.Field(field.index)
96 isPtr := v.Kind() == reflect.Ptr
97
98 if isPtr && v.IsNil() {
99 v.Set(reflect.New(v.Type().Elem()))
100 }
101 if !isPtr && v.Type().Name() != "" && v.CanAddr() {
102 v = v.Addr()
103 isPtr = true
104 }
105
106 if isPtr && v.Type().NumMethod() > 0 && v.CanInterface() {
107 switch scan := v.Interface().(type) {
108 case Scanner:
109 return scan.ScanRedis(value)
110 case encoding.TextUnmarshaler:
111 return scan.UnmarshalText(util.StringToBytes(value))
112 case encoding.BinaryUnmarshaler:
113 return scan.UnmarshalBinary(util.StringToBytes(value))
114 }
115 }
116
117 if isPtr {
118 v = v.Elem()
119 }
120
121 if err := field.fn(v, value); err != nil {
122 t := s.value.Type()
123 return fmt.Errorf("cannot scan redis.result %s into struct field %s.%s of type %s, error-%s",
124 value, t.Name(), t.Field(field.index).Name, t.Field(field.index).Type, err.Error())
125 }
126 return nil
127}

Callers

nothing calls this directly

Calls 8

StringToBytesFunction · 0.92
SetMethod · 0.65
TypeMethod · 0.65
NameMethod · 0.65
ScanRedisMethod · 0.65
AddrMethod · 0.45
UnmarshalBinaryMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected