flat-key-patch

command
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 16 Imported by: 0

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:

  1. Fixed dotted key — codex.RequiredField with a dotted Name literal.
  2. Dynamic key via [File.Patch] — build the key with string concatenation.
  3. Typed update via [PatchEncoded] + codex.StringMap — validates values.
  4. Key + value validation via [PatchEncoded] + codex.Map — validates both.
  5. Adding new keys not in the file codec — field survival via patchCodec.
  6. Schema rendering — twinCodec, moduleCodec, modulePatchCodec as OpenAPI YAML.
  7. Error cases + structured logging — FileEncodeError, FileDecodeError, FileReadError.
  8. Observer summary — per-operation metrics collected by FileMetricsObserver.
  9. Key+value merge with codex.EntrySlice (single field) — decode flat object into []Container where container name is extracted from the key.
  10. Multi-field key extraction — two segments (tenant + name) extracted from the key into a struct using a codex.Struct domain codec + codex.MapCodecValidated.
  11. 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL