fieldpath

package
v1.14.4 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: Apache-2.0 Imports: 10 Imported by: 113

Documentation

Overview

Package fieldpath provides utilities for working with field paths.

Field paths reference a field within a Kubernetes object via a simple string. API conventions describe the syntax as "standard JavaScript syntax for accessing that field, assuming the JSON object was transformed into a JavaScript object, without the leading dot, such as metadata.name".

Valid examples:

* metadata.name * spec.containers[0].name * data[.config.yml] * metadata.annotations['crossplane.io/external-name'] * spec.items[0][8] * apiVersion * [42]

Invalid examples:

* .metadata.name - Leading period. * metadata..name - Double period. * metadata.name. - Trailing period. * spec.containers[] - Empty brackets. * spec.containers.[0].name - Period before open bracket.

https://github.com/kubernetes/community/blob/61f3d0/contributors/devel/sig-architecture/api-conventions.md#selecting-fields

Index

Constants

View Source
const DefaultMaxFieldPathIndex = 1024

DefaultMaxFieldPathIndex is the max allowed index in a field path.

Variables

This section is empty.

Functions

func IsNotFound added in v0.8.0

func IsNotFound(err error) bool

IsNotFound returns true if the supplied error indicates a field path was not found, for example because a field did not exist within an object or an index was out of bounds in an array.

Types

type Paved

type Paved struct {
	// contains filtered or unexported fields
}

A Paved JSON object supports getting and setting values by their field path.

func Pave

func Pave(object map[string]any, opts ...PavedOption) *Paved

Pave a JSON object, making it possible to get and set values by field path.

func PaveObject added in v0.8.0

func PaveObject(o runtime.Object, opts ...PavedOption) (*Paved, error)

PaveObject paves a runtime.Object, making it possible to get and set values by field path. o must be a non-nil pointer to an object.

func (*Paved) DeleteField added in v0.18.0

func (p *Paved) DeleteField(path string) error

DeleteField deletes the field from the object. If the path points to an entry in an array, the element on that index is removed and the next ones are pulled back. If it is a field on a map, the field is removed from the map.

func (*Paved) ExpandWildcards added in v0.16.0

func (p *Paved) ExpandWildcards(path string) ([]string, error)

ExpandWildcards expands wildcards for a given field path. It returns an array of field paths with expanded values. Please note that expanded paths depend on the input data which is paved.object.

Example:

For a Paved object with the following data: []byte(`{"spec":{"containers":[{"name":"cool", "image": "latest", "args": ["start", "now", "debug"]}]}}`), ExpandWildcards("spec.containers[*].args[*]") returns: []string{"spec.containers[0].args[0]", "spec.containers[0].args[1]", "spec.containers[0].args[2]"},

func (*Paved) GetBool

func (p *Paved) GetBool(path string) (bool, error)

GetBool value of the supplied field path.

func (*Paved) GetInteger added in v0.10.0

func (p *Paved) GetInteger(path string) (int64, error)

GetInteger value of the supplied field path.

func (*Paved) GetString

func (p *Paved) GetString(path string) (string, error)

GetString value of the supplied field path.

func (*Paved) GetStringArray

func (p *Paved) GetStringArray(path string) ([]string, error)

GetStringArray value of the supplied field path.

func (*Paved) GetStringObject

func (p *Paved) GetStringObject(path string) (map[string]string, error)

GetStringObject value of the supplied field path.

func (*Paved) GetValue

func (p *Paved) GetValue(path string) (any, error)

GetValue of the supplied field path.

func (*Paved) GetValueInto added in v0.8.0

func (p *Paved) GetValueInto(path string, out any) error

GetValueInto the supplied type.

func (Paved) MarshalJSON

func (p Paved) MarshalJSON() ([]byte, error)

MarshalJSON to the underlying object.

func (*Paved) MergeValue added in v0.15.0

func (p *Paved) MergeValue(path string, value any, mo *xpv1.MergeOptions) error

MergeValue of the receiver p at the specified field path with the supplied value according to supplied merge options

func (*Paved) SetBool

func (p *Paved) SetBool(path string, value bool) error

SetBool value at the supplied field path.

func (*Paved) SetNumber

func (p *Paved) SetNumber(path string, value float64) error

SetNumber value at the supplied field path.

func (*Paved) SetString

func (p *Paved) SetString(path, value string) error

SetString value at the supplied field path.

func (*Paved) SetUnstructuredContent

func (p *Paved) SetUnstructuredContent(content map[string]any)

SetUnstructuredContent sets the JSON serialisable content of this Paved.

func (*Paved) SetValue

func (p *Paved) SetValue(path string, value any) error

SetValue at the supplied field path.

func (*Paved) UnmarshalJSON

func (p *Paved) UnmarshalJSON(data []byte) error

UnmarshalJSON from the underlying object.

func (*Paved) UnstructuredContent

func (p *Paved) UnstructuredContent() map[string]any

UnstructuredContent returns the JSON serialisable content of this Paved.

type PavedOption added in v0.16.1

type PavedOption func(paved *Paved)

PavedOption can be used to configure a Paved behavior.

func WithMaxFieldPathIndex added in v0.16.1

func WithMaxFieldPathIndex(max uint) PavedOption

WithMaxFieldPathIndex returns a PavedOption that sets the max allowed index for field paths, 0 means no limit.

type Segment

type Segment struct {
	Type  SegmentType
	Field string
	Index uint
}

A Segment of a field path.

func Field

func Field(s string) Segment

Field produces a new segment from the supplied string. The segment is always considered to be an object field name.

func FieldOrIndex

func FieldOrIndex(s string) Segment

FieldOrIndex produces a new segment from the supplied string. The segment is considered to be an array index if the string can be interpreted as an unsigned 32 bit integer. Anything else is interpreted as an object field name.

type SegmentType

type SegmentType int

A SegmentType within a field path; either a field within an object, or an index within an array.

const (
	SegmentField SegmentType
	SegmentIndex
)

Segment types.

type Segments

type Segments []Segment

Segments of a field path.

func Parse

func Parse(path string) (Segments, error)

Parse the supplied path into a slice of Segments.

func (Segments) String

func (sg Segments) String() string

Jump to

Keyboard shortcuts

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