MCPcopy
hub / github.com/TomWright/dasel / GetMapKey

Method GetMapKey

model/value_map.go:85–134  ·  view source on GitHub ↗

GetMapKey returns the value at the specified key in the map.

(key string)

Source from the content-addressed store, hash-verified

83
84// GetMapKey returns the value at the specified key in the map.
85func (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.
137func (v *Value) DeleteMapKey(key string) error {

Callers 15

EqualTypeValueMethod · 0.95
MapKeyExistsMethod · 0.95
RangeMapMethod · 0.95
MapKeyValuesMethod · 0.95
TestValue_Set_SetFnFunction · 0.95
TestMapFunction · 0.80
TestMapCopyFunction · 0.80
TestValue_CopyFunction · 0.80
nodesToValueFunction · 0.80
TestReader_EmptyNodeFunction · 0.80

Calls 8

isDencodingMapMethod · 0.95
dencodingMapValueMethod · 0.95
isStandardMapMethod · 0.95
UnpackUntilKindMethod · 0.95
TypeMethod · 0.95
NewValueFunction · 0.85
GetMethod · 0.45
SetMethod · 0.45

Tested by 15

TestValue_Set_SetFnFunction · 0.76
TestMapFunction · 0.64
TestMapCopyFunction · 0.64
TestValue_CopyFunction · 0.64
TestReader_EmptyNodeFunction · 0.64
TestReader_V2KeywordsFunction · 0.64
assertMapStringValueFunction · 0.64
assertMapIntValueFunction · 0.64