4 ms·
One technique I use is to create a struct that mirrors the layout of the target struct, then just cast between them. So if you have a type Person struct {
by tynorf 7y ago
One technique I use is to create a struct that mirrors the layout of the target struct, then just cast between them. So if you have a
type Person struct {
FirstName string `json:"first_name"`
}
And you get json like `{"name": "Alice"}`, you can decode it something like this:
var doc struct {
FirstName string `json:"name"`
}
json.Unmarshal(data, &doc)
person := Person(doc)
I have not looked at the assembly so I can't say whether this incurs extra overhead or not.
- seiferteric 7y agoI thought of this, but I would have to do this for every single protobuf message. Also, anytime the protobuf changes, you would have to update the code.
- tynorf 7y agoYour protobuf messages are changing that much? My condolences. ETA: Also, you already are needing to add code to every encode/decode point for the `map[string]interface{}` handling. :P