Replies: 1 comment 3 replies
-
Give func (p *PositiveNumber[T]) Decode(ctx *kong.DecodeContext) error {
return ctx.Scan.PopValueInto("number", &p.Value)
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I want to define a
PositiveNumber[T]
type that can be set to any number typeT
and validates the the input to be positive. Since Go doesn't allow you to use a type variable as the RHS in a type statement (i.e.type PositiveNumber[T any] T
), I have to create a struct with the actual value as a field:Now I'd like to be able to use this in a struct to be populated by Kong:
However this obviously won't work out of the box:
So what I want to do is tell Kong to decode the value into
PositiveNumber.Value
instead. I could implementencoding.TextUnmarshaler
, but that would mean having to handle every possible typeT
manually. If possible I would like to leverage the existing decoders that Kong already provides.I did come up with this solution, but it's pretty jank and I was wondering if there's a better way:
Beta Was this translation helpful? Give feedback.
All reactions