MCPcopy Create free account
hub / github.com/AdRoll/baker / decodeAndCheckConfig

Function decodeAndCheckConfig

config.go:233–277  ·  view source on GitHub ↗
(md toml.MetaData, compCfg interface{})

Source from the content-addressed store, hash-verified

231}
232
233func decodeAndCheckConfig(md toml.MetaData, compCfg interface{}) error {
234 var (
235 cfg *toml.Primitive // config
236 dcfg interface{} // decoded config
237 name string // component name
238 typ string // component type
239 )
240
241 switch t := compCfg.(type) {
242 case ConfigInput:
243 cfg, dcfg = t.Config, t.DecodedConfig
244 name, typ = t.Name, "input"
245 case ConfigFilter:
246 cfg, dcfg = t.Config, t.DecodedConfig
247 name, typ = t.Name, "filter"
248 case ConfigOutput:
249 cfg, dcfg = t.Config, t.DecodedConfig
250 name, typ = t.Name, "output"
251 case ConfigUpload:
252 cfg, dcfg = t.Config, t.DecodedConfig
253 name, typ = t.Name, "upload"
254 case ConfigMetrics:
255 cfg, dcfg = t.Config, t.DecodedConfig
256 name, typ = t.Name, "metrics"
257 default:
258 panic(fmt.Sprintf("unexpected type %#v", cfg))
259 }
260
261 if cfg == nil {
262 // No config section was given in the TOML, so we create a pointer to
263 // the zero value of our config struct to check required field
264 tcfg := reflect.TypeOf(dcfg).Elem()
265 dcfg = reflect.New(tcfg).Interface()
266 } else {
267 if err := md.PrimitiveDecode(*cfg, dcfg); err != nil {
268 return fmt.Errorf("%s %q: error parsing config: %v", typ, name, err)
269 }
270 }
271
272 if req := CheckRequiredFields(dcfg); req != "" {
273 return fmt.Errorf("%s %q: %w", typ, name, ErrorRequiredField{req})
274 }
275
276 return nil
277}
278
279// NewConfigFromToml creates a Config from a reader reading from a TOML
280// configuration. comp describes all the existing components.

Callers 1

NewConfigFromTomlFunction · 0.85

Calls 1

CheckRequiredFieldsFunction · 0.85

Tested by

no test coverage detected