merger

package
v0.15.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package merger 实现 Kustomize 风格的 map[string]any 深度合并。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyPatch

func ApplyPatch(doc map[string]any, patchJSON []byte) (map[string]any, error)

ApplyPatch applies an RFC 6902 JSON Patch (provided as a JSON-encoded array) to the merged document. The document is round-tripped through JSON to take advantage of the upstream library; this is acceptable because patching only happens during reload, never on the hot Get() path.

The returned map replaces the caller's document on success. On failure, the caller MUST keep its previous document untouched (failure-safe pipeline).

func Deep

func Deep(dst, src map[string]any, opt Options) error

Deep 把 src 合并进 dst(dst 被原地修改)。 复杂度 O(n),不分配新顶层 map。

规则:

  • 若 dst[k] 不存在 → dst[k] = src[k];
  • 若两侧都为 map → 递归;
  • 若两侧都为 slice 且 AppendSlices → 追加;否则 src 替换 dst;
  • 若 path 在 MergeKeys 表中 → 按 mergeKey 字段做 strategic merge;
  • 类型不一致:Strict=true 报错,否则 src 替换 dst。

func PatchBytesFromAny

func PatchBytesFromAny(v any) ([]byte, error)

PatchBytesFromAny converts a decoded patch payload (either []any or already-encoded raw JSON) to the JSON byte form expected by ApplyPatch.

Types

type Options

type Options struct {
	// AppendSlices 为 true 时,slice 不被替换而是追加(罕见,需在 _meta 中显式启用)。
	AppendSlices bool
	// Strict 为 true 时类型不一致直接报错;否则后者覆盖前者。
	Strict bool
	// MergeKeys (Phase 132 / SPEC-132) enables Kustomize-style strategic
	// merge for list-of-object slices. Each entry maps a dotted path in
	// the merged tree to the field name that identifies "the same item"
	// across overlays. Example:
	//
	//	MergeKeys: map[string]string{
	//	  "spec.containers": "name",
	//	  "services":        "id",
	//	}
	//
	// When merging a slice under spec.containers, entries are aligned by
	// the value of their "name" field and merged recursively instead of
	// being appended or replaced wholesale.
	MergeKeys map[string]string
}

Options 控制合并行为。

Jump to

Keyboard shortcuts

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