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

Function Struct

pkg/lib/configreader/reader.go:133–389  ·  view source on GitHub ↗
(dest interface{}, inter interface{}, v *StructValidation)

Source from the content-addressed store, hash-verified

131}
132
133func Struct(dest interface{}, inter interface{}, v *StructValidation) []error {
134 allowedFields := []string{}
135 allErrs := []error{}
136 var ok bool
137
138 if inter == nil {
139 if v.TreatNullAsEmpty {
140 inter = make(map[interface{}]interface{}, 0)
141 } else {
142 if !v.AllowExplicitNull {
143 return []error{ErrorCannotBeEmptyOrNull(v.Required)}
144 }
145 return nil
146 }
147 }
148
149 interMap, ok := cast.InterfaceToStrInterfaceMap(inter)
150 if !ok {
151 return []error{ErrorInvalidPrimitiveType(inter, PrimTypeMap)}
152 }
153
154 for _, structFieldValidation := range v.StructFieldValidations {
155 key := inferKey(reflect.TypeOf(dest), structFieldValidation.StructField, structFieldValidation.Key)
156 allowedFields = append(allowedFields, key)
157
158 if structFieldValidation.Nil == true {
159 continue
160 }
161
162 var err error
163 var errs []error
164 var val interface{}
165
166 if structFieldValidation.StringValidation != nil {
167 validation := *structFieldValidation.StringValidation
168 updateValidation(&validation, dest, structFieldValidation)
169 val, err = StringFromInterfaceMap(key, interMap, &validation)
170 if err == nil && structFieldValidation.Parser != nil {
171 val, err = structFieldValidation.Parser(val.(string))
172 err = errors.Wrap(err, key)
173 }
174 } else if structFieldValidation.StringPtrValidation != nil {
175 validation := *structFieldValidation.StringPtrValidation
176 updateValidation(&validation, dest, structFieldValidation)
177 val, err = StringPtrFromInterfaceMap(key, interMap, &validation)
178 if err == nil && structFieldValidation.Parser != nil {
179 if val.(*string) == nil {
180 val = nil
181 } else {
182 val, err = structFieldValidation.Parser(*val.(*string))
183 if err == nil && val != nil {
184 valValue := reflect.ValueOf(val)
185 valPtrValue := reflect.New(valValue.Type())
186 valPtrValue.Elem().Set(valValue)
187 val = valPtrValue.Interface()
188 } else {
189 val = nil
190 err = errors.Wrap(err, key)

Callers 6

testConfigFunction · 0.85
testConfigErrorFunction · 0.85
StructListFunction · 0.85
InterfaceStructFunction · 0.85
ParseYAMLFileFunction · 0.85
ParseYAMLBytesFunction · 0.85

Calls 15

WrapFunction · 0.92
WrapAllFunction · 0.92
PanicFunction · 0.92
AddErrorFunction · 0.92
AddErrorsFunction · 0.92
HasErrorFunction · 0.92
SubtractStrSliceFunction · 0.92
InterfaceMapKeysFunction · 0.92
ErrorCannotBeEmptyOrNullFunction · 0.85
inferKeyFunction · 0.85

Tested by 2

testConfigFunction · 0.68
testConfigErrorFunction · 0.68