README
¶
Config Pattern Mapper Example
This example demonstrates the new pattern-based config mapping system in Glazed.
Overview
The pattern mapper allows you to declaratively map config file structures to layer parameters using pattern matching rules, without writing custom Go functions.
Key Features
- Exact Match: Simple one-to-one mappings
- Named Captures: Extract values from config paths and use them in parameter names
- Wildcards: Match multiple paths with a single pattern
- Nested Rules: Group related mappings together with clean syntax
- Capture Inheritance: Child rules inherit captures from parent rules
- Builder API: Fluent way to assemble rules with the same strict validation
- YAML/JSON Loader: Load mapping rules from a file (
mappings.yaml)
Running the Example
go run main.go
Pattern Syntax
Exact Match
{
Source: "app.settings.api_key",
TargetLayer: "demo",
TargetParameter: "api-key",
}
Named Capture
{
Source: "app.{env}.api_key", // Captures "env"
TargetLayer: "demo",
TargetParameter: "{env}-api-key", // Uses captured value
}
Wildcard
{
Source: "app.*.api_key", // Matches any environment
TargetLayer: "demo",
TargetParameter: "api-key",
}
Nested Rules
{
Source: "app.settings",
TargetLayer: "demo",
Rules: []MappingRule{
{Source: "api_key", TargetParameter: "api-key"},
{Source: "threshold", TargetParameter: "threshold"},
},
}
Comparison with ConfigFileMapper
Old Way (ConfigFileMapper function):
mapper := func(rawConfig interface{}) (map[string]map[string]interface{}, error) {
configMap := rawConfig.(map[string]interface{})
result := map[string]map[string]interface{}{
"demo": make(map[string]interface{}),
}
// Manual traversal and mapping
if app, ok := configMap["app"].(map[string]interface{}); ok {
if settings, ok := app["settings"].(map[string]interface{}); ok {
if apiKey, ok := settings["api_key"]; ok {
result["demo"]["api-key"] = apiKey
}
}
}
return result, nil
}
New Way (Pattern Mapper - Rules Array):
mapper, err := patternmapper.NewConfigMapper(layers,
patternmapper.MappingRule{
Source: "app.settings.api_key",
TargetLayer: "demo",
TargetParameter: "api-key",
},
)
New Way (Pattern Mapper - Builder API):
b := patternmapper.NewConfigMapperBuilder(layers).
Map("app.settings.api_key", "demo", "api-key").
MapObject("environments.{env}.settings", "demo", []patternmapper.MappingRule{
patternmapper.Child("api_key", "{env}-api-key"),
})
mapper, err := b.Build() // Validates via NewConfigMapper
New Way (Pattern Mapper - YAML/JSON Loader):
// mappings.yaml
mappings:
- source: "environments.{env}.settings"
target_layer: "demo"
rules:
- source: "api_key"
target_parameter: "{env}-api-key"
// Go
mapper, err := patternmapper.LoadMapperFromFile(layers, "mappings.yaml")
if err != nil { /* handle */ }
middleware := middlewares.LoadParametersFromFile(
"config.yaml",
middlewares.WithConfigMapper(mapper),
)
When to Use
- Pattern Mapper: For simple to moderately complex mappings (5 rules or less)
- ConfigFileMapper: For complex logic, arrays, conditionals, or transformations
Both approaches are fully supported and can be used interchangeably.
Documentation
¶
There is no documentation for this package.
Click to show internal directories.
Click to hide internal directories.