data

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Lookup

func Lookup(d Data, k, vptr any) (ok bool)

Lookup retrieves the value associated with the given key and stores it into the pointer that vptr points to.

Reports whether a value was found.

d = data.WithValue(d, x, 42)

var out int
data.Lookup(k, x, &out) // == true

Panics if the type of the value for the pointer is not compatible with the value associated with the key.

Types

type Data

type Data interface {
	// Keys returns all known keys for this Data object in an unspecified
	// order.
	Keys() []any

	// Values retrives the value associated with the given key or nil.
	Value(k any) any
}

Data stores arbitrary data for Matchers and Replacers as they traverse or build the Go AST.

Data provides an interface very similar to context.Context.

func Index

func Index(d Data) Data

Index returns a copy of the provided Data object where items are indexed for fast lookup.

Lookups with Value or the Lookup function are constant time in the returned data object.

Use this if your workload is divided between write-heavy and read-heavy portions.

func New

func New() Data

New builds a new empty Data.

func WithValue

func WithValue(d Data, k, v any) Data

WithValue returns a new Data with the given key-value pair associated with it.

d = data.WithValue(d, x, 42)
d.Value(x) // == 42

The original object is left unmodified so if the returned Data object is discarded, its value will not be made available.

Retreiving the value from this Data object is a linear time operation. To optimize for read-heavy use cases, use the Index function.

Panics if either the key or the value are nil.

Jump to

Keyboard shortcuts

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