Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-210 - Add Required property to indicate a config registration c… #211

Merged
merged 6 commits into from
Jun 21, 2024

Conversation

BowlOfSoup
Copy link
Contributor

@BowlOfSoup BowlOfSoup commented Jun 20, 2024

Add Required property to indicate a config registration can't be omitted or nil (+ tests)

References

Issue(s): #210

Description

Changes in this PR make it possible to, when registering a config value, to indicate that the value can not be nil and also validates omitting the complete config line. Both will trigger a validation message.

Possible drawbacks/suggestions

Imo we should provide a 'constructor' like config.NewEntry() in which we can set default values to prevent this change to the Entry struct to be a breaking change for people not using keyed initialization on structs. This was discussed and marked as not breaking.

Proposal for constructor NewEntry():

package config

type EntryOpts struct {
    AuthorizedValues []any
    IsSlice bool
    Required bool
}

func NewEntry(value any, type reflect.Kind, opts EntryOpts) *Entry {
    return &Entry{
        Value: "default value",
	Type: reflect.String,
	IsSlice: opts.IsSlice,
	AuthorizedValues: opts.AutorizedValues,
	Required: opts.Required
    }
}

// use it like this
config.Register("customCategory.customEntry", config.NewEntry(
    Value: "default value",
    Type: reflect.String,
    EntryOpts{
        AuthorizedValues: []any{},
        IsSlice: false,
        Required: true,
    }
)

@System-Glitch System-Glitch self-requested a review June 20, 2024 12:13
@System-Glitch System-Glitch self-assigned this Jun 20, 2024
@System-Glitch System-Glitch added the enhancement Enhancement of existing feature label Jun 20, 2024
Copy link
Member

@System-Glitch System-Glitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a breaking change to me if we drop the "empty" validation. It would only be breaking for codebases not always using keyed struct fields, which is very rare and usually not recommended anyway. The behavior for entries without Required: true stays the same as before.

I would prefer not adding NewEntry().

config/default.go Outdated Show resolved Hide resolved
config/entry.go Outdated Show resolved Hide resolved
config/entry.go Outdated Show resolved Hide resolved
config/entry.go Outdated Show resolved Hide resolved
config/entry.go Outdated Show resolved Hide resolved
Copy link
Member

@System-Glitch System-Glitch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor adjustments to make, otherwise LGTM! 👍🏻

config/entry.go Outdated Show resolved Hide resolved
config/entry.go Outdated
if err := e.tryEnvVarConversion(key); err != nil {
return err
}

v := reflect.ValueOf(e.Value)
if e.Required && (!v.IsValid() || e.Value == nil) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if e.Required && (!v.IsValid() || e.Value == nil) {
if e.Required && (!v.IsValid() || v.IsNil()) {

Copy link
Contributor Author

@BowlOfSoup BowlOfSoup Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsNil reports whether its argument v is nil. The argument must be a chan, func, interface, map, pointer, or slice value; if it is not, IsNil panics.

I've tested this when you first suggested it, but it will not work if the value is not one of the above. This happens e.g. when an inputted value is actually nil.

So !v.IsValid() || e.Value == nil would cover the functionality we want.

If it's not a value (IsValid) it will return true. If it is, it will check for explicit nil.

Copy link
Member

@System-Glitch System-Glitch Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Here is what I suggest to cover the scenario I showed in the playground link earlier.

Suggested change
if e.Required && (!v.IsValid() || e.Value == nil) {
if e.Required && (!v.IsValid() || e.Value == nil || (v.Kind() == reflect.Pointer && v.IsNil())) {

Updated playground link

config/config_test.go Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jun 21, 2024

Pull Request Test Coverage Report for Build 9608948098

Details

  • 11 of 11 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 97.335%

Totals Coverage Status
Change from base Build 9594754702: 0.001%
Covered Lines: 6100
Relevant Lines: 6267

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 21, 2024

Pull Request Test Coverage Report for Build 9610067852

Details

  • 11 of 11 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 97.335%

Totals Coverage Status
Change from base Build 9594754702: 0.001%
Covered Lines: 6100
Relevant Lines: 6267

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 21, 2024

Pull Request Test Coverage Report for Build 9610325654

Details

  • 11 of 11 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.001%) to 97.335%

Totals Coverage Status
Change from base Build 9594754702: 0.001%
Covered Lines: 6100
Relevant Lines: 6267

💛 - Coveralls

@System-Glitch System-Glitch merged commit 3b612aa into go-goyave:master Jun 21, 2024
4 checks passed
@System-Glitch
Copy link
Member

Thank you very much! 🎉

@BowlOfSoup BowlOfSoup deleted the ISSUE-210 branch June 21, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement of existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants