MCPcopy
hub / github.com/cortexlabs/cortex / FlattenAllStrValues

Function FlattenAllStrValues

pkg/lib/configreader/interface.go:153–184  ·  view source on GitHub ↗

FlattenAllStrValues assumes that the order for maps is deterministic

(obj interface{})

Source from the content-addressed store, hash-verified

151
152// FlattenAllStrValues assumes that the order for maps is deterministic
153func FlattenAllStrValues(obj interface{}) ([]string, error) {
154 obj = pointer.IndirectSafe(obj)
155 flattened := []string{}
156
157 if objStr, ok := obj.(string); ok {
158 return append(flattened, objStr), nil
159 }
160
161 if objSlice, ok := cast.InterfaceToInterfaceSlice(obj); ok {
162 for i, elem := range objSlice {
163 subFlattened, err := FlattenAllStrValues(elem)
164 if err != nil {
165 return nil, errors.Wrap(err, s.Index(i))
166 }
167 flattened = append(flattened, subFlattened...)
168 }
169 return flattened, nil
170 }
171
172 if objMap, ok := cast.InterfaceToStrInterfaceMap(obj); ok {
173 for _, key := range maps.InterfaceMapSortedKeys(objMap) {
174 subFlattened, err := FlattenAllStrValues(objMap[key])
175 if err != nil {
176 return nil, errors.Wrap(err, s.UserStrStripped(key))
177 }
178 flattened = append(flattened, subFlattened...)
179 }
180 return flattened, nil
181 }
182
183 return nil, ErrorInvalidPrimitiveType(obj, PrimTypeString, PrimTypeList, PrimTypeMap)
184}
185
186func FlattenAllStrValuesAsSet(obj interface{}) (strset.Set, error) {
187 strs, err := FlattenAllStrValues(obj)

Callers 3

CheckFlattenAllStrValuesFunction · 0.85
FlattenAllStrValuesAsSetFunction · 0.85
validateInterfaceMapFunction · 0.85

Calls 6

IndirectSafeFunction · 0.92
WrapFunction · 0.92
InterfaceMapSortedKeysFunction · 0.92

Tested by 1

CheckFlattenAllStrValuesFunction · 0.68