Skip to content

Commit

Permalink
feat: check that old field is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 18, 2024
1 parent 2933229 commit ac75992
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ const (
FlagMaxAmount = "max-amount"
)

type ForbiddenField struct {
FieldName string
Reason string
}

var (
forbiddenFieldsOnConfig = []ForbiddenField{
{
FieldName: "Aggregator.Synchronizer.DB",
Reason: "Field deprecated use Aggregator.Synchronizer.SQLDB instead",
},
}
)

/*
Config represents the configuration of the entire Hermez Node
The file is [TOML format]
Expand Down Expand Up @@ -135,6 +149,7 @@ func Load(ctx *cli.Context) (*Config, error) {
if err != nil {
return nil, err
}
expectedKeys := viper.AllKeys()

configFilePath := ctx.String(FlagCfg)
if configFilePath != "" {
Expand All @@ -160,7 +175,6 @@ func Load(ctx *cli.Context) (*Config, error) {
log.Error("config file not found")
} else {
log.Errorf("error reading config file: ", err)

return nil, err
}
}
Expand All @@ -179,8 +193,47 @@ func Load(ctx *cli.Context) (*Config, error) {
if err != nil {
return nil, err
}

if expectedKeys != nil {
configKeys := viper.AllKeys()
unexpectedFields := getUnexpectedFields(configKeys, expectedKeys)
for _, field := range unexpectedFields {
forbbidenInfo := getForbiddenField(field)
if forbbidenInfo != nil {
log.Errorf("forbidden field %s in config file: %s", field, forbbidenInfo.Reason)
} else {
log.Warnf("unexpected field %s in config file", field)
}
}
}
fmt.Println("cfg", cfg.NetworkConfig.L1Config)

return cfg, nil
}

func getForbiddenField(fieldName string) *ForbiddenField {
for _, forbiddenField := range forbiddenFieldsOnConfig {
if forbiddenField.FieldName == fieldName {
return &forbiddenField
}
}
return nil
}

func getUnexpectedFields(keysOnFile, expectedConfigKeys []string) []string {
wrongFields := make([]string, 0)
for _, key := range keysOnFile {
if !contains(expectedConfigKeys, key) {
wrongFields = append(wrongFields, key)
}
}
return wrongFields
}

func contains(keys []string, key string) bool {
for _, k := range keys {
if k == key {
return true
}
}
return false
}

0 comments on commit ac75992

Please sign in to comment.