1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// Copyright 2022 PerimeterX. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package marshmallow
import (
"encoding/json"
"reflect"
"strings"
)
var unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
type reflectionInfo struct {
path []int
t reflect.Type
}
func (r reflectionInfo) field(target reflect.Value) reflect.Value {
current := target
for _, i := range r.path {
current = current.Field(i)
}
return current
}
func mapStructFields(target interface{}) map[string]reflectionInfo {
t := reflectStructType(target)
result := cacheLookup(t)
if result != nil {
return result
}
result = make(map[string]reflectionInfo, t.NumField())
mapTypeFields(t, result, nil)
cacheStore(t, result)
return result
}
func mapTypeFields(t reflect.Type, result map[string]reflectionInfo, path []int) {
num := t.NumField()
for i := 0; i < num; i++ {
field := t.Field(i)
fieldPath := append(path, i)
if field.Anonymous && field.Type.Kind() == reflect.Struct {
mapTypeFields(field.Type, result, fieldPath)
continue
}
name := field.Tag.Get("json")
if name == "" || name == "-" {
continue
}
if index := strings.Index(name, ","); index > -1 {
name = name[:index]
}
result[name] = reflectionInfo{
path: fieldPath,
t: field.Type,
}
}
}
func reflectStructValue(target interface{}) reflect.Value {
v := reflect.ValueOf(target)
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v
}
func reflectStructType(target interface{}) reflect.Type {
t := reflect.TypeOf(target)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t
}
var primitiveConverters = map[reflect.Kind]func(v interface{}) (interface{}, bool){
reflect.Bool: func(v interface{}) (interface{}, bool) {
res, ok := v.(bool)
return res, ok
},
reflect.Int: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return int(res), true
}
return v, false
},
reflect.Int8: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return int8(res), true
}
return v, false
},
reflect.Int16: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return int16(res), true
}
return v, false
},
reflect.Int32: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return int32(res), true
}
return v, false
},
reflect.Int64: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return int64(res), true
}
return v, false
},
reflect.Uint: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return uint(res), true
}
return v, false
},
reflect.Uint8: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return uint8(res), true
}
return v, false
},
reflect.Uint16: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return uint16(res), true
}
return v, false
},
reflect.Uint32: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return uint32(res), true
}
return v, false
},
reflect.Uint64: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return uint64(res), true
}
return v, false
},
reflect.Float32: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return float32(res), true
}
return v, false
},
reflect.Float64: func(v interface{}) (interface{}, bool) {
res, ok := v.(float64)
if ok {
return res, true
}
return v, false
},
reflect.Interface: func(v interface{}) (interface{}, bool) {
return v, true
},
reflect.String: func(v interface{}) (interface{}, bool) {
res, ok := v.(string)
return res, ok
},
}
func assignValue(field reflect.Value, value interface{}) {
if value == nil {
return
}
reflectValue := reflect.ValueOf(value)
if reflectValue.Type().AssignableTo(field.Type()) {
field.Set(reflectValue)
}
}
func isValidValue(v interface{}) bool {
value := reflect.ValueOf(v)
return value.Kind() == reflect.Ptr && value.Elem().Kind() == reflect.Struct && !value.IsNil()
}
func safeReflectValue(t reflect.Type, v interface{}) reflect.Value {
if v == nil {
return reflect.Zero(t)
}
return reflect.ValueOf(v)
}
|