unknownfields

package
v0.46.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 9 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func Marshal(known any, extra map[string]json.RawMessage) ([]byte, error)

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

func Unmarshal(data []byte, known any) (map[string]json.RawMessage, error)

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

func YAML(known any, extra map[string]json.RawMessage) (any, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL