You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I you want the struct to (un)marshal with more than one encoder, then you typically need to add a struct tag for every encoder. Two, three, or more types of struct tags are fairly common, and they often all have the same value.
Most encoders don't support changing the struct tag they look at (and probably shouldn't, either).
All of this is noisy, annoying to maintain, and hard to read.
I propose adding a new generic name alias, which can be re-used by different encoders. this would be similar to the encoding.Text{Unmarshaler,Marshaler}. With that, the above would be:
type Bread struct {
Weight int `text:"weight"`
Slices int `text:"slices"`
WholeGrain bool `text:"whole_grain"`
SourDough bool `text:"sour_dough"`
Price int `text:"price"`
Ingredients []string `text:"ingredients,omitempty"`
}
Maybe text isn't the best name; but it does fit nicely with encoding.TextMarshaler / encoding.TextUnmarshaler. Alternatively it could be name, alias, or maybe something else.
The logic would be:
name, ok := lookupTag(field, "json") // Or yaml, xml, etc.
if !ok {
name, ok = lookupTag(field, "text")
}
if !ok {
name = field.Name()
}
That is:
Prefer marshaller-specific tag if it exists.
Fall back to the generic text if it doesn't.
And the field name if that also doesn't exist.
This allows setting reasonable defaults that should be fine for the common use case, and deviating from that if need be.
The only allowed option is omitempty (although that should be a vet check). Values with - will be skipped as it works currently.
Concretely, the change required would be to implement this in the encoding/json, encoding/xml, and database/sql packages, and maybe document it somewhere. The wider ecosystem of parsers (YAML, TOML, various "fast json" packages, ec.) can then adopt this.
This change should be fairly low-complexity throughout, and simplify the life of everyone wanting to deal with more than one encoding format.
The text was updated successfully, but these errors were encountered:
Proposal Details
It's not uncommon to see code like this:
I you want the struct to (un)marshal with more than one encoder, then you typically need to add a struct tag for every encoder. Two, three, or more types of struct tags are fairly common, and they often all have the same value.
Most encoders don't support changing the struct tag they look at (and probably shouldn't, either).
All of this is noisy, annoying to maintain, and hard to read.
I propose adding a new generic name alias, which can be re-used by different encoders. this would be similar to the
encoding.Text{Unmarshaler,Marshaler}
. With that, the above would be:Maybe
text
isn't the best name; but it does fit nicely withencoding.TextMarshaler
/encoding.TextUnmarshaler
. Alternatively it could bename
,alias
, or maybe something else.The logic would be:
That is:
text
if it doesn't.This allows setting reasonable defaults that should be fine for the common use case, and deviating from that if need be.
The only allowed option is
omitempty
(although that should be a vet check). Values with-
will be skipped as it works currently.Concretely, the change required would be to implement this in the encoding/json, encoding/xml, and database/sql packages, and maybe document it somewhere. The wider ecosystem of parsers (YAML, TOML, various "fast json" packages, ec.) can then adopt this.
This change should be fairly low-complexity throughout, and simplify the life of everyone wanting to deal with more than one encoding format.
The text was updated successfully, but these errors were encountered: