Barky
is a Go package for managing hierarchical key-value data structures, mainly designed for configuration files
such as JSON, YAML, and TOML.
It can flatten nested data structures into a map[string]string
while preserving path information, detecting conflicts,
and tracking multi-file sources.
- Supports converting nested
map
,slice
, andarray
into a flatmap[string]string
. - Uses dots to denote map keys and brackets to denote array/slice indices.
- The
Path
type parses hierarchical keys into a sequence of path segments (map keys or array indices). - Provides
SplitPath
to convert"foo.bar[0]"
into structured path segments, andJoinPath
to join path segments back into a string. - Performs syntax validation to prevent invalid keys (e.g., consecutive dots, unclosed brackets).
-
The
Storage
type manages a set of flattened key-value pairs and builds an internal tree to detect structural conflicts. -
Each value is associated with a source file index, making it easy to track origins when merging multiple files.
-
Key methods include:
Set
: Set a key-value pair with conflict detection.Get
: Retrieve a value, optionally providing a default.Has
: Check whether a key exists.SubKeys
: List subkeys under a given path.Keys
: Get all stored keys in sorted order.
go get github.com/go-spring/barky
package main
import (
"fmt"
"github.com/go-spring/barky"
)
func main() {
s := barky.NewStorage()
fileIdx := s.AddFile("config.yaml")
_ = s.Set("server.hosts[0].ip", "192.168.0.1", fileIdx)
_ = s.Set("server.hosts[1].ip", "192.168.0.2", fileIdx)
fmt.Println("Keys:", s.Keys())
fmt.Println("SubKeys of server.hosts:", s.SubKeys("server.hosts"))
fmt.Println("Get server.hosts[0].ip:", s.Get("server.hosts[0].ip"))
}
Output:
Keys: [server.hosts[0].ip server.hosts[1].ip]
SubKeys of server.hosts: [0 1]
Get server.hosts[0].ip: 192.168.0.1
-
Configuration Management Convert configuration files of various formats into a unified flat key-value map for easier comparison and merging.
-
Querying & Retrieval Access nested data directly using simple path strings like
"server.hosts[0].ip"
, without manually traversing the structure. -
Multi-File Merging Handle multiple configuration files at once, track the source of each key, and detect conflicts.
-
Data Transformation Flatten hierarchical structures for easier processing in testing, diffing, or downstream systems.
- Paths must not contain spaces, consecutive dots, unclosed brackets, or other invalid formats.
- Type conflicts in the tree structure (e.g.,
"user.name"
vs"user[0]"
) will return an error. RawData
andRawFile
expose internal storage directly—use with caution.
Apache 2.0 License