radix

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NTBranch   nodeType    = iota // non-leaf nodes in a tree
	NTLeaf                        // leaf node
	NTData     = 1 << iota        // node has data field, only if it is a leaf node
	NTModified                    // node attrs(data, desc, comment, or tag) modified?
	NTMask     = NTLeaf           // mask for checking if it's a branch or leaf
)
View Source
const (
	FgLightGreen = color.FgLightGreen // light green
	FgGreen      = color.FgGreen      // green
)
View Source
const NoDelimiter rune = 0 // reserved for an internal special tree

Variables

View Source
var ColorToColor = color.ToColor

ColorToColor is a wrapper for color.ToColor to make a string around by specified color.Color.

View Source
var ColorToDim = color.ToDim

ColorToDim is a wrapper for color.ToDim to make a string around by dimmed foreground.

View Source
var StatesEnvSetColorMode = func(b bool) {
	states.Env().SetNoColorMode(b)
}

StatesEnvSetColorMode is a setter for "--no-color"

Functions

func NewTrie

func NewTrie[T any]() *trieS[T]

NewTrie returns a Trie-tree instance.

func NewTrieBy added in v1.1.3

func NewTrieBy[T any](delimiter rune) *trieS[T]

NewTrieBy returns a Trie-tree instance.

Types

type Extractor

type Extractor func(outputPtr any, defaultValue ...any) (err error) // data field extractor

type FilterFn

type FilterFn[T any] func(node Node[T]) bool // used by GetM, MustM, ...

type MOpt

type MOpt[T any] func(s *prefixPutter[T]) // used by GetM, MustM, ...

func WithFilter

func WithFilter[T any](filter FilterFn[T]) MOpt[T]

WithFilter can be used in calling nodeS[T].GetM(path, ...)

func WithKeepPrefix

func WithKeepPrefix[T any](b bool) MOpt[T]

WithKeepPrefix _

func WithoutFlattenKeys

func WithoutFlattenKeys[T any](b bool) MOpt[T]

WithoutFlattenKeys allows returns a nested map. If the keys contain delimiter char, they will be split as nested sub-map.

type MoreIntegersGetters

type MoreIntegersGetters interface {
	GetKibiBytes(key string, defaultVal ...uint64) (ret uint64, err error)
	MustKibiBytes(key string, defaultVal ...uint64) (ret uint64)
	GetKiloBytes(key string, defaultVal ...uint64) (ret uint64, err error)
	MustKiloBytes(key string, defaultVal ...uint64) (ret uint64)
}

MoreIntegersGetters collects the extractors for Kibi-byte and Kilo-byte representations.

type Node

type Node[T any] interface {
	EndsWith(ch rune) bool
	StartsWith(ch rune) bool

	// Walk iterators the whole sub-tree from this node.
	Walk(cb func(path, fragment string, node Node[T]))

	// Dup duplicates a new instance from this one. = Clone.
	Dup() (newNode *nodeS[T])

	Data() T             // retrieve the data value, just valid for leaf node
	Key() string         // retrieve the key field (full path of the node), just valid for leaf node
	Description() string // retrieve the description field, just valid for leaf node
	Comment() string     // retrieve the remarks field, just valid for leaf node
	Tag() any            // retrieve the tag field, just valid for leaf node

	SetData(data T)                  // setter for Data field
	SetEmpty()                       // SetEmpty clear the Data field. An empty node is same with node.Empty() or ! HasData()
	SetComment(desc, comment string) // setter for Description and Comment field
	SetTag(tag any)                  // setter for Tag field

	Modified() bool     // node data changed by user?
	SetModified(b bool) // set modified state
	ToggleModified()    // toggle modified state

	IsLeaf() bool   // check if a node type is leaf
	IsBranch() bool // check if a node is branch (has children)
	HasData() bool  // check if a node has data. only leaf node can contain data field. = ! Empty() bool
	Empty() bool    // check if the node has no data. It means an empty data.
}

Node is a Trie-tree node.

type Source

type Source struct {
	// Function is the package path-qualified function name containing the
	// source line. If non-empty, this string uniquely identifies a single
	// function in the program. This may be the empty string if not known.
	Function string `json:"function"`
	// File and Line are the file name and line number (1-based) of the source
	// line. These may be the empty string and zero, respectively, if not known.
	File string `json:"file"`
	// line number
	Line int `json:"line"`
}

Source describes the location of a line of source code.

type Trie

type Trie[T any] interface {
	Insert(path string, data T) (oldData any)                                // Insert data (T) to path
	Search(word string) (found bool)                                         // tests if word exists (= Has)
	Query(path string) (data T, branch, found bool, err error)               // full ability word searching (=enhanced Has)
	Locate(path string) (node *nodeS[T], branch, partialMatched, found bool) // Locate is an enhanced Has and returns more internal information (=enhanced Has)
	SetComment(path, description, comment string) (ok bool)                  // set extra meta-info bound to a key
	SetTag(path string, tags any) (ok bool)                                  // set extra notable data bound to a key
	Dump() string                                                            // dumping the node tree for debugging, including some internal states

	Remove(path string) (removed bool)                                // Remove a key and its children
	RemoveEx(path string) (nodeRemoved, parent Node[T], removed bool) // RemoveEx a key and its children

	Merge(pathAt string, data map[string]any) (err error) // advanced operation to Merge hierarchical data

	StartsWith(path string, r rune) (yes bool) // tests the last path fragment by delimiter
	EndsWith(path string, r rune) (yes bool)   // tests the last path fragment by delimiter

	// Set = Insert
	Set(path string, data T) (node Node[T], oldData any) // = Insert
	Has(path string) (found bool)                        // = Search
	HasPart(path string) (found bool)                    // tests if word exists, even if a partial matching.
	Get(path string) (data T, found bool)                // shortcut to Query
	MustGet(path string) (data T)                        // shortcut to Get

	// SetNode at once, advanced api here.
	SetNode(path string, data T, tag any, descriptionAndComments ...string) (ret Node[T], oldData any)
	// SetEmpty clear the Data field.
	SetEmpty(path string) (oldData any)
	// Update a node whether it existed or not.
	Update(path string, cb func(node Node[T], old any))

	TypedGetters[T] // getters

	WithPrefix(prefix ...string) (entry Trie[T])            // appends prefix string and make a new instance of Trie[T]
	WithPrefixReplaced(newPrefix ...string) (entry Trie[T]) // make a new instance of Trie with prefix
	SetPrefix(newPrefix ...string)                          // set prefix. Change it on a store takes your own advantages.
	Prefix() string                                         // return current prefix string
	Delimiter() rune                                        // return current delimiter, generally it's dot ('.')
	SetDelimiter(delimiter rune)                            // setter. Change it in runtime doesn't update old delimiter inside tree nodes.

	// Dup duplicates a new instance from this one. = Clone.
	Dup() (newTrie *trieS[T]) // a native Clone function

	// Walk iterators the whole tree for each node.
	Walk(path string, cb func(path, fragment string, node Node[T]))

	String() string               // for log/slog text mode
	MarshalJSON() ([]byte, error) // for log/slog json mode
}

Trie tree, an radix-tree

type TypedBooleanGetters

type TypedBooleanGetters interface {
	GetBool(path string, defaultVal ...bool) (ret bool, err error)
	MustBool(path string, defaultVal ...bool) (ret bool)
	GetBoolSlice(path string, defaultVal ...bool) (ret []bool, err error)
	MustBoolSlice(path string, defaultVal ...bool) (ret []bool)
	GetBoolMap(path string, defaultVal ...map[string]bool) (ret map[string]bool, err error)
	MustBoolMap(path string, defaultVal ...map[string]bool) (ret map[string]bool)
}

TypedBooleanGetters collects boolean extractors

type TypedComplexesGetters

type TypedComplexesGetters interface {
	GetComplex128(path string, defaultVal ...complex128) (ret complex128, err error)
	MustComplex128(path string, defaultVal ...complex128) (ret complex128)
	GetComplex64(path string, defaultVal ...complex64) (ret complex64, err error)
	MustComplex64(path string, defaultVal ...complex64) (ret complex64)
	GetComplex128Slice(path string, defaultVal ...complex128) (ret []complex128, err error)
	MustComplex128Slice(path string, defaultVal ...complex128) (ret []complex128)
	GetComplex64Slice(path string, defaultVal ...complex64) (ret []complex64, err error)
	MustComplex64Slice(path string, defaultVal ...complex64) (ret []complex64)
	GetComplex128Map(path string, defaultVal ...map[string]complex128) (ret map[string]complex128, err error)
	MustComplex128Map(path string, defaultVal ...map[string]complex128) (ret map[string]complex128)
	GetComplex64Map(path string, defaultVal ...map[string]complex64) (ret map[string]complex64, err error)
	MustComplex64Map(path string, defaultVal ...map[string]complex64) (ret map[string]complex64)
}

TypedComplexesGetters collects the extractors for complex numbers.

type TypedFloatsGetters

type TypedFloatsGetters interface {
	GetFloat64(path string, defaultVal ...float64) (ret float64, err error)
	MustFloat64(path string, defaultVal ...float64) (ret float64)
	GetFloat32(path string, defaultVal ...float32) (ret float32, err error)
	MustFloat32(path string, defaultVal ...float32) (ret float32)
	GetFloat64Slice(path string, defaultVal ...float64) (ret []float64, err error)
	MustFloat64Slice(path string, defaultVal ...float64) (ret []float64)
	GetFloat32Slice(path string, defaultVal ...float32) (ret []float32, err error)
	MustFloat32Slice(path string, defaultVal ...float32) (ret []float32)
	GetFloat64Map(path string, defaultVal ...map[string]float64) (ret map[string]float64, err error)
	MustFloat64Map(path string, defaultVal ...map[string]float64) (ret map[string]float64)
	GetFloat32Map(path string, defaultVal ...map[string]float32) (ret map[string]float32, err error)
	MustFloat32Map(path string, defaultVal ...map[string]float32) (ret map[string]float32)
}

TypedFloatsGetters collects the extractors for float numbers.

type TypedGetters

type TypedGetters[T any] interface {
	GetString(path string, defaultVal ...string) (ret string, err error)        // extract data field to its string representation
	MustString(path string, defaultVal ...string) (ret string)                  // extract data field to its string representation
	GetStringSlice(path string, defaultVal ...string) (ret []string, err error) // extract data field to its string slice representation
	MustStringSlice(path string, defaultVal ...string) (ret []string)           // extract data field to its string slice representation

	// GetStringMap locates a node and returns its data as a string map.
	//
	// Note that it doesn't care about the sub-nodes and these children's data.
	// If you want to extract a nodes tree from a given node, using GetM, or GetR.
	GetStringMap(path string, defaultVal ...map[string]string) (ret map[string]string, err error)
	MustStringMap(path string, defaultVal ...map[string]string) (ret map[string]string) // extract data field to its string map representation

	TypedBooleanGetters
	TypedIntegersGetters
	MoreIntegersGetters
	TypedFloatsGetters
	TypedComplexesGetters
	TypedTimeGetters

	// GetR finds a given path recursively, and returns
	// the matched subtree as a map, which key is a dotted key path.
	//
	// See MustR for a sample result.
	//
	// GetR / MustR returns the whole tree when you gave the path "".
	GetR(path string, defaultVal ...map[string]any) (ret map[string]any, err error)
	// MustR finds a given path in tree recursively, and returns
	// the matched subtree as a map, which key is a dotted key path.
	//
	// A subtree will be flattened to dotted-key-path and value pairs.
	//
	// A sample is like (dumped by spew printer):
	//
	//     map[string]any{
	//       "app.debug": bool(false),
	//       "app.dump": int(3),
	//       "app.dump.to": "stdout",
	//       "app.logging.file": "/tmp/1.log",
	//       "app.logging.rotate": int(6),
	//       "app.logging.words": []string{
	//         "a",
	//         "1",
	//         "false",
	//       },
	//       "app.server.start": int(5),
	//       "app.verbose": bool(true),
	//     }
	//
	// Note the `GetR("app")` return the same result.
	//
	// GetR / MustR returns the whole tree when you gave the path "".
	MustR(path string, defaultVal ...map[string]any) (ret map[string]any)

	// GetM finds a given path recursively, and returns the matched
	// subtree as a map, which keys keep the original hierarchical
	// structure.
	//
	// GetM("") will return the whole tree.
	//
	// The optional MOpt operators could be:
	//  - WithKeepPrefix
	//  - WithFilter
	//  - WithoutFlattenKeys
	GetM(path string, opt ...MOpt[T]) (ret map[string]any, err error)
	// MustM finds a given path recursively, and returns the matched
	// subtree as a map, which keys keep the original hierarchical
	// structure.
	//
	// MustM("") will return the whole tree.
	//
	// The optional MOpt operators could be:
	//  - WithKeepPrefix
	//  - WithFilter
	//  - WithoutFlattenKeys
	//
	// MustM("app.logging") returns a subtree like:
	//
	//     map[string]any{
	//       "file": "/tmp/1.log",
	//       "rotate": int(6),
	//       "words": []string{
	//         "a",
	//         "1",
	//         "false",
	//       },
	//     }
	//
	// Note the key is without prefix 'app.logging.'.
	//
	// If you want to extract subtree with full prefixed keys, using
	// filter is a good idea:
	//
	//    m, err := trie.GetM("", WithFilter[any](func(node Node[any]){
	//       return strings.HasPrefix(node.Key(), "app.logging.")
	//    }))
	MustM(path string, opt ...MOpt[T]) (ret map[string]any)
	// GetSectionFrom finds a given path and loads the subtree into
	// 'holder', typically 'holder' could be a struct.
	//
	// For yaml input
	//
	//    app:
	//      server:
	//        sites:
	//          - name: default
	//            addr: ":7999"
	//            location: ~/Downloads/w/docs
	//
	// The following codes can load it into sitesS struct:
	//
	//	var sites sitesS
	//	err = store.WithPrefix("app").GetSectionFrom("server.sites", &sites)
	//
	//	type sitesS struct{ Sites []siteS }
	//
	//	type siteS struct {
	//	  Name        string
	//	  Addr        string
	//	  Location    string
	//	}
	//
	// In this above case, 'store' loaded yaml and built it
	// into memory, and extract 'server.sites' into 'sitesS'.
	// Since 'server.sites' is a yaml array, it was loaded
	// as a store entry and holds a slice value, so GetSectionFrom
	// extract it to sitesS.Sites field.
	//
	// The optional MOpt operators could be:
	//  - WithKeepPrefix
	//  - WithFilter
	GetSectionFrom(path string, holder any, opts ...MOpt[T]) (err error)
}

TypedGetters makes a formal specification for Trie[any]

type TypedIntegersGetters

type TypedIntegersGetters interface {
	GetInt64(path string, defaultVal ...int64) (ret int64, err error)
	MustInt64(path string, defaultVal ...int64) (ret int64)
	GetInt32(path string, defaultVal ...int32) (ret int32, err error)
	MustInt32(path string, defaultVal ...int32) (ret int32)
	GetInt16(path string, defaultVal ...int16) (ret int16, err error)
	MustInt16(path string, defaultVal ...int16) (ret int16)
	GetInt8(path string, defaultVal ...int8) (ret int8, err error)
	MustInt8(path string, defaultVal ...int8) (ret int8)
	GetInt(path string, defaultVal ...int) (ret int, err error)
	MustInt(path string, defaultVal ...int) (ret int)

	GetUint64(path string, defaultVal ...uint64) (ret uint64, err error)
	MustUint64(path string, defaultVal ...uint64) (ret uint64)
	GetUint32(path string, defaultVal ...uint32) (ret uint32, err error)
	MustUint32(path string, defaultVal ...uint32) (ret uint32)
	GetUint16(path string, defaultVal ...uint16) (ret uint16, err error)
	MustUint16(path string, defaultVal ...uint16) (ret uint16)
	GetUint8(path string, defaultVal ...uint8) (ret uint8, err error)
	MustUint8(path string, defaultVal ...uint8) (ret uint8)
	GetUint(path string, defaultVal ...uint) (ret uint, err error)
	MustUint(path string, defaultVal ...uint) (ret uint)

	GetInt64Slice(path string, defaultVal ...int64) (ret []int64, err error)
	MustInt64Slice(path string, defaultVal ...int64) (ret []int64)
	GetInt32Slice(path string, defaultVal ...int32) (ret []int32, err error)
	MustInt32Slice(path string, defaultVal ...int32) (ret []int32)
	GetInt16Slice(path string, defaultVal ...int16) (ret []int16, err error)
	MustInt16Slice(path string, defaultVal ...int16) (ret []int16)
	GetInt8Slice(path string, defaultVal ...int8) (ret []int8, err error)
	MustInt8Slice(path string, defaultVal ...int8) (ret []int8)
	GetIntSlice(path string, defaultVal ...int) (ret []int, err error)
	MustIntSlice(path string, defaultVal ...int) (ret []int)

	GetUint64Slice(path string, defaultVal ...uint64) (ret []uint64, err error)
	MustUint64Slice(path string, defaultVal ...uint64) (ret []uint64)
	GetUint32Slice(path string, defaultVal ...uint32) (ret []uint32, err error)
	MustUint32Slice(path string, defaultVal ...uint32) (ret []uint32)
	GetUint16Slice(path string, defaultVal ...uint16) (ret []uint16, err error)
	MustUint16Slice(path string, defaultVal ...uint16) (ret []uint16)
	GetUint8Slice(path string, defaultVal ...uint8) (ret []uint8, err error)
	MustUint8Slice(path string, defaultVal ...uint8) (ret []uint8)
	GetUintSlice(path string, defaultVal ...uint) (ret []uint, err error)
	MustUintSlice(path string, defaultVal ...uint) (ret []uint)

	GetInt64Map(path string, defaultVal ...map[string]int64) (ret map[string]int64, err error)
	MustInt64Map(path string, defaultVal ...map[string]int64) (ret map[string]int64)
	GetInt32Map(path string, defaultVal ...map[string]int32) (ret map[string]int32, err error)
	MustInt32Map(path string, defaultVal ...map[string]int32) (ret map[string]int32)
	GetInt16Map(path string, defaultVal ...map[string]int16) (ret map[string]int16, err error)
	MustInt16Map(path string, defaultVal ...map[string]int16) (ret map[string]int16)
	GetInt8Map(path string, defaultVal ...map[string]int8) (ret map[string]int8, err error)
	MustInt8Map(path string, defaultVal ...map[string]int8) (ret map[string]int8)
	GetIntMap(path string, defaultVal ...map[string]int) (ret map[string]int, err error)
	MustIntMap(path string, defaultVal ...map[string]int) (ret map[string]int)

	GetUint64Map(path string, defaultVal ...map[string]uint64) (ret map[string]uint64, err error)
	MustUint64Map(path string, defaultVal ...map[string]uint64) (ret map[string]uint64)
	GetUint32Map(path string, defaultVal ...map[string]uint32) (ret map[string]uint32, err error)
	MustUint32Map(path string, defaultVal ...map[string]uint32) (ret map[string]uint32)
	GetUint16Map(path string, defaultVal ...map[string]uint16) (ret map[string]uint16, err error)
	MustUint16Map(path string, defaultVal ...map[string]uint16) (ret map[string]uint16)
	GetUint8Map(path string, defaultVal ...map[string]uint8) (ret map[string]uint8, err error)
	MustUint8Map(path string, defaultVal ...map[string]uint8) (ret map[string]uint8)
	GetUintMap(path string, defaultVal ...map[string]uint) (ret map[string]uint, err error)
	MustUintMap(path string, defaultVal ...map[string]uint) (ret map[string]uint)
}

TypedIntegersGetters collects integer/unsigned integer extractors

type TypedTimeGetters

type TypedTimeGetters interface {
	GetDuration(path string, defaultVal ...time.Duration) (ret time.Duration, err error)
	MustDuration(path string, defaultVal ...time.Duration) (ret time.Duration)
	GetDurationSlice(path string, defaultVal ...time.Duration) (ret []time.Duration, err error)
	MustDurationSlice(path string, defaultVal ...time.Duration) (ret []time.Duration)
	GetDurationMap(path string, defaultVal ...map[string]time.Duration) (ret map[string]time.Duration, err error)
	MustDurationMap(path string, defaultVal ...map[string]time.Duration) (ret map[string]time.Duration)

	GetTime(path string, defaultVal ...time.Time) (ret time.Time, err error)
	MustTime(path string, defaultVal ...time.Time) (ret time.Time)
	GetTimeSlice(path string, defaultVal ...time.Time) (ret []time.Time, err error)
	MustTimeSlice(path string, defaultVal ...time.Time) (ret []time.Time)
	GetTimeMap(path string, defaultVal ...map[string]time.Time) (ret map[string]time.Time, err error)
	MustTimeMap(path string, defaultVal ...map[string]time.Time) (ret map[string]time.Time)
}

TypedTimeGetters collects the extractors for time.Time and time.Duration.

Jump to

Keyboard shortcuts

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