Skip to content

Commit

Permalink
Fix issue with field load conditions on conditionally required fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Rheisen committed Sep 9, 2023
1 parent 494e746 commit e6ab606
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ go get github.com/rheisen/bconf
### Why `bconf`

`bconf` provides tooling to write your configuration package by package. With `bconf`, configuration lives right
alongside the code that needs it. This makes it so that configuration is more easily re-used and composible by
alongside the code that needs it. This also makes it so that configuration is more easily re-used and composible by
multiple applications (just like your packages should be).

`bconf` accomplishes this with `bconf.FieldSets`, which provide a namespace and logical grouping for related
Expand Down Expand Up @@ -57,7 +57,8 @@ In Progress

* Ability to generate default configuration values with the `bconf.Field` `DefaultGenerator` parameter
* Ability to define custom configuration value validation with the `bconf.Field` `Validator` parameter
* Ability to conditionally load `bconf.FieldSets` by defining `bconf.LoadConditions`
* Ability to conditionally load a `bconf.FieldSet` by defining `bconf.LoadConditions`
* Ability to conditionally load a `bconf.Field` by defining `bconf.LoadConditions`
* Ability to get a safe map of configuration values from the `bconf.AppConfig` `ConfigMap()` function
* (the configuration map will obfuscate values from fields with `Sensitive` parameter set to `true`)
* Ability to reload field-sets and individual fields via the `bconf.AppConfig`
Expand Down
12 changes: 11 additions & 1 deletion app_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,10 +619,20 @@ func (c *AppConfig) loadFieldSet(fieldSetKey string) []error {
}

for _, field := range fieldSet.fieldMap {
if field.Required {
if field.Required && len(field.LoadConditions) < 1 {
if _, err := field.getValue(); err != nil {
errs = append(errs, fmt.Errorf("required field '%s_%s' not set", fieldSet.Key, field.Key))
}
} else if field.Required {
if load, _ := c.shouldLoadField(field, fieldSet.Key); load {
if _, err := field.getValue(); err != nil {
errs = append(errs, fmt.Errorf(
"conditionally required field '%s_%s' load condition met, but field value not set",
fieldSet.Key,
field.Key,
))
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions app_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ func TestAppConfigWithFieldLoadConditions(t *testing.T) {
// test field load condition w/ invalid field-set key
// test help string output with no field-set key
// test help string output with field-set key
// test conditionally required field cannot be unset

fieldSetWithInternalFieldDependencies := bconf.FSB().Key(fieldSetOneKey).Fields(
bconf.FB().Key(fieldAKey).Type(bconf.String).Default("postgres").Create(),
Expand Down

0 comments on commit e6ab606

Please sign in to comment.