MCPcopy Index your code
hub / github.com/go-openapi/jsonpointer / rebindChild

Function rebindChild

pointer.go:320–362  ·  view source on GitHub ↗

rebindChild writes newChild back into node at decodedToken. For cases where the child was already mutated in place (pointer aliasing, addressable slice elements) the rebind is a safe no-op. For cases where the child was returned by value (map entries holding a slice, slices reached through a non-a

(node any, decodedToken string, newChild any, nameProvider NameProvider)

Source from the content-addressed store, hash-verified

318// Parents implementing [JSONPointable] are left alone: they took ownership of the child via
319// JSONLookup and did not opt into a JSONSet-based rebind on intermediate tokens.
320func rebindChild(node any, decodedToken string, newChild any, nameProvider NameProvider) (any, error) {
321 if _, ok := node.(JSONPointable); ok {
322 return node, nil
323 }
324
325 rValue := reflect.Indirect(reflect.ValueOf(node))
326
327 switch rValue.Kind() {
328 case reflect.Struct:
329 nm, ok := nameProvider.GetGoNameForType(rValue.Type(), decodedToken)
330 if !ok {
331 return node, fmt.Errorf("object has no field %q: %w", decodedToken, ErrPointer)
332 }
333 fld := rValue.FieldByName(nm)
334 if !fld.CanSet() {
335 return node, nil
336 }
337 assignReflectValue(fld, newChild)
338 return node, nil
339
340 case reflect.Map:
341 rValue.SetMapIndex(reflect.ValueOf(decodedToken), reflect.ValueOf(newChild))
342 return node, nil
343
344 case reflect.Slice:
345 if decodedToken == dashToken {
346 return node, errDashIntermediate()
347 }
348 idx, err := strconv.Atoi(decodedToken)
349 if err != nil {
350 return node, errors.Join(err, ErrPointer)
351 }
352 elem := rValue.Index(idx)
353 if !elem.CanSet() {
354 return node, nil
355 }
356 assignReflectValue(elem, newChild)
357 return node, nil
358
359 default:
360 return node, errInvalidReference(decodedToken)
361 }
362}
363
364// assignReflectValue assigns src into dst, unwrapping a pointer when dst expects the pointee type.
365//

Callers 1

setAtMethod · 0.85

Calls 4

assignReflectValueFunction · 0.85
errDashIntermediateFunction · 0.85
errInvalidReferenceFunction · 0.85
GetGoNameForTypeMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…