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

Function validateInterfaceMap

pkg/lib/configreader/interface_map.go:83–161  ·  view source on GitHub ↗
(val map[string]interface{}, v *InterfaceMapValidation)

Source from the content-addressed store, hash-verified

81}
82
83func validateInterfaceMap(val map[string]interface{}, v *InterfaceMapValidation) (map[string]interface{}, error) {
84 if v.RequireCortexResources {
85 if err := checkOnlyCortexResources(val); err != nil {
86 return nil, err
87 }
88 } else if !v.AllowCortexResources {
89 if err := checkNoCortexResources(val); err != nil {
90 return nil, err
91 }
92 }
93
94 if !v.AllowEmpty {
95 if val != nil && len(val) == 0 {
96 return nil, ErrorCannotBeEmpty()
97 }
98 }
99
100 if v.ScalarsOnly {
101 for k, v := range val {
102 if !cast.IsScalarType(v) {
103 return nil, errors.Wrap(ErrorInvalidPrimitiveType(v, PrimTypeString, PrimTypeInt, PrimTypeFloat, PrimTypeBool), k)
104 }
105 }
106 }
107
108 if v.StringLeavesOnly {
109 _, err := FlattenAllStrValues(val)
110 if err != nil {
111 return nil, err
112 }
113 }
114
115 if v.AllowedLeafValues != nil {
116 leafVals, err := FlattenAllStrValues(val)
117 if err != nil {
118 return nil, err
119 }
120 for _, leafVal := range leafVals {
121 if !slices.HasString(v.AllowedLeafValues, leafVal) {
122 return nil, ErrorInvalidStr(leafVal, v.AllowedLeafValues[0], v.AllowedLeafValues[1:]...)
123 }
124 }
125 }
126
127 if v.StringKeysOnly {
128 for key, value := range val {
129 m, ok := cast.InterfaceToInterfaceInterfaceMap(value)
130 if !ok {
131 continue
132 }
133
134 stringToIntMap := map[string]interface{}{}
135 for kInterface, vInterface := range m {
136 kString, ok := kInterface.(string)
137 if !ok {
138 return nil, errors.Wrap(ErrorNonStringKeyFound(kInterface), key)
139 }
140 stringToIntMap[kString] = vInterface

Callers 2

Calls 11

IsScalarTypeFunction · 0.92
WrapFunction · 0.92
HasStringFunction · 0.92
checkOnlyCortexResourcesFunction · 0.85
checkNoCortexResourcesFunction · 0.85
ErrorCannotBeEmptyFunction · 0.85
FlattenAllStrValuesFunction · 0.85
ErrorInvalidStrFunction · 0.85
ErrorNonStringKeyFoundFunction · 0.85

Tested by

no test coverage detected