Help us shape the future of Go! Take the 2026 Go Developer Survey

fields

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package fields resolves JSON-v2 field visibility and reflection paths. It does not compile validation rules or mutate application data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EscapePointer

func EscapePointer(segment string) string

EscapePointer escapes a segment per RFC 6901.

func Marshals

func Marshals(typ reflect.Type) bool

Marshals reports whether the type, or a pointer to it, customizes its JSON encoding. Such types are leaves: their fields are never promoted.

func Options

func Options(options string) []string

Options splits the option suffix of a JSON-v2 field tag without interpreting commas inside single-quoted format strings as option boundaries. Backslash escapes protect the next character inside a quoted string. This only locates boundaries; the JSON encoder still owns tag validity and format interpretation. An empty suffix produces one empty, harmless option.

Types

type Field

type Field struct {
	// GoName is the Go field selector accepted during registration.
	GoName string
	// JSONName is the encoded field name used in JSON Pointers and schema
	// properties.
	JSONName string
	// Index is the reflection path through promoted fields to the selected value.
	Index []int
	// Type is the exact selected field type, including named-type identity.
	Type reflect.Type
	// contains filtered or unexported fields
}

Field describes one JSON-visible Go field selected by discovery. Its Go name selects registrations; its JSON name determines error pointers.

func (*Field) Value

func (field *Field) Value(object reflect.Value) reflect.Value

Value resolves the compiled field path without allocating. A nil embedded or explicit intermediate pointer returns an invalid Value, preserving the distinction between an unreachable target and a real scalar zero.

type Struct

type Struct struct {
	// Fields lists selected JSON-visible fields in Go declaration order.
	Fields []*Field
	// ByGo indexes selected fields by their Go selector for registration lookup.
	ByGo map[string]*Field
	// Rejected maps unavailable selectors to the reason registration must reject
	// them.
	Rejected map[string]string
}

Struct is the resolved field table of a struct type.

func Inspect

func Inspect(typ reflect.Type) *Struct

Inspect resolves the JSON names of the fields of a struct type following the encoding/json/v2 rules. The caller caches the result.

func (*Struct) Lookup

func (info *Struct) Lookup(typ reflect.Type, name string) (*Field, bool)

Lookup resolves a Go selector that is either a JSON-visible terminal field or an untagged embedded struct on an explicit path. Embedded containers have no JSON segment: their selected children retain their flattened JSON names. FieldByName enforces Go ambiguity and shortest-depth selection independently of JSON dominance, including selectors through unexported value embeddings.

Jump to

Keyboard shortcuts

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