encoding

package
v40410.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package encoding provides utility for encoding/decoding IDL types to/from their JSON representation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsByte

func AsByte(v any) (byte, error)

AsByte asserts that v is a json.Number and parses it as a byte.

func AsEnum

func AsEnum[E Enum](v any) (E, error)

AsEnum asserts that v is a json.Number and parses it as Enum type E. For unknown values, a fallback is returned if E is a Fallbacker.

func AsFloat32

func AsFloat32(v any) (float32, error)

AsFloat32 asserts that v is a json.Number and parses it as a float32.

func AsFloat64

func AsFloat64(v any) (float64, error)

AsFloat64 asserts that v is a json.Number and parses it as a float64.

func AsInt

func AsInt(v any) (int, error)

AsInt asserts that v is a json.Number and parses it as an int.

func AsInt32

func AsInt32(v any) (int32, error)

AsInt32 asserts that v is a json.Number and parses it as an int32.

func AsInt64

func AsInt64(v any) (int64, error)

AsInt64 asserts that v is a json.Number and parses it as an int64.

func AsMapItems

func AsMapItems(v any) (items iter.Seq2[any, any], errFn func() error, length int)

AsMapItems asserts that v is a container slice of map items with "key" and "value". It returns an iterator over those key-value pairs that stops on the first error, a callback to retrieve this error, and the slice length.

func AsTime

func AsTime(v any) (time.Time, error)

AsTime asserts that v is a json.Number and parses it as a time.Time. The number is interpreted as seconds since the Unix epoch.

func AsTypeCode

func AsTypeCode(v any) (idl.TypeCode, error)

AsTypeCode asserts that v is a string and parses it as an idl.TypeCode.

func In

func In(key string, m map[string]any) error

In returns an error if the key is not in m.

func InAndIs

func InAndIs[T JSON](key string, m map[string]any) (T, error)

InAndIs checks that m contains a value for the key like In, and if so asserts that the value is of type T like Is.

func Is

func Is[T JSON](v any) (t T, err error)

Is asserts that v holds a value of JSON type T.

func NonNilSlice

func NonNilSlice[T SimpleEncoding](slice []T) []T

NonNilSlice returns the passed slice as is if non-nil. Else a new empty slice of same type is returned. This forces json.Marshal to encode it as a JSON array instead of as JSON null.

Types

type Enum

type Enum interface {
	~int
	IsKnown() bool
}

Enum describes an enumeration type.

type Fallbacker

type Fallbacker[E Enum] interface {
	Fallback() E
}

A Fallbacker has a known Enum value as fallback.

type JSON

type JSON interface {
	// true/false | string | array | object | number
	bool | string | []any | map[string]any | json.Number
}

JSON types are those which json.Decoder.Decode stores into an interface value when decoding JSON after json.Decoder.UseNumber was called.

The json.Decoder stores nil for JSON null.

type SimpleEncoding

type SimpleEncoding interface {
	// boolean | string | int | long | float | double | enumeration
	bool | string | int32 | int64 | float32 | float64 | ~int
}

SimpleEncoding types are those of which non-nil slices can be passed to json.Marshal without further encoding work.

Note that byte is not included, as json.Marshal has special handling for []byte.

Directories

Path Synopsis
Package object handles encoding/decoding of idl.Object types.
Package object handles encoding/decoding of idl.Object types.
Package valobj handles encoding/decoding of idl.ValueObject types.
Package valobj handles encoding/decoding of idl.ValueObject types.

Jump to

Keyboard shortcuts

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