X Tutup
Skip to content

add GetOrDefault and related methods to Config#5022

Merged
vilmibm merged 4 commits intotrunkfrom
config-defaults
Jan 17, 2022
Merged

add GetOrDefault and related methods to Config#5022
vilmibm merged 4 commits intotrunkfrom
config-defaults

Conversation

@vilmibm
Copy link
Copy Markdown
Contributor

@vilmibm vilmibm commented Jan 11, 2022

Part of #3091
Part of #2711

This PR adds three new methods to our Config type:

  • GetOrDefault
  • GetOrDefaultWithSource
  • Default

and changes the behavior of Get and GetWithSource to now not automatically use a default value if no value has been set for a key.

This change allows callers of the config system to know when a config value has been explicitly set by the user.

All existing invocations of Get that cared about a default have been replaced with GetOrDefault to maintain the same functionality as prior to this PR and there are no intended end-user visible changes here.

@vilmibm vilmibm requested a review from a team as a code owner January 11, 2022 20:59
@vilmibm vilmibm requested review from samcoe and removed request for a team January 11, 2022 20:59
Copy link
Copy Markdown
Contributor

@samcoe samcoe left a comment

Choose a reason for hiding this comment

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

This code looks good to me. What are next steps on making this operational? Seems like we will need to change configFunc() in default.go to return something other than a blank config when a config does not exist.

Copy link
Copy Markdown
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

Looks good except that I don't understand why so many changes across the board? I had thought that only fetching keys that might have defaults will need to be switched to GetOrDefault— everything else can stay the same. But it looks like all Gets across the codebase were ported over?

@vilmibm
Copy link
Copy Markdown
Contributor Author

vilmibm commented Jan 14, 2022

@mislav good points. I did the bulk change because the exact behavior of Get (returning a default if unset) now exists in GetOrDefault so I mindlessly find-replaced; but you are correct that all those invocations of Get for keys with no default did not need to be ported. I'll undo those changes.

@vilmibm vilmibm enabled auto-merge January 14, 2022 21:36
@vilmibm vilmibm requested a review from mislav January 14, 2022 22:09
@vilmibm
Copy link
Copy Markdown
Contributor Author

vilmibm commented Jan 14, 2022

@mislav those call sites are updated.

return c[fmt.Sprintf("%s:%s", host, key)], c["_source"], nil
}

func (c tinyConfig) Get(host, key string) (val string, err error) {
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.

Was adding this method necessary? The interface that this struct aims to satisfy only has one method:

type config interface {
GetWithSource(string, string) (string, string, error)
}

@vilmibm vilmibm merged commit 4b415f8 into trunk Jan 17, 2022
@vilmibm vilmibm deleted the config-defaults branch January 17, 2022 16: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.

3 participants

X Tutup