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 ¶
- func Bool(v any) (b, ok bool)
- func Compare(a, b any) (int, bool)
- func DecodeJSON(data []byte) (any, error)
- func DecodeJSONLenient(data []byte) (any, error)
- func Equal(a, b any) bool
- func Len(v any) (int, bool)
- func List(v any) ([]any, bool)
- func Number(v any) (any, bool)
- func Object(v any) (map[string]any, bool)
- func ParseInteger(s string) (int64, bool)
- func ParseNumber(s string) (any, bool)
- func Same(a, b any) bool
- func Text(v any) (string, bool)
- func ViaJSON(v any) (any, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
List returns the items of v, a slice or an array other than a byte slice, which is text.
func Number ¶
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 ¶
Object returns the members of v, a map whose keys are strings, or of a named string type.
func ParseInteger ¶
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 ¶
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 ¶
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.
Types ¶
This section is empty.