value

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package value is the data model that documents, settings and matchers share: how JSON is read, how text reads as a bool or a number, and how values compare.

Values fall into classes: nil, bools, text (strings and byte slices), numbers (any Go int, uint or float), lists (other slices and arrays), objects (maps with string keys) and other values, such as structs. Values of one class compare by value. Across classes, only two readings happen, the ones text from a request, an environment variable or a flag needs: text compared with a number reads as a number, with ParseNumber, and anything compared with a bool reads as a bool, with Bool.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v any) (b, ok bool)

Bool reads v as a bool the way strconv.ParseBool reads text: text as written, with no trimming; the numbers 1 and 0, as "1" and "0"; and a bool as itself. Nil, lists, objects and other values aren't bools.

func Compare

func Compare(a, b any) (int, bool)

Compare orders a and b, each a number or text that ParseNumber reads, by their exact values: -1 when a is less than b, 0 when they are equal and +1 when a is greater. ok is false for other values, and NaN.

func DecodeJSON

func DecodeJSON(data []byte) (any, error)

DecodeJSON reads a JSON document the way documents are read: objects are map[string]any, lists []any, and a number is an int64 when its value is whole and fits, with all its digits, and a float64 otherwise. Duplicate names, invalid UTF-8 and numbers beyond float64 are errors; syntax errors are *jsontext.SyntacticError.

func DecodeJSONLenient

func DecodeJSONLenient(data []byte) (any, error)

DecodeJSONLenient reads a JSON document as DecodeJSON does, but takes what a server may be sent: the last of duplicate names wins, invalid UTF-8 becomes U+FFFD and a number beyond float64 is ±Inf.

func Equal

func Equal(a, b any) bool

Equal reports whether a and b are equal.

Values of one class compare by value: bools and text as they are, numbers by their exact value whatever their Go types, lists item by item and in order, objects member by member, and other values with reflect.DeepEqual. Across classes, text compared with a number reads as one with ParseNumber, and anything compared with a bool reads as one with Bool: "10" equals 10, and "t", 1 and true are equal. Nothing else is equal across classes.

Equal is symmetric, but not transitive: "1.0" and "1" both equal 1, yet not each other.

func Len

func Len(v any) (int, bool)

Len returns the length of v: the bytes of text, the items of a list or the members of an object. ok is false for other values.

func List

func List(v any) ([]any, bool)

List returns the items of v, a slice or an array other than a byte slice, which is text.

func Number

func Number(v any) (any, bool)

Number returns v, a Go number of any int, uint or float kind, named types included, with its exact value: as an int64, as a uint64 when above math.MaxInt64, or as a float64. Other values aren't numbers.

func Object

func Object(v any) (map[string]any, bool)

Object returns the members of v, a map whose keys are strings, or of a named string type.

func ParseInteger

func ParseInteger(s string) (int64, bool)

ParseInteger reads s, a decimal number, when its value is a whole number that fits an int64, with every digit: read as a float64 first, 9.007199254740993e15 would be 9007199254740992. It takes the JSON number grammar, and what YAML floats add to it: a leading + or zeros, and a point with no digits on one side, as in .5e1 or 5. ok is false for other text, and for numbers that aren't whole or don't fit.

func ParseNumber

func ParseNumber(s string) (any, bool)

ParseNumber reads text written in the JSON number grammar, -?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][+-]?[0-9]+)?, the way documents read numbers: an integer that fits an int64 keeps every digit, however it is written, so 9007199254740993, 9007199254740993.0 and 9.007199254740993e15 are one number; any other number is the nearest float64, kept as an int64 when that is whole. Other text, such as +1, 01, .5, 1_000, Inf or text with spaces, isn't a number, and neither are numbers beyond float64, such as 1e400.

func Same

func Same(a, b any) bool

Same reports whether a and b are equal without reading text as a number or anything as a bool: they must be of one class. Numbers still compare by their exact value, and lists and objects by their items, whatever their Go types.

func Text

func Text(v any) (string, bool)

Text returns v as text: a string, a byte slice, or a value of a named type of either, such as json.Number.

func ViaJSON

func ViaJSON(v any) (any, error)

ViaJSON returns what the JSON of v means: v written with encoding/json/v2, then read with DecodeJSON. Values of any Go type, such as a map[string]string or an int, come back in the data model of documents.

Types

This section is empty.

Jump to

Keyboard shortcuts

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