Documentation
¶
Overview ¶
Package structfields reads the fields a Go struct type shows to the codec: the name each is written under, its `yaml` flags, and the fields an embedded or `,inline` struct promotes.
Mode selects the tags read: `yaml` alone, as go.yaml.in/yaml/v3 does, or with encoding/json's rules added for github.com/go-openapi/go-yaml/codec.UseJSONTags and github.com/go-openapi/go-yaml/codec.UseInferredNames.
Of returns a type's Fields: each field by Go name, the field a mapping key names, and the fields its embedded structs promote. MapOf returns the fields by Go name alone. Both read a type once per mode and keep the result in a Cache.
The codec calls SelfDecoding once, at init, with its unmarshaler interfaces. An `,inline` field whose type implements one decodes its own node; see InlineReadsItsOwnNode.
Index ¶
- func Ignored(field reflect.StructField, mode Mode) bool
- func InlineReadsItsOwnNode(t reflect.Type) bool
- func InlineTakesUnclaimedEntries(t reflect.Type) bool
- func SelfDecoding(interfaces ...reflect.Type)
- func StringTakesNumber(t reflect.Type) bool
- type Cache
- type Fields
- type Flat
- type Info
- type InfoMap
- type Mode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Ignored ¶
func Ignored(field reflect.StructField, mode Mode) bool
Ignored reports whether the codec skips field: an unexported field that is not embedded, or a field whose tag under mode is "-".
func InlineReadsItsOwnNode ¶
InlineReadsItsOwnNode reports whether a `,inline` field of type t decodes the whole mapping itself.
It does when t implements one of the unmarshalers passed to SelfDecoding. Such a field gets no entry in Fields.flat, and the decoder's decodeInlineFields passes it the whole mapping. The decoder fills the other fields of the struct from the same mapping, as go.yaml.in/yaml/v3 does.
func InlineTakesUnclaimedEntries ¶
InlineTakesUnclaimedEntries reports whether a `,inline` field of type t collects the entries no other field of the struct claims. Only a map does.
go.yaml.in/yaml/v3 fills such a map with the keys its FieldsMap does not name, and leaves it nil where every key is named. Fields.flat holds the same set here: the struct's own fields, and everything its embedded structs promote.
func SelfDecoding ¶
SelfDecoding sets the interfaces through which a type decodes the node it is handed, the decoder's unmarshalers. A `,inline` field whose type implements one reads the mapping itself, and its fields are not promoted.
The decoder calls it once, at init, before any type is read: the fields read for a type are cached, so a later call would not change them.
func StringTakesNumber ¶
StringTakesNumber reports whether `json:",string"` applies to a field of type t: a number, or a pointer to one at any depth, as in encoding/json/v2. v2 also rejects the option on a bool or a string, which v1 accepted.
Types ¶
type Cache ¶
type Cache[V any] struct { // contains filtered or unexported fields }
Cache maps a reflect.Type to a V over a sync.Map, and is safe for concurrent use. It holds the type assertion that its callers would otherwise repeat.
func (*Cache[V]) LoadOrStore ¶
LoadOrStore returns the value already stored for t if there is one, and otherwise stores and returns v.
type Fields ¶
type Fields struct {
// ByGoName holds every field, keyed by Go field name.
ByGoName InfoMap
// Inline lists the embedded fields, which the tree decoder reads after the
// rest.
Inline []*Info
// contains filtered or unexported fields
}
Fields holds one type's fields, or the error raised by its tags. Both are kept, so a type with a duplicated field name is refused as fast as one that reads.
func Of ¶
Of returns everything read from structType: the fields by Go name, by the name a mapping key writes, and the embedded ones on their own.
func (*Fields) Lookup ¶
Lookup finds the field a mapping key names.
A key matches a name exactly or matches nothing, in every mode, as in go.yaml.in/yaml/v3 and encoding/json/v2. The result's At is nil where the field is in the type itself and there is no index path to walk.
func (*Fields) RenderNameOf ¶
RenderNameOf returns the name a mapping key writes for the Go field called fieldName, following the embedded structs.
A validator names a rejected field by its Go name. The entry that wrote the field is found by the name used in the document. A promoted field is missing from ByGoName, which holds only the type's own fields, so RenderNameOf reads flat too.
func (*Fields) WritesPromoted ¶
WritesPromoted reports whether an entry encoded by the embedded struct at index belongs in the mapping around it.
flat holds the one field each name reaches. A name that flat places under another index is already written by a shallower field. A name in dropped reaches no field, because the conflict rule picked none. Writing either name would put a key in the document that a decode then ignores or rejects.
A name in neither map is a key no struct field declares, such as one from an embedded struct's own `,inline` map. WritesPromoted returns true for it.
type Flat ¶
Flat describes the field reached by a mapping key.
It holds the field's location At, its tag information SF, and an ordinal index Ord. The ordinal index Ord starts from zero, and numbers the key names of the type.
The tree decoder uses Ord as a bit position. It sets one bit for each field written by a mapping, to tell an own entry from a later "<<" merge. An index path cannot serve as a bit position. Neither can a leaf's own Index, because two embedded structs can both have a field at index zero.
type Info ¶
type Info struct {
// Index gives the field's position in the struct, for reflect.Value.Field.
// FieldByName walks the type's fields and compares names on every call.
Index int
FieldName string
RenderName string
AnchorName string
AliasName string
IsAutoAnchor bool
IsAutoAlias bool
IsOmitEmpty bool
IsOmitZero bool
// IsJSONTag records that the field's tag is a `json` tag, so its flags follow encoding/json/v2.
// `json:",omitempty"`, and the OmitEmpty encode option on such a field, drop the field when it encodes as null,
// an empty string, an empty mapping or an empty sequence.
IsJSONTag bool
IsFlow bool
IsInline bool
// IsString records `json:",string"`: the field's number is written inside a string, as encoding/json/v2 writes it.
// Only a `json` tag sets it. [StringTakesNumber] tells whether the field's type allows it.
IsString bool
// contains filtered or unexported fields
}
Info holds what the struct tags set on one field of a struct: the name it is written under, and its flags.
The flags follow the `yaml` tag: `omitempty`, `omitzero`, `flow` and `inline`, then `anchor`, `anchor=name`, `alias` and `alias=name`, which set IsAutoAnchor, AnchorName, IsAutoAlias and AliasName.
func (*Info) OmitsEmptyEncoded ¶
OmitsEmptyEncoded reports whether the encoder decides omitempty on sf from its encoded value, as encoding/json/v2 does: sf has a `json` tag, and either the tag carries `,omitempty` or the OmitEmpty option is set.
type InfoMap ¶
InfoMap holds the Info of each field of a struct, keyed by Go field name.
func MapOf ¶
MapOf returns the fields of structType, keyed by Go field name.
A type's fields never change, so MapOf reads them once and returns the same map afterwards. Reading them for every decoded value was the largest single cost of decoding into Go types. Decoding citm_catalog into its own structs allocated 73,708 times here per document, out of 243,679 allocations for the whole decode. Each value cost an InfoMap, a renderNameMap and an Info, and strings.Split ran 21,627 times on tags already split thousands of times.
Every decode and encode of the type shares the map and the Info values in it, so no caller may write to them.
func (InfoMap) HasMergeProperty ¶
HasMergeProperty reports whether a field is tagged `,omitempty,inline,alias`. The tree decoder then skips the "<<" key of the mapping it reads into the struct.
type Mode ¶
type Mode uint8
Mode selects which of encoding/json's rules a decode adds to the `yaml` tag, as a pair of independent bits.
The `yaml` tag is read in every mode and always wins. The two bits add the rules a `yaml` tag has no spelling for:
- encoding/json promotes the fields of an untagged embedded struct into the outer one. YAML needs an explicit `yaml:",inline"` for that.
- encoding/json names an untagged field after its Go name. go.yaml.in/yaml/v3 lowercases the name.
A type tagged for JSON alone has no inline marker anywhere. go-openapi/spec is one: every field has a `json:` tag. Read without encoding/json's embedding rule, such a type leaves its embedded structs empty.
const ( // YAMLTags reads `yaml` alone and inlines an embedded struct only where a // tag asks for it, as go.yaml.in/yaml/v3 does. The default, and the mode with // neither bit set. YAMLTags Mode = 0 // JSONTags reads `json` where a field carries no `yaml` tag, and promotes an // untagged embedded struct, as encoding/json does. [github.com/go-openapi/go-yaml/codec.UseJSONTags] sets it. JSONTags Mode = 1 << 0 // InferredNames names a field that no tag names after its Go name, // verbatim, where every other mode lowercases it. [github.com/go-openapi/go-yaml/codec.UseInferredNames] sets // it. InferredNames Mode = 1 << 1 // ModeCount sizes the per-mode caches, one for each pair of bits. ModeCount = (JSONTags | InferredNames) + 1 )