More features
evendeep library in golang
More Features
The store
gives many advanced features from out of the box, but the relative documents are not enough. We will try our best to fill more documentation at a certain point in the future.
In short, the store
can load the data from a provider which will load from its external source, which data will be decoded by a codec decoder.
Once your configuration data is loaded or inserted into the store manually, you can read them at any time, in any way.
That is, the original items can be extracted with a different data type if they can convert smoothly. For example, a string item 3d3h3s
can be got as a time.Duration
value (via MustDuration(path)
).
In totally:
- access fastly to the hierarchical tree data
- app configurations management
- load app configs from external sources
- various codec translators and source providers
- merge config fragment
- save the modifications to alternated external source
- see cmdr-loaders
$(PWD)/.app.toml
is the target
- standalone in-memory key-value pair
- as a in memory cache provider
- as a kernel of a http-router
- highly customizable
Retrieve Node Data
A config entry, so-called as a node (in our Trie-tree), can be retrieved as a typed value:
Extract A Subset
GetM(path, opts...) map[string]any
is a power tool to extract the nodes as a map, which has the flattened keys. The extracted result looks like:
The test code is:
GetM("")
can extract the whole tree, and GetM("app.logging")
extract that subtree.
With filter functor, you can extract app.logging
subtree by GetM("", WithFilter[any](func(node radix.Node[any]) bool { return strings.HasPrefix(node.Key(), "app.logging.") })))
.
Extract Subtree Into Struct
GetSectionFrom
makes extracting to struct easier. For example,
TODO: Transferring a struct into the Store
isn't in our plan yet.
Light-weight Sub-tree
The store
has a dead lightweight subtree accessor. By using WithPrefix
or WithPrefixReplaced
, you can construct a subtree accessor and read/write a node:
Easily Cutting
By using SetPrefix(prefix)
, a store
and its whole subtree can be moved or associated with a new hierarchical tree structure.
By using Dup
or Clone
, and Merge
, the store
can be cut and layout.
Split key with delimiter
If a key contains delimiter, it will be split and insert into the Store
. Technically, the inserter doesn't do special stuff for this key, but the getter will access the tree path separated by the delimiter char.
So when you're loading a YAML file (or others), the dotted key can make the file consicer:
It equals
This feature is builtin and cannot disable, due to we have a Trie-tree store and the node is always recognized at extracting.
A side effect is, when you're using a float-point number as a key, that will have matters. Our tip is, don't do that.
Decompound Map
The data can be split and orchestrated into tree structure when you're inserting a map.
This feature works when a provider is loading its external source. Set(k, v)
doesn't decompound anything in v
. But Merge(k, m)
does:
Of course, it shall be a valid deep map[string]any
.
Decompound Slice
A slice can be decompounded once you enabled WithStoreFlattenSlice(true)
.
It works for loading an external source, similar like Decompounding Map.
For example:
The supplied hjson file has the following contents:
Notable Nodes
Different from other configuration managers, the store is not only a memory key-value store. The nodes in store are both notable and taggable.
Dump()
will produce the detailed output.
Walk The Whole Tree
Walk(path)
gives a way to iterator the Store
.
As a feature based on Trie-tree, Walk("app")
will walk from the parent of app.
node. And Walk("app.")
will walk from the app.
node.
Like GetM
, passing "" will walk from the top-level root node.
Modified State
Each node has a modified state, so we can extract them from the Store
:
We assume the user calling Set(k, v)
will cause modified state was set to true. And app loading and merging to the Store
at startup will be treated as initial state, so the modified state keeps unset (i.e., false).
Provider for External Source
Provider
s could be used to describe an external source, such as file, env, or consul and vice versa.
Codec
s are used to describe how to decode a streaming input loaded by Provider, such as yaml, toml, json, hcl, etc.
A loading logic typically is:
More tests at tests/*_test.go .
Implement A Provider
A Provider
should support Read()
:
Your provider can support OnceProvider
or StreamProvider
while its Read
return radix.ErrNotImplemented
. OnceProvider assumes the loader read binary content at once. StreamProvider
allows reading the large content progressively.
Set TTL for A Key
Since v1.2.5, we added TTL support for a trie node. Once the TTL arrives, the data of the target key will be cleared. Which means, the data field would be reset to zero value (or a nil value for an any type T).
The code could be:
How is this guide?
Last updated on