value

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package value is the typed value model of evaluation: what queries return, filters transform, predicates compare and templates render.

A Value is one of Null, Bool, Int, BigInt, Float, String, Bytes, List, Object, Date, Nodeset, Unit, Regex or HTTPResponse. Numbers compare across representations (Int 1 equals Float 1.0). Objects keep the member order they were built with; DecodeJSON sorts members by key.

Values have three textual forms: Display (plain text, used inside lists and messages), Repr (`kind <display>`, the actual side of an assert failure) and Expected (the expected side). Render is the form a template placeholder produces; only scalar values are renderable.

Index

Constants

This section is empty.

Variables

View Source
var ErrType = errors.New("types between actual and expected are not consistent")

ErrType reports values whose kinds cannot be compared.

Functions

func Display

func Display(v Value) string

Display returns the plain text form of v.

func Equal

func Equal(a, b Value) bool

Equal reports whether a and b are equal. Numbers compare by value across representations; lists and objects compare member by member, in order. Regex and HTTPResponse values are never equal to anything.

func Expected

func Expected(v Value) string

Expected describes v as the expected side of a predicate.

func FormatFloat

func FormatFloat(f float64) string

FormatFloat formats f in the shortest form that reads back exactly, without exponent; whole numbers keep a ".0" suffix ("1.0", "-2.0", "1e21" prints "1000000000000000000000.0"). Non-finite values print "inf", "-inf", "NaN".

func FormatYear

func FormatYear(y int) string

FormatYear writes a year with at least four digits; years outside 0..9999 carry an explicit sign.

func Render

func Render(v Value) (string, bool)

Render returns the text a template placeholder produces for v. Only null, booleans, numbers, strings and dates are renderable.

func Repr

func Repr(v Value) string

Repr returns `kind <display>`, just the kind for Unit, or "none" for nil.

func TranslateRegex

func TranslateRegex(pattern string) string

TranslateRegex rewrites \d, \D, \w, \W, \s and \S to their Unicode definitions. Inside a bracket class \W and \S cannot be expressed as a union and keep their ASCII meaning; \b stays ASCII as well.

Types

type BigInt

type BigInt string

BigInt is a number too large for Int or Float, kept as its decimal text (for example a 30-digit JSON integer).

func (BigInt) Kind

func (BigInt) Kind() Kind

type Bool

type Bool bool

Bool is a boolean.

func (Bool) Kind

func (Bool) Kind() Kind

type Bytes

type Bytes []byte

Bytes is a byte sequence.

func (Bytes) Kind

func (Bytes) Kind() Kind

type Date

type Date time.Time

Date is an instant; it is always handled in UTC.

func (Date) Kind

func (Date) Kind() Kind

func (Date) UTC

func (d Date) UTC() time.Time

UTC returns the date as a time.Time in UTC.

type Float

type Float float64

Float is a 64-bit floating point number.

func (Float) Kind

func (Float) Kind() Kind

type HTTPResponse

type HTTPResponse struct {
	// Location is the absolute URL of the next hop, "" when HasLocation is false.
	Location    string
	HasLocation bool
	Status      int
}

HTTPResponse is one hop of a redirect chain.

func (HTTPResponse) Kind

func (HTTPResponse) Kind() Kind

type Int

type Int int64

Int is an integer that fits in 64 bits.

func (Int) Kind

func (Int) Kind() Kind

type Kind

type Kind int

Kind is the type of a value as shown in messages.

const (
	KindNull Kind = iota
	KindBool
	KindInteger
	KindFloat
	KindString
	KindBytes
	KindList
	KindObject
	KindDate
	KindNodeset
	KindUnit
	KindRegex
	KindHTTPResponse

	// KindCount is the number of kinds; new kinds go above it.
	KindCount
)

Value kinds. BigInt values have KindInteger.

func (Kind) String

func (k Kind) String() string

type List

type List []Value

List is an ordered list of values.

func (List) Kind

func (List) Kind() Kind

type Member

type Member struct {
	Key   string
	Value Value
}

Member is an object member.

type Nodeset

type Nodeset int

Nodeset is the result of an XPath query: only its size is kept.

func (Nodeset) Kind

func (Nodeset) Kind() Kind

type Null

type Null struct{}

Null is the JSON null.

func (Null) Kind

func (Null) Kind() Kind

type Object

type Object []Member

Object is a list of members.

func (Object) Get

func (o Object) Get(key string) (Value, bool)

Get returns the value of the first member named key.

func (Object) Kind

func (Object) Kind() Kind

type Ordering

type Ordering int

Ordering is the result of comparing two values.

const (
	Less Ordering = iota - 1
	Same
	Greater
	Unordered
)

Orderings. Same means equal; Unordered is returned when a NaN is involved.

func Compare

func Compare(a, b Value) (Ordering, error)

Compare orders two strings (byte-wise), two numbers or two dates. Other combinations return ErrType. Comparing with NaN returns Unordered.

type Regex

type Regex struct {
	Source string
	Re     *regexp.Regexp
}

Regex is a compiled regular expression with its source.

func NewRegex

func NewRegex(pattern string) (Regex, error)

NewRegex compiles a pattern with Unicode-aware character classes (see TranslateRegex).

func (Regex) Kind

func (Regex) Kind() Kind

type String

type String string

String is a UTF-8 string.

func (String) Kind

func (String) Kind() Kind

type Unit

type Unit struct{}

Unit is the value of a query that matched without producing data, such as a cookie attribute flag.

func (Unit) Kind

func (Unit) Kind() Kind

type Value

type Value interface {
	Kind() Kind
	// contains filtered or unexported methods
}

Value is a typed evaluation value. The set of implementations is closed.

func DecodeJSON

func DecodeJSON(text string) (Value, error)

DecodeJSON parses a complete JSON document. Numbers are classified by NumberFromText without precision loss; object members are sorted by key (byte-wise) and a duplicated key keeps its last value.

func FromJSON

func FromJSON(raw any) Value

FromJSON converts a value produced by encoding/json (decoded with UseNumber) into a Value.

func NumberFromText

func NumberFromText(s string) Value

NumberFromText classifies a JSON number by its text: a fraction or exponent makes a Float (BigInt when not finite), an integer that fits 64 bits makes an Int, anything else a BigInt. A BigInt keeps the text with its exponent written `e+N` or `e-N`.

Jump to

Keyboard shortcuts

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