-
Notifications
You must be signed in to change notification settings - Fork 5
Relax the enum type constraint #12
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
Conversation
This is a POC that it's possible to relax the `constraint.Integer` constraint into simply `constraint.Ordered`. This means in particular that string enum can be accepted, which I found to be a common case. It seems to work, but I may not have covered all the cases properly. Signed-off-by: Michael Muré <[email protected]>
e4cc308 to
d64c71d
Compare
|
Thank you very much for catching this and contributing this improvement! I will later cross-check just in case, but this is definitely good to have. How to you use it with strings? I'm wondering if defining our own constraint type from basically |
|
Really like you would expect: type Sample string
const (
SampleInuseSpace Sample = "inuse_space"
SampleInuseObjects = "inuse_objects"
SampleAllocSpace = "alloc_space"
SampleAllocObjects = "alloc_objects"
)
// ------
var allocSampleIds = map[engine.Sample][]string{
engine.SampleInuseSpace: {"inuse_space"},
engine.SampleInuseObjects: {"inuse_objects"},
engine.SampleAllocSpace: {"alloc_space"},
engine.SampleAllocObjects: {"alloc_objects"},
}
var allocSampleHelp = map[engine.Sample]string{
engine.SampleInuseSpace: "Memory in use (space)",
engine.SampleInuseObjects: "Memory in use (objects)",
engine.SampleAllocSpace: "Memory allocated (space)",
engine.SampleAllocObjects: "Memory allocated (objects)",
}
sampleEnum := enumflag.New(&options.sample, "sample", allocSampleIds, enumflag.EnumCaseInsensitive)
flags.VarP(sampleEnum, "sample", "s", "Memory sample to analyse")
err := sampleEnum.RegisterCompletion(cmd, "sample", allocSampleHelp)Btw, it would be really nice to have a function to generate the description of possible values (something like As for the constraint, I don't think it makes sense to define a custom one. As I understand, your only imperative is |
|
Sorry for the silence; I've now checked with what the stdlib |
|
Ha yeah, if it does work with |
|
Thank you very much for your contribution! |
|
Thank you for maintaining it :-) |
|
released as v2.1.0; I've finally went for |
This is a POC that it's possible to relax the
constraint.Integerconstraint into simplyconstraint.Ordered. This means in particular that string enum can be accepted, which I found to be a common case.It seems to work, but I may not have covered all the cases properly.