-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Modify optional flag to use generics #14645
Conversation
Review ChecklistHello reviewers! 👋 Please follow this checklist when reviewing this Pull Request. General
Tests
Documentation
New flags
If a workflow is added or modified:
Backward compatibility
|
Hello @Its-Maniaco thank you for this first contribution! The DCO check is failing, can you please fix it using:
From https://github.com/vitessio/vitess/pull/14645/checks?check_run_id=19177278801 |
go/flagutil/optional.go
Outdated
type OptionalType struct { | ||
val any | ||
set bool | ||
} |
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 this should just be
type OptionalType struct { | |
val any | |
set bool | |
} | |
type Optional[T any] struct { | |
val T | |
set bool | |
} |
and then rewriting the rest to match this type definition
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.
func (t OptionalType[any]) Set(arg string) error {
tType := t.String()
switch tType {
case "float64":
var temp interface{}
temp, err := strconv.ParseFloat(arg, 64)
if err != nil {
return numError(err)
}
t.val = temp.(any)
case "string":
var temp interface{}
temp = arg
t.val = temp.(any)
}
t.set = true
return nil
}
I was having trouble directly setting t.val
because of type mismatch. So I came up with above workaround.
Please let me know if this is the correct approach.
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 on the right track. i think you should be able to do just this, for example:
switch tType {
case "float64":
temp, err := strconv.ParseFloat(...)
if err != nil { ... }
t.val = temp.(any)
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 tried that earlier but was given InvalidAssert.
invalid operation: temp (variable of type float64) is not an interface
Same error when I did:
t.val = arg.(any)
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.
ah then i guess you still need the declaration (but you should be able to declare as any
rather than the older interface{}
)
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.
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 don't think from the code you've shown that you have the right definition for Optional
. does it match what i suggested here?
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.
this approach seems to work: https://go.dev/play/p/RrQxZeRXD2a
e612bfb
to
a97a2f0
Compare
Signed-off-by: Divya Pamecha <[email protected]> Signed-off-by: Divya Pamecha <[email protected]>
a97a2f0
to
5271832
Compare
Co-authored-by: Andrew Mason <[email protected]> Signed-off-by: Maniaco <[email protected]>
This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:
If no action is taken within 7 days, this PR will be closed. |
This PR is being marked as stale because it has been open for 30 days with no activity. To rectify, you may do any of the following:
If no action is taken within 7 days, this PR will be closed. |
This PR was closed because it has been stale for 7 days with no activity. |
Description
Fixes #11154
Checklist