json

package
v0.6.17 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package json provides JSON-related utilities for ToolHive.

This package extends Go's standard json package with types that work seamlessly with both Kubernetes CRDs and CLI YAML configurations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Any

type Any = Data[any]

Any is a type alias for Data[any], storing arbitrary JSON values. This is the most flexible type, suitable when the JSON structure is unknown.

+kubebuilder:pruning:PreserveUnknownFields +kubebuilder:validation:Type=object

func FromRawExtension

func FromRawExtension(ext runtime.RawExtension) (Any, error)

FromRawExtension creates an Any from runtime.RawExtension. Returns an error if the JSON cannot be unmarshaled.

func MustParse

func MustParse(jsonStr string) Any

MustParse parses a JSON string into an Any. This is a convenience function for tests. Panics if parsing fails.

func NewAny

func NewAny(v any) Any

NewAny creates an Any (Data[any]) from a value. This is a convenience function for tests and programmatic use.

type Data

type Data[T any] struct {
	// Value holds the typed Go value.
	Value T `json:"-" yaml:"-"`
}

Data stores JSON-compatible data of type T. It supports both JSON and YAML marshaling/unmarshaling, making it suitable for use in both Kubernetes CRDs and CLI YAML configurations.

The Value field stores the Go value directly, which simplifies usage in tests and when working with the data programmatically.

Common instantiations:

  • Data[any] (aliased as Any) for arbitrary JSON values
  • Data[map[string]any] (aliased as Map) for JSON objects

+kubebuilder:pruning:PreserveUnknownFields +kubebuilder:validation:Type=object

func NewData

func NewData[T any](v T) Data[T]

NewData creates a Data[T] from a value.

func (*Data[T]) DeepCopy

func (d *Data[T]) DeepCopy() *Data[T]

DeepCopy creates a deep copy. Required for controller-gen.

func (*Data[T]) DeepCopyInto

func (d *Data[T]) DeepCopyInto(out *Data[T])

DeepCopyInto copies the receiver into out. Required for controller-gen.

func (Data[T]) Get

func (d Data[T]) Get() T

Get returns the stored value.

func (Data[T]) IsEmpty

func (d Data[T]) IsEmpty() bool

IsEmpty returns true if the value is nil or empty. For maps and slices, it checks if the length is 0.

func (Data[T]) MarshalJSON

func (d Data[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (Data[T]) MarshalYAML

func (d Data[T]) MarshalYAML() (interface{}, error)

MarshalYAML implements yaml.Marshaler.

func (Data[T]) ToAny

func (d Data[T]) ToAny() (any, error)

ToAny returns the data as any type. This is useful when you need to pass the value to functions expecting any.

func (Data[T]) ToMap

func (d Data[T]) ToMap() (map[string]any, error)

ToMap returns the data as a map[string]any. This is a convenience method for Any types. Returns nil if there is no data or if the data is not a map.

func (*Data[T]) UnmarshalJSON

func (d *Data[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler.

func (*Data[T]) UnmarshalYAML

func (d *Data[T]) UnmarshalYAML(node *yaml.Node) error

UnmarshalYAML implements yaml.Unmarshaler.

type Map

type Map = Data[map[string]any]

Map is a type alias for Data[map[string]any], storing JSON objects. Use this when you know the data will always be a JSON object (not array, string, etc.).

+kubebuilder:pruning:PreserveUnknownFields +kubebuilder:validation:Type=object

func MapFromRawExtension

func MapFromRawExtension(ext runtime.RawExtension) (Map, error)

MapFromRawExtension creates a Map from runtime.RawExtension. Returns an error if the JSON cannot be unmarshaled.

func NewMap

func NewMap(m map[string]any) Map

NewMap creates a Map (Data[map[string]any]) from a map.

Jump to

Keyboard shortcuts

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