Document and benchmark sibling inheritance
This commit is contained in:
@@ -52,6 +52,22 @@ func Example_seriatim() {
|
||||
// Output: visitor true the event occurred <nil> <nil>
|
||||
}
|
||||
|
||||
func Example_inheritedPresets() {
|
||||
result, err := consumers.LoadPresets(configkit.File(fixture("inheritance", "presets.yml")))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
path := configkit.Keys("presets", "custom", "enabled")
|
||||
info, _ := result.Metadata().Lookup(path)
|
||||
custom := result.Config.Presets["custom"]
|
||||
fmt.Println(custom.Enabled, result.Metadata().IsExplicit(path), custom.Settings["secondary"]["width"])
|
||||
fmt.Println(info.Origin.Locations[0].SourceID, info.Origin.Inheritance[0].From)
|
||||
// Output:
|
||||
// false true 20
|
||||
// source-1 /presets/base/enabled
|
||||
}
|
||||
|
||||
func Example_weatherStartup() {
|
||||
const blockedName = "CONFIGKIT_WEATHER_EXAMPLE_BLOCKED"
|
||||
const tokenName = "CONFIGKIT_WEATHER_EXAMPLE_TOKEN"
|
||||
|
||||
20
examples/consumers/inheritance.go
Normal file
20
examples/consumers/inheritance.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package consumers
|
||||
|
||||
import configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
|
||||
// PresetCatalog is ordinary application data; no inheritance fields or
|
||||
// registration hooks are required in the typed configuration.
|
||||
type PresetCatalog struct {
|
||||
Presets map[string]Preset `yaml:"presets"`
|
||||
}
|
||||
|
||||
type Preset struct {
|
||||
Enabled bool `yaml:"enabled"`
|
||||
Settings map[string]map[string]int `yaml:"settings"`
|
||||
}
|
||||
|
||||
// LoadPresets performs one load; callers may inspect inherited presence and
|
||||
// source origins on the returned result before choosing a preset to execute.
|
||||
func LoadPresets(source configkit.Source) (*configkit.Result[PresetCatalog], error) {
|
||||
return configkit.Load[PresetCatalog](source)
|
||||
}
|
||||
35
examples/consumers/inheritance_test.go
Normal file
35
examples/consumers/inheritance_test.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package consumers_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
||||
)
|
||||
|
||||
func TestPresetCatalogOneLoadAndDerivation(t *testing.T) {
|
||||
result, err := consumers.LoadPresets(configkit.File(fixture("inheritance", "presets.yml")))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.Config.Presets["base"].Settings["secondary"]["width"] != 10 ||
|
||||
result.Config.Presets["custom"].Settings["secondary"]["width"] != 20 ||
|
||||
result.Config.Presets["custom"].Enabled {
|
||||
t.Fatalf("expanded presets = %#v", result.Config.Presets)
|
||||
}
|
||||
enabled := configkit.Keys("presets", "custom", "enabled")
|
||||
if !result.Metadata().IsExplicit(enabled) {
|
||||
t.Fatal("inherited false lost explicit presence")
|
||||
}
|
||||
info, found := result.Metadata().Lookup(enabled)
|
||||
if !found || info.Origin.Locations[0].SourceID != "source-1" ||
|
||||
len(info.Origin.Inheritance) != 1 || info.Origin.Inheritance[0].From.String() != "/presets/base/enabled" {
|
||||
t.Fatalf("origin and derivation = %#v, %v", info, found)
|
||||
}
|
||||
nested := configkit.Keys("presets", "custom", "settings", "secondary", "width")
|
||||
info, found = result.Metadata().Lookup(nested)
|
||||
if !found || len(info.Origin.Inheritance) != 1 ||
|
||||
info.Origin.Inheritance[0].From.String() != "/presets/custom/settings/primary/width" {
|
||||
t.Fatalf("destination-local derivation = %#v, %v", info, found)
|
||||
}
|
||||
}
|
||||
10
examples/consumers/testdata/inheritance/presets.yml
vendored
Normal file
10
examples/consumers/testdata/inheritance/presets.yml
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
presets:
|
||||
base:
|
||||
enabled: false
|
||||
settings:
|
||||
primary: {width: 10}
|
||||
secondary: {extends: primary}
|
||||
custom:
|
||||
extends: base
|
||||
settings:
|
||||
primary: {width: 20}
|
||||
Reference in New Issue
Block a user