github.com/smartcontractkit/chainlink-testing-framework/libs@v0.0.0-20240227141906-ec710b4eb1a3/config/utils.go (about) 1 package config 2 3 import ( 4 "github.com/pelletier/go-toml/v2" 5 "github.com/pkg/errors" 6 "github.com/rs/zerolog" 7 ) 8 9 // BytesToAnyTomlStruct unmarshals the given bytes into the given target struct, apart from unmarshalling the root 10 // it also looks for given configuration name and unmarshals it into the target struct overwriting the root. 11 // Target needs to be a struct with exported fields with `toml:"field_name"` tags. 12 func BytesToAnyTomlStruct(logger zerolog.Logger, filename, configurationName string, target any, content []byte) error { 13 err := toml.Unmarshal(content, target) 14 15 if err != nil { 16 return errors.Wrapf(err, "error unmarshaling config") 17 } 18 19 logger.Debug().Msgf("Successfully unmarshalled %s config file", filename) 20 21 var someToml map[string]interface{} 22 err = toml.Unmarshal(content, &someToml) 23 if err != nil { 24 return err 25 } 26 27 if configurationName == "" { 28 logger.Debug().Msgf("No configuration name provided, will read only default configuration.") 29 30 return nil 31 } 32 33 if _, ok := someToml[configurationName]; !ok { 34 logger.Debug().Msgf("Config file %s does not contain configuration named '%s', will read only default configuration.", filename, configurationName) 35 return nil 36 } 37 38 marshalled, err := toml.Marshal(someToml[configurationName]) 39 if err != nil { 40 return err 41 } 42 43 err = toml.Unmarshal(marshalled, target) 44 if err != nil { 45 return err 46 } 47 48 logger.Debug().Msgf("Configuration named '%s' read successfully.", configurationName) 49 50 return nil 51 }