Documentation
¶
Overview ¶
Package main demonstrates flat dotted-key JSON patching with go-codex.
Some JSON formats use flat dotted keys instead of nested objects:
{
"properties.desired.modules.my-container": {"image": "...", "status": "running"},
"properties.desired.modules.other-container": {"image": "...", "status": "running"},
"properties.desired.schemaVersion": "1.0"
}
This pattern is common in Azure IoT Edge device twins, Kubernetes ConfigMaps with dot-separated keys, and flat namespaced configuration stores.
The example shows ten patterns:
- Fixed dotted key — codex.RequiredField with a dotted Name literal.
- Dynamic key via [File.Patch] — build the key with string concatenation.
- Typed update via [PatchEncoded] + codex.StringMap — validates values.
- Key + value validation via [PatchEncoded] + codex.Map — validates both.
- Adding new keys not in the file codec — field survival via patchCodec.
- Schema rendering — twinCodec, moduleCodec, modulePatchCodec as OpenAPI YAML.
- Error cases + structured logging — FileEncodeError, FileDecodeError, FileReadError.
- Observer summary — per-operation metrics collected by FileMetricsObserver.
- Key+value merge with codex.EntrySlice (single field) — decode flat object into []Container where container name is extracted from the key.
- Multi-field key extraction — two segments (tenant + name) extracted from the key into a struct using a codex.Struct domain codec + codex.MapCodecValidated.
- Static key — the container name is a compile-time constant, not a wire value. codex.MapCodecSafe injects the constant into the decoded struct; encode drops it.
Run with: go run ./examples/flat-key-patch
Click to show internal directories.
Click to hide internal directories.