-
Notifications
You must be signed in to change notification settings - Fork 15
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
Parse into other integer types (currently only 'int'?) #78
Comments
Yes. Generally in Go it is recommended to just use the int type for everything unless you have code that is performance sensitive or you explicitly know that you need that type (i.e you are sure you need int with 64 bits to avoid overflow). Therefore, although we may implement other int types in the future it is currently not a priority. And I'm not even saying we will implement other int types. My current thinking on this is that I would be open to it as long as it only imposes a small maintenance burden. But I might change my mind either way as I haven't really thought about it much. For now, my recommendation is that you use int, the int type is great as go will choose either int32 or int64 depending on the compile target for optimal performance. If you really need uints you are free to look into what adding them would look like and I'm happy to discuss it here. |
Hey @mac-hel Just thought I should let you know that I have thought about it and found a way that makes sense at least for the moment. In the future I might want to add more direct support for other number types but for now I have pushed float32, float64, int, int32 and int64. Additionally you can just build your own schema since the number schema is just a generic. Here is an example of doing that: schema1 := (&NumberSchema[uint64]{}).Required().LTE(10)
// You can also put it in a function which is what Zog does:
func Uint64(..) *z.NumberSchema[uint64] {
return &z.NumberSchema[uint64]{}
}
schema2 := Uint64().LTE(10) If you are using parse. You may need to write your own coercion function for these weird types since zog won't have one built in. But this is quite easy, I recommend you checkout func Uint64(..) *z.NumberSchema[uint64] {
return &z.NumberSchema[uint64]{
Coercer: MyCustomCoercerFunc
}
} Let me know if the explanation is clear enough. I hope you can get what you need with this. edit: forgot to mention that you'll need v0.17.0 for this |
Lets say I have string that can potentially contain large integer value
I can do this:
But for unsigned and/or 64 bit integer it will not work:
Is there a way to parse into other int types?
The text was updated successfully, but these errors were encountered: