datamodel

package
v0.0.0-...-bd88888 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2026 License: MIT Imports: 4 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrFieldNotFound = errors.New("field not found")

Functions

func CreateMergePatch

func CreateMergePatch(old, new map[string]any) (map[string]any, error)

CreateMergePatch returns the RFC 7386 merge patch that transforms old into new: fields whose value changed carry the new value, fields present in old and absent from new are removed, everything else is absent. Arrays are atomic values (no schema means no merge keys), so any element change replaces the whole array. Maps merge, so removal recurses: a vanished map-valued field nulls its leaves rather than the map, because the map at the target may hold entries other writers own. An explicit null in new means removal, exactly as RFC 7386 defines it. Both sides are normalized through JSON serialization first, so numeric types compare by value. An empty result means the documents are equal and there is nothing to patch.

func DeepCopyAny

func DeepCopyAny(v any) any

DeepCopyAny returns a deep copy of a JSON-shaped value: map[string]any and []any are recursively cloned so that mutations of the copy never affect the original; primitives (string, float64, bool, int64, nil, etc.) are immutable and returned as-is.

func DeepEqual

func DeepEqual(a, b any) bool

DeepEqual reports value equality of two JSON-shaped values in canonical JSON semantics, computed structurally instead of through serialization: numbers compare by value whatever Go numeric type carries them (the int64/float64 split is a decode-path artifact, not data), maps and lists compare element-wise through the same normalization, and everything else compares strictly. Two values are DeepEqual exactly when their NormalizeAny forms are reflect.DeepEqual.

func Normalize

func Normalize(m map[string]any) (map[string]any, error)

Normalize is NormalizeAny for a document content map. A nil map normalizes to an empty one.

func NormalizeAny

func NormalizeAny(v any) (any, error)

NormalizeAny returns the canonical JSON form of a value: the value serialized to JSON and parsed back, so every number lands as float64, every nested value is JSON-shaped, and documents materialize their content. The result shares no memory with the input. Two values with equal canonical JSON normalize to deep-equal results, whatever Go types they started from.

Types

type Document

type Document interface {
	fmt.Stringer
	json.Marshaler
	json.Unmarshaler

	// Hash returns a string identifier for equality checking. Two elements are equal iff their
	// hashes are equal. This is based on full content (like a hash of all fields). Content
	// hash is the only identity notion the document interface provides: primary keys are
	// schema information. For performance reasons Hash SHOULD be amortized O(1): the engine's
	// hot paths (the Z-set algebra, Distinct, group folds, join indexes) ask for it
	// repeatedly. Most implementations would memoize the digest. Caching the hash is optional,
	// but a cached digest MUST be reset in every mutator (SetField and friends) for
	// correctness.
	Hash() string

	// Copy returns a deep copy of the document when possible.
	Copy() Document

	// Merge combines this document with another document and returns the merged result.
	Merge(other Document) Document

	// New returns a new empty document of the same type.
	New() Document

	// GetField returns the value for a field name.
	// It may return ErrFieldNotFound when the field is missing.
	GetField(key string) (any, error)

	// SetField sets the value for a field name.
	// It may return ErrFieldNotFound when the field is missing.
	SetField(key string, value any) error

	// Fields returns a deep copy of the document fields map.
	Fields() map[string]any
}

Document is the interface that Z-set elements must implement.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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