MCPcopy
hub / github.com/danielgtaylor/huma / setDeepObjectValue

Function setDeepObjectValue

huma.go:1186–1236  ·  view source on GitHub ↗
(pb *PathBuffer, res *ValidateResult, f reflect.Value, data map[string]string)

Source from the content-addressed store, hash-verified

1184}
1185
1186func setDeepObjectValue(pb *PathBuffer, res *ValidateResult, f reflect.Value, data map[string]string) map[string]any {
1187 t := f.Type()
1188 result := make(map[string]any)
1189 switch t.Kind() {
1190 case reflect.Map:
1191 if t.Key().Kind() != reflect.String {
1192 panic("unsupported map key type")
1193 }
1194 f.Set(reflect.MakeMap(t))
1195 for k, v := range data {
1196 key := reflect.New(t.Key()).Elem()
1197 key.SetString(k)
1198 value := reflect.New(t.Elem()).Elem()
1199 if err := setFieldValue(value, v); err != nil {
1200 pb.Push(k)
1201 res.Add(pb, v, err.Error())
1202 pb.Pop()
1203 } else {
1204 f.SetMapIndex(key, value)
1205 result[k] = value.Interface()
1206 }
1207 }
1208 case reflect.Struct:
1209 for i := 0; i < t.NumField(); i++ {
1210 field := t.Field(i)
1211 // Get the field name
1212 fieldName := field.Name
1213
1214 if name := jsonName(field); name != "" {
1215 fieldName = name
1216 }
1217
1218 fv := f.Field(i)
1219 if val, ok := data[fieldName]; ok {
1220 if err := setFieldValue(fv, val); err != nil {
1221 pb.Push(fieldName)
1222 res.Add(pb, val, err.Error())
1223 pb.Pop()
1224 } else {
1225 result[fieldName] = fv.Interface()
1226 }
1227 } else {
1228 if val := field.Tag.Get("default"); val != "" {
1229 setFieldValue(fv, val)
1230 result[fieldName] = fv.Interface()
1231 }
1232 }
1233 }
1234 }
1235 return result
1236}
1237
1238func setFieldValue(f reflect.Value, value string) error {
1239 switch f.Kind() {

Callers 1

RegisterFunction · 0.85

Calls 7

setFieldValueFunction · 0.85
jsonNameFunction · 0.85
PushMethod · 0.80
PopMethod · 0.80
AddMethod · 0.65
ErrorMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…