storage

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 10, 2025 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Overview

Package storage provides hierarchical configuration storage and path parsing utilities.

Features: - Storage manages key-value pairs with support for nested paths, subkey lookup, and conflict detection. - Path represents structured access paths with support for parsing (SplitPath) and construction (JoinPath). - Supports two path types:

  • Key (e.g., "user.name") for map access
  • Index (e.g., "[0]") for array access

- Maintains a tree structure (treeNode) for consistent and type-safe hierarchy management.

Use cases: - Accessing values in JSON/YAML/TOML-like configs - Managing nested config data (CRUD) - Validating structure and detecting conflicts

Notes: - Path syntax follows common config patterns (e.g., "users[0].profile.age") - Type-safe path handling (keys vs. indices)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JoinPath

func JoinPath(path []Path) string

JoinPath constructs a string representation from a slice of Path segments. Keys are joined with '.', and indices are represented as '[i]'.

Types

type Path

type Path struct {
	Type PathType // Type determines whether the segment is a key or index.
	Elem string   // Elem holds the actual key or index value as a string.
}

Path represents a segment of a hierarchical path. Each segment is either a key (e.g., "user") or an index (e.g., "0").

func SplitPath

func SplitPath(key string) (_ []Path, err error)

SplitPath parses a string path into a slice of Path segments. It supports keys separated by '.' and indices enclosed in brackets (e.g., "users[0].name").

type PathType

type PathType int

PathType represents the type of a path segment.

const (
	PathTypeKey   PathType = iota // PathTypeKey indicates a named key in a map.
	PathTypeIndex                 // PathTypeIndex indicates a numeric index in a list.
)

type Storage

type Storage struct {
	// contains filtered or unexported fields
}

Storage stores hierarchical key-value pairs and tracks their structure using a tree. It supports nested paths and detects structural conflicts when paths differ in type.

func NewStorage

func NewStorage() *Storage

NewStorage creates and initializes a new Storage instance.

func (*Storage) Has

func (s *Storage) Has(key string) bool

Has returns true if the given key exists in the storage, either as a direct value or as a valid path in the hierarchical tree structure.

func (*Storage) RawData

func (s *Storage) RawData() map[string]string

RawData returns the underlying flat key-value map. Note: This exposes internal state; use with caution.

func (*Storage) Set

func (s *Storage) Set(key, val string) error

Set inserts a key-value pair into the storage. It also constructs or extends the corresponding hierarchical path in the tree. Returns an error if there is a type conflict or if the key is empty.

func (*Storage) SubKeys

func (s *Storage) SubKeys(key string) (_ []string, err error)

SubKeys returns the immediate subkeys under the given key path. It walks the tree structure and returns child elements if the path exists. Returns an error if there's a type conflict along the path.

Jump to

Keyboard shortcuts

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