How to figure out if a flag's value was provided by the user or is the default #214
Unanswered
josephschmitt
asked this question in
Q&A
Replies: 1 comment 2 replies
-
This is by design, so the behaviour won't be changing, but I think a helper function |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a simple command-line wrapper around an API endpoint. One of my commands is an
update
command which takes a few flags, such as--name
,--description
, etc.Ideally, I'd like for that update command to only mutate the values that were changed, and not touch anything else. However, kong only lets flags be values and not pointers. This means that if I run my update command without any flags, it'll populate the
Name
andDescription
struct fields with their default values of""
, and my code has no real easy way to determine if the user intention was to not update that field, or if they want to wipe that field out.Is there an easy-ish way to determine if a value was set because it was defaulted vs because the user explicitly set it to be that value?
My ideal use case would be:
However the way it works right now, excluding either flag sets that value to be an empty string. If I try this now, running
$ cli update --name "My cool name"
Will produce a go struct of the following
And then calling my update API endpoint will now wipe out my description, which was not the user intent. If the user wanted to wipe out the description, I would expect them to have to do
The way I'd normally do this is by making these struct fields pointers, with a
nil
pointer meaning the value was never set, but kong doesn't allow flags to be pointers.Beta Was this translation helpful? Give feedback.
All reactions