Skip to content

Handle required fields#41

Merged
arl merged 13 commits intomainfrom
handle-required-fields
Sep 21, 2020
Merged

Handle required fields#41
arl merged 13 commits intomainfrom
handle-required-fields

Conversation

@arl
Copy link
Copy Markdown
Collaborator

@arl arl commented Sep 17, 2020

❓ What

Component configuration fields can now be tagged as required:"true". If that's the case, the configuration is considered valid if and only if the field has been set to a non-zero value. This allows to handle required fields in a single place, rather than all over the place, and with a homogeneous error message.

Modification to the components configuration to set required fields as required and remove component-specific checks will be pushed in a follow-up PR

🔨 How to test

  1. List all steps necessary;
  2. To test this pull request.

✅ Checklists

This section contains a list of checklists for common uses, please delete the checklists that are useless for your current use case (or add another checklist if your use case isn't covered yet).

  • Is there unit/integration test coverage for all new and/or changed functionality added in this PR?
  • Have the changes in this PR been functionally tested?
  • Has make gofmt-write been run on the code?
  • Has make govet been run on the code? Has the code been fixed accordingly to the output?
  • Have the changes been added to the CHANGELOG.md file?
  • Have the steps in CONTRIBUTING.md been followed to update a Go module?

@arl arl requested a review from tommyblue September 17, 2020 15:45
@arl arl mentioned this pull request Sep 17, 2020
6 tasks
@arl arl force-pushed the handle-required-fields branch 3 times, most recently from ea9bcca to f0f69e0 Compare September 21, 2020 08:58
Copy link
Copy Markdown
Contributor

@tommyblue tommyblue left a comment

Choose a reason for hiding this comment

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

sounds good, check the comments

Comment thread config.go
cfg, dcfg = t.Config, t.DecodedConfig
name, typ = t.Name, "metrics"
default:
panic(fmt.Sprintf("unexpected type %#v", cfg))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

panic? shouldn't it return an error?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Getting an unexpected type here means there's a developer error which must be fixed, no way around that.
That's the typical use case for panic

Comment thread config.go
panic(fmt.Sprintf("unexpected type %#v", cfg))
}

if cfg == nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

uhm, does this ever happen? isn't already panicked in the switch?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

uhm, does this ever happen? isn't already panicked in the switch?
yes it does. if t.Config is nil. t.Config is nil if the user didn't specify an [input.config] section in the TOML.

Comment thread config.go
return nil
}

if err := md.PrimitiveDecode(*cfg, dcfg); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

PrimitiveDecode is deprecated, in favor of MetaData.PrimitiveDecode. Maybe it's worth updating it here

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

where did you see that? link?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the editor suggested that, but I now see it makes a mistake: the deprecated function is func PrimitiveDecode, not func (md *MetaData) PrimitiveDecode

Comment thread config.go Outdated
Comment thread config.go Outdated
return fmt.Errorf("%s %q: error parsing config: %v", typ, name, err)
}

if req := CheckRequiredFields(dcfg); req != "" {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

req could be called fieldName?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

https://github.com/golang/go/wiki/CodeReviewComments#variable-names
IMHO fieldName seems a bit redundant here.
I actually hesitated calling it just name.
Maybe just calling it f could do if I transform the next line to be:

ErrorRequiredField{Field:f}

rather than

ErrorRequiredField{req}

Comment thread config.go
// "required" struct struct tag must be present and set to true.
//
// RequiredFields doesn't support struct embedding other structs.
func RequiredFields(cfg interface{}) []string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is this public for testing purposes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

no. I thought this could be a good addition to the public API for users that desire to use baker as a library.
We're provided the help functions and all but we should also provide the necessary API for users to differently.
That's why I added a detailed documentation there.

Comment thread config.go Outdated
// are set of the struct doesn't have any required fields (or any fields at all).
//
// CheckRequiredFields doesn't support struct embedding other structs.
func CheckRequiredFields(val interface{}) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is the function public for testing purposes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

same answer as for RequiredFields

@tommyblue
Copy link
Copy Markdown
Contributor

Oh, I forgot: I think we should also add this change to the README

@arl arl force-pushed the handle-required-fields branch from e1b6265 to 50cb62c Compare September 21, 2020 13:13
@arl
Copy link
Copy Markdown
Collaborator Author

arl commented Sep 21, 2020

Oh, I forgot: I think we should also add this change to the README

At the moment there's not a clear section in the README about the configuration structures of baker components.
I think we should create one and document the new feature there.
creating a ticket for that.
@tommyblue

Copy link
Copy Markdown
Contributor

@tommyblue tommyblue left a comment

Choose a reason for hiding this comment

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

ok, just remove the line from the changelog then merge

Comment thread CHANGELOG.md
- filter: add RegexMatch filter. [#37](https://github.com/AdRoll/baker/pull/37)
- filter: add Concatenate filter [#33](https://github.com/AdRoll/baker/pull/33)
- filter: add NotNull filter [#43](https://github.com/AdRoll/baker/pull/43)
- filter: add Concatenate filter [#28](https://github.com/AdRoll/baker/pull/33)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
- filter: add Concatenate filter [#28](https://github.com/AdRoll/baker/pull/33)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

already included in the file

@arl arl force-pushed the handle-required-fields branch from 50cb62c to 994e8fc Compare September 21, 2020 14:33
@arl arl merged commit b1fa829 into main Sep 21, 2020
@arl arl deleted the handle-required-fields branch September 21, 2020 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants