MCPcopy Create free account
hub / github.com/DeAI-Artist/Linkis / decodeReflect

Function decodeReflect

libs/json/decoder.go:39–99  ·  view source on GitHub ↗
(bz []byte, rv reflect.Value)

Source from the content-addressed store, hash-verified

37}
38
39func decodeReflect(bz []byte, rv reflect.Value) error {
40 if !rv.CanAddr() {
41 return errors.New("value is not addressable")
42 }
43
44 // Handle null for slices, interfaces, and pointers
45 if bytes.Equal(bz, []byte("null")) {
46 rv.Set(reflect.Zero(rv.Type()))
47 return nil
48 }
49
50 // Dereference-and-construct pointers, to handle nested pointers.
51 for rv.Kind() == reflect.Ptr {
52 if rv.IsNil() {
53 rv.Set(reflect.New(rv.Type().Elem()))
54 }
55 rv = rv.Elem()
56 }
57
58 // Times must be UTC and end with Z
59 if rv.Type() == timeType {
60 switch {
61 case len(bz) < 2 || bz[0] != '"' || bz[len(bz)-1] != '"':
62 return fmt.Errorf("JSON time must be an RFC3339 string, but got %q", bz)
63 case bz[len(bz)-2] != 'Z':
64 return fmt.Errorf("JSON time must be UTC and end with 'Z', but got %q", bz)
65 }
66 }
67
68 // If value implements json.Umarshaler, call it.
69 if rv.Addr().Type().Implements(jsonUnmarshalerType) {
70 return rv.Addr().Interface().(json.Unmarshaler).UnmarshalJSON(bz)
71 }
72
73 switch rv.Type().Kind() {
74 // Decode complex types recursively.
75 case reflect.Slice, reflect.Array:
76 return decodeReflectList(bz, rv)
77
78 case reflect.Map:
79 return decodeReflectMap(bz, rv)
80
81 case reflect.Struct:
82 return decodeReflectStruct(bz, rv)
83
84 case reflect.Interface:
85 return decodeReflectInterface(bz, rv)
86
87 // For 64-bit integers, unwrap expected string and defer to stdlib for integer decoding.
88 case reflect.Int64, reflect.Int, reflect.Uint64, reflect.Uint:
89 if bz[0] != '"' || bz[len(bz)-1] != '"' {
90 return fmt.Errorf("invalid 64-bit integer encoding %q, expected string", string(bz))
91 }
92 bz = bz[1 : len(bz)-1]
93 fallthrough
94
95 // Anything else we defer to the stdlib.
96 default:

Callers 5

decodeFunction · 0.85
decodeReflectListFunction · 0.85
decodeReflectMapFunction · 0.85
decodeReflectStructFunction · 0.85
decodeReflectInterfaceFunction · 0.85

Calls 10

decodeReflectListFunction · 0.85
decodeReflectMapFunction · 0.85
decodeReflectStructFunction · 0.85
decodeReflectInterfaceFunction · 0.85
decodeStdlibFunction · 0.85
EqualMethod · 0.65
SetMethod · 0.65
TypeMethod · 0.65
AddrMethod · 0.45
UnmarshalJSONMethod · 0.45

Tested by

no test coverage detected