-
Notifications
You must be signed in to change notification settings - Fork 9
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
change method of setting config defaults #192
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bf7ebf2
change method of setting config defaults
dustinblack 7c90f7a
update engine test to use podman
dustinblack bf73a03
update load test to podman
dustinblack 7046dd9
test go lint and test w/ podman added
dustinblack fb23b03
revert lint and test to action main branch
dustinblack ac6e415
simplify if condition
dustinblack File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the canonical way to set a map to
nil
is to just declare the variable with a type; the default value is nil rather than empty, which may be more efficient. (Although the details of Go behind-the-scenes memory management is obscure 😆 )That is,
var configData map[string]any
... although are there any config map values that aren't strings? Isn't it reallymap[string]string
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur with Dave: if you declare
configData
with the proper type (map[string]any
), then Go will implicitly initialize it with the appropriate "nil" value for that type (which, in this case, would bemap[string]any{}
).Note that this "nil" value is not the same as
nil
(for a map), and I believe that it is not the same as what is returned bymake(map[string]any)
(which is a map with no keys -- an empty map -- not a nil map). However, the nil map, as Dave indicated elsewhere, should behave the same as an empty map for read accesses, and it should be more efficient to instantiate and handle.In any case, it would be good to combine lines 123 and line 125 and declare and initialize the variable in a single statement. It's just a happy coincidence that you can probably omit the explicit value from that initialization. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't seem to find the right way to do this. Everything I try results in the IDE complaining somewhere. It makes sense to me to simply declare the var up front as
var configData map[string]any
, but that breaks theloadYamlFile()
function call at line 134.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a CLI, so "efficiency" is of somewhat limited use. But I'm really confused if the
loadYamlFile
is complaining, since it's overwriting the current value ofconfigData
, and shouldn't care. Isn't it??? 😕 What's the IDE complaint?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/me sighs.
loadYamlFile()
is a wrapper aroundyaml.Unmarshal()
which takes an output parameter ofout interface{}
(akaany
), because, more or less, it can return anything (or, at least, any value that you could encode in YAML).So, somewhere along the line, we need to convert the output value from type
any
to the type that we expect to get based on what we know about the input that we supplied.I suppose that that point is in this code after the call to
loadYamlFile()
.So, we need something like this:
This will "convert" the value in
configDataRaw
from anany
type to the mapping type; if the conversion fails, it reports the error and exits.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh. So GitHub only finds this one caller to the unexported
loadYamlFile
. Are there more? Is there any conceivable reason it should allow returning anything other than a map?The actual
yaml.Unmarshall
reacts to the type of the output pointer, so is the only problem here thatloadYamlFile
blindly tries to force anany
return type?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only caller I found, but, given the name of
loadYamlFile
, it seems like it should have a more generic interface than forcing it to return a map (since "a file" could have lists or strings or things other than mappings in it).yaml.Unmarshall()
accepts anany
parameter, so it is up to its caller to decide what the type should be (butUnmarshall()
does a compatibility check against the data). The problem is thatloadYamlFile()
is a blind intermediary between the "real caller" andUnmarshall()
so it cannot return anything but anany
(unless we make it "opinionated", which I'm not really inclined to do) because it has no idea what the caller expects.So, we're left with having the caller apply a type assertion...which, perhaps, is not that big of a deal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webbnh Your code suggestion does not work as-is (
ok
is undefined). At this point, I think the existing solution is how we should move forward. I feel like we are trying to untangle something that amounts to an annoyance with significantly more code and effort.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code as I presented it above would result in a compiler error (
undefined: ok
) if you took it literally as-is, because the variableok
is never declared.But, it probably would have worked if you had added
var ok bool
or if you had used:=
to assign the result of the type assertion (I didn't supply those, because it wasn't clear which was better or where to put the declaration)...but, see below.What we're trying to do is to get back to the safety of using strongly-typed variables. But, now that I look at it, that appears to be awkward at best: this code takes the value returned by
loadYamlFile()
(which is anany
type, hopefully one containing a map value) and passes it toconfig.Load()
which just forwards it togetConfigSchema().UnserializeType()
which is a generic function that coerces its return value to the type that its caller wants (which appears to be*Config
).So, while the type should be a
map[string]any
(e.g., because that is whatgetConfigSchema().UnserializeType()
needs in order to produce a*Config
), the value inconfigData
needs to be compatible with whatloadYamlFile()
returns, which is anany
...so, we're kind of trapped by circumstance. That is,loadYamlFile()
is conspiring withconfig.Load()
to produce a reasonable result, and we unfortunately landed in between them.Actually, it was one type assertion and an error handler for it. (Hopefully, it will turn out that that error handler is redundant with the one for
config.Load()
.)But, if you're interested in reducing the code, I think that the separate initialization of
configData
at line 125 is superfluous: you could have done that on the declaration at line 123, and, as we pointed out, you could use the "zero value" for the map type.