GetMapKey returns the value at the specified key in the map.
(key string)
| 83 | |
| 84 | // GetMapKey returns the value at the specified key in the map. |
| 85 | func (v *Value) GetMapKey(key string) (*Value, error) { |
| 86 | switch { |
| 87 | case v.isDencodingMap(): |
| 88 | m, err := v.dencodingMapValue() |
| 89 | if err != nil { |
| 90 | return nil, fmt.Errorf("error getting map: %w", err) |
| 91 | } |
| 92 | val, ok := m.Get(key) |
| 93 | if !ok { |
| 94 | return nil, MapKeyNotFound{Key: key} |
| 95 | } |
| 96 | if modelValue, isValue := val.(*Value); isValue { |
| 97 | modelValue.setFn = func(newValue *Value) error { |
| 98 | m.Set(key, newValue) |
| 99 | return nil |
| 100 | } |
| 101 | return modelValue, nil |
| 102 | } |
| 103 | res := NewValue(val) |
| 104 | res.setFn = func(newValue *Value) error { |
| 105 | m.Set(key, newValue) |
| 106 | return nil |
| 107 | } |
| 108 | return res, nil |
| 109 | case v.isStandardMap(): |
| 110 | unpacked, err := v.UnpackUntilKind(reflect.Map) |
| 111 | if err != nil { |
| 112 | return nil, fmt.Errorf("error unpacking value: %w", err) |
| 113 | } |
| 114 | i := unpacked.value.MapIndex(reflect.ValueOf(key)) |
| 115 | if !i.IsValid() { |
| 116 | return nil, MapKeyNotFound{Key: key} |
| 117 | } |
| 118 | res := NewValue(i) |
| 119 | res.setFn = func(newValue *Value) error { |
| 120 | mapRv, err := v.UnpackUntilKind(reflect.Map) |
| 121 | if err != nil { |
| 122 | return fmt.Errorf("error unpacking value: %w", err) |
| 123 | } |
| 124 | mapRv.value.SetMapIndex(reflect.ValueOf(key), newValue.value) |
| 125 | return nil |
| 126 | } |
| 127 | return res, nil |
| 128 | default: |
| 129 | return nil, ErrUnexpectedType{ |
| 130 | Expected: TypeMap, |
| 131 | Actual: v.Type(), |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // DeleteMapKey deletes the key from the map. |
| 137 | func (v *Value) DeleteMapKey(key string) error { |