Documentation
¶
Overview ¶
Package unknownfields lets a typed struct carry the JSON object members it does not model through a decode/encode round trip.
dtctl's typed resource structs are read-modify-write views: a command reads a document, changes one field and sends the whole document back. Any member the struct has no field for would otherwise be dropped from that update — silently resetting it on the server, or, for a never-modifiable property, getting the update rejected. Keeping unknown members in an Extra map lets a schema grow without a dtctl release.
A type opts in with an `Extra map[string]json.RawMessage` field tagged `json:"-"` and two methods that delegate here through a method-less alias:
func (c *Config) UnmarshalJSON(data []byte) error {
type plain Config
extra, err := unknownfields.Unmarshal(data, (*plain)(c))
if err != nil {
return err
}
c.Extra = extra
return nil
}
func (c Config) MarshalJSON() ([]byte, error) {
type plain Config
return unknownfields.Marshal(plain(c), c.Extra)
}
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal encodes known, which must be a struct (or pointer to one) without its own MarshalJSON, and appends the extra members after the modelled ones in key order. An extra member whose name matches a modelled field is skipped: the typed field is the source of truth for anything dtctl models.
func Unmarshal ¶
Unmarshal decodes the JSON object in data into known, which must be a pointer to a struct without its own UnmarshalJSON (use a method-less alias), and returns the members no field of that struct consumes. A member is "consumed" under the same case-insensitive name matching encoding/json applies, so a key the typed field already absorbed is never kept twice.
A JSON null is a no-op, as encoding/json expects from an Unmarshaler. The returned map is nil when every member is modelled.
func YAML ¶
YAML renders known (a method-less alias of the struct) the way yaml.v3 reflection always has — same keys, order and empty values — and appends the extra members under their original API names, in key order, with numbers kept exactly as the server sent them. It is the MarshalYAML counterpart of Marshal, so `get -o yaml` → `apply -f` keeps unmodelled members: apply matches the reflected lowercase keys case-insensitively and reads the appended ones back into Extra.
TEMPORARY: this exists only because `-o yaml` still prints reflected lowercase field names, which stable commands promise to keep. Once YAML output uses JSON field names everywhere (centrally, in the printer), Extra is emitted by MarshalJSON alone, and this function and every MarshalYAML that calls it should be deleted.
Types ¶
This section is empty.