▲ | xnorswap a day ago | |||||||
As a .net programmer, the "stringly typed" nature of the metadata horrifies me, but the choices of Go have long confused me. So in .NET, like Java as you mention, we have attributes, . e.g.
etc.This is simple, and obvious. The JsonPropertyName attribute is an override, you can set naming policies for the whole class. camelCase by default, with kebab-case, snake_case etc as alternative defaults. C#/.NET of course has the benefit of having public properties, which are serialised by default, and private properties, which aren't, so you're unlikely to be exposing things you don't want to expose. This contrasts to Go's approach, much like python, of using casing convention to determine private vs public fields. ( Please correct me if I'm wrong on this? ) The first example still confuses me though, because either you want IsAdmin to come from the user, in which case you still want to deserialise it, or you don't, in which case it shouldn't even be in your DTO at all. Deserialisation there is a bit of a red-herring, as there should be a validation step which includes, "Does this user have the rights to create an admin?". The idea of having a user class, which gets directly updated using properties straight from deserialized user input, feels weird to me, but I'd probably be dismissed as an "enterprise programmer" who wants to put layers between everything. | ||||||||
▲ | hmry a day ago | parent | next [-] | |||||||
> This contrasts to Go's approach, much like python, of using casing convention to determine private vs public fields. ( Please correct me if I'm wrong on this? ) I think calling it a convention is misleading. In Python, you can access an _field just by writing obj._field. It's not enforced, only a note to the user that they shouldn't do that. But in Go, obj.field is a compiler error. Fields that start with a lowercase letter really are private, and this is enforced. So I think it's better to think of it as true private fields, just with a... unique syntax. | ||||||||
▲ | masklinn a day ago | parent | prev | next [-] | |||||||
> This contrasts to Go's approach, much like python, of using casing convention to determine private vs public fields. ( Please correct me if I'm wrong on this? ) Go actually ties visibility to casing, instead of using separate annotations. And it will not serialise private fields, only public. Python has no concept of visibility at all, conventionally you should not access attributes prefixed with `_` but it won't stop you. | ||||||||
| ||||||||
▲ | grey-area a day ago | parent | prev [-] | |||||||
Not just weird, it’s dangerous to do this - to easy to miss validation as fields are added over time. Better to explicitly validate all fields IMO. |