News for hackers. Stuff that matters.
Re:The Serde Rust Framework by the_gipsy on Thursday October 14, 2021 @06:56PM (#28871318)
Isn’t it insane to change your types to accommodate this? You have to change all fields accesses, it behaves differently, you lose non-nullability…
Re:The Serde Rust Framework by autarch on Thursday October 14, 2021 @07:02PM (#28871384)
> Isn’t it insane to change your types to accommodate this?

Welcome to Go! This was a real pain for us at my last job, where we used go-swagger to generate REST API code. All the generated structs had pointers for _non-optional_ fields so it could also check that they were populated. Meanwhile, optional fields were not pointers because they could use the default value.

So then we ended up with lots of code doing checks for `is X nil` all over the place. Many of those wouldn't be needed in normal usage, since a nil value would be rejected by the validator earlier, but we also sometimes needed to make these structs by hand and pass them around.

All of this is necessary because of the lack of generics. With generics you can solve this with things like an `Option<T>` type, like Rust does.

Re:The Serde Rust Framework by the_gipsy on Friday October 15, 2021 @09:51AM (#28877680)
Yea exactly, pointers id allowing nils, which I don’t want to conflate with omitting either.

In the end what I did is parse the JSON-string twice, once more with a “checkjson” library that tells me about missing fields.

Re:The Serde Rust Framework by bpicolo on Thursday October 14, 2021 @08:38PM (#28872334)
Pretty excited for Go 1.18 for that reason! Will likely take some time for the ecosystem to shuffle out good solutions for different bits, though
Re:The Serde Rust Framework by shp0ngle on Friday October 15, 2021 @01:20AM (#28874582)
Well that's go for you.

Go is simple and "default case" which is simple and works for most cases... and then you have edge cases where you need to bend over backwards.

As an exercise, try to use comma (",") in JSON field name.

Re:The Serde Rust Framework by bpicolo on Thursday October 14, 2021 @08:36PM (#28872317)
Don't you do that in rust too? You'd be working with Option<T> instead of T.
Re:The Serde Rust Framework by couchand on Thursday October 14, 2021 @09:16PM (#28872682)
The critical difference is that an Option<T> is not a T, but a nil pointer is still a pointer.

The stylistic difference is that the Rust type is semantic: your optional values are Options. The Go type is silly: why does being a pointer (which is allowed to be nil) imply required?

Re:The Serde Rust Framework by mcintyre1994 on Friday October 15, 2021 @03:37AM (#28875345)
Option<Int> is a really good way to describe a field that if present is an int, but may not be present, though. Pointer isn't.
Re:The Serde Rust Framework by ironmagma on Thursday October 14, 2021 @09:57PM (#28873122)
I don't think so. Speaking as a Rust dev, if you want to do anything slightly different from how things currently work, you need to change the types, which can be a major hassle.