state

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package state 实现 Layer 4 — State Contract(§9)。

核心原则:StateChange 是观测事实,不是数据库事务。`before`/`after` 都是观测到的 状态片段,由此自然解释 before=null / before==after / version=0(§9.1)。

包依赖约束(§4.3):state 只允许依赖 event 与 schema,禁止 import contract。 因此 CheckAgainst 返回 state 自己的 Issue,由 contract 层 StateChecker 转换为 分层 Violation;规则 ID 沿用 §9.7 的 gt.state.* 命名空间。

Index

Constants

View Source
const (
	SevError = "error"
	SevWarn  = "warn"
	SevInfo  = "info"
)

严重级别复用契约规则的 error | warn | info 三档(§11.2)。

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	SubjectType string      `msgpack:"subject_type" yaml:"subject_type"`
	SubjectID   string      `msgpack:"subject_id" yaml:"subject_id"`
	Op          Op          `msgpack:"op" yaml:"op"`
	Path        string      `msgpack:"path" yaml:"path"`
	Before      event.Value `msgpack:"before,omitempty" yaml:"before,omitempty"`
	After       event.Value `msgpack:"after,omitempty" yaml:"after,omitempty"`
	Version     int64       `msgpack:"version,omitempty" yaml:"version,omitempty"`
	Metadata    event.Value `msgpack:"metadata,omitempty" yaml:"metadata,omitempty"`
}

Change 是一次状态变更观测(字段与 event.StateChange 逐字段一致,§9.4)。

func ExtractChanges

func ExtractChanges(v event.Value) []Change

ExtractChanges 从 payload 的 _state_changes 字段抽取状态变更(取代 event.ExtractStateChanges)。 每个元素按字段名映射到 Change;缺失字段留零值,由 Change.Validate 在后续判定。

func (Change) CheckAgainst

func (c Change) CheckAgainst(s *schema.Schema) []Issue

CheckAgainst 校验本 change 相对 subject schema 的合法性(§9.5 / §9.7)。 返回 state.Issue 列表(可能多条)。path 越界即提前返回;其余检查尽量全部收集。

func (Change) Validate

func (c Change) Validate() error

Validate 校验最小必填字段与 op 取值(等价 event.StateChange.Validate)。

type Issue

type Issue struct {
	RuleID   string
	Severity string
	Path     string
	Message  string
}

Issue 是 state 层的内部违规(不 import contract,供上层转换,§9.7)。

type Op

type Op string

Op 是状态变更算子(§9.4)。

const (
	OpSet    Op = "set"
	OpDelete Op = "delete"
	OpMerge  Op = "merge"
)

type Path

type Path struct {
	Raw      string
	Segments []PathSeg
}

Path 是解析后的状态路径,可静态解析到一个声明字段(§9.3)。

func ParsePath

func ParsePath(s string) (Path, error)

ParsePath 解析 §9.3 闭集语法:segment ( "." segment | "[" index "]" | "[" quoted "]" )*。 不支持通配符/负下标/表达式/entity lookup;path 必须能静态解析到一个声明字段。

func (Path) Resolve

func (p Path) Resolve(s *schema.Schema) (*schema.Field, error)

Resolve 把 path 静态解析到 subject schema 中的一个声明字段(§9.3)。 解析失败返回 gt.state.path-resolvable 对应错误(path 未能落到一个声明字段)。

type PathSeg

type PathSeg struct {
	Name  string
	Index *int
	Key   *string
}

PathSeg 是 path 的一个寻址段:Name 为字段名,Index/Key 为可选的下标或动态键(§9.3)。

type Subject

type Subject struct {
	Type    string     // subject_type,单段小写
	Schema  schema.Ref // 必填:subject 的状态形状
	Version int        // schema major
	IDField string     // 可选但强烈建议:subject_id 的来源字段
	Paths   []string   // 可选白名单;空 = schema 内全部字段可变
}

Subject 是 manifest states 条目的 Go 表示(§9.2)。

func (*Subject) UnmarshalYAML

func (s *Subject) UnmarshalYAML(value *yaml.Node) error

UnmarshalYAML 解析 manifest states 形状:subject / schema(字符串) / version / id_field / paths(§9.2)。 schema 与 version 合并为 schema.Ref,供与 ManifestSchemaIndex 用同一 Wire() 键对齐。

Jump to

Keyboard shortcuts

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