Skip to content
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

Closed
mac-hel opened this issue Feb 9, 2025 · 2 comments
Closed

Parse into other integer types (currently only 'int'?) #78

mac-hel opened this issue Feb 9, 2025 · 2 comments

Comments

@mac-hel
Copy link

mac-hel commented Feb 9, 2025

Lets say I have string that can potentially contain large integer value

I can do this:

var val int
_ = z.Int().Required().GT(0).Parse("123", &val)

But for unsigned and/or 64 bit integer it will not work:

var val uint64
_ = z.Int().Required().GT(0).Parse("9223372036854775807", &val)   // error: cannot use &val (value of type *uint64) as *int value

Is there a way to parse into other int types?

@Oudwins
Copy link
Owner

Oudwins commented Feb 10, 2025

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.

@Oudwins
Copy link
Owner

Oudwins commented Feb 22, 2025

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 /conf/coercers.go for inspiration. Then you just have to provide that coercer to the schema like this:

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

@Oudwins Oudwins closed this as completed Feb 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants