draft

package
v0.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package draft owns the targeted replacement semantics introduced by Spec 004.

Index

Constants

View Source
const (
	ScopeBaseline            = "baseline"
	ScopeEnvironmentOverride = "environment_override"
)

Variables

View Source
var (
	ErrEnvironmentNotFound = errors.New("draft environment not found")
	ErrInvalidScope        = errors.New("invalid draft write scope")
	// ErrSourceRevisionChanged lets a source transaction request the typed
	// source_revision_mismatch response without coupling draft to an adapter.
	ErrSourceRevisionChanged = errors.New("source revision changed")
)

Functions

func EnsureSchema

func EnsureSchema(schema Schema) error

func FormatRevision

func FormatRevision(revision uint64) string

func ParseRevision

func ParseRevision(etag string) (uint64, error)

func ValidScope

func ValidScope(scope string) bool

Types

type ConfigurationPresence

type ConfigurationPresence struct {
	Present bool           `json:"present"`
	Value   map[string]any `json:"value,omitempty"`
}

func (ConfigurationPresence) MarshalJSON

func (p ConfigurationPresence) MarshalJSON() ([]byte, error)

MarshalJSON preserves a present null-free empty object while omitting value when the replacement is missing.

type ConflictError

type ConflictError struct {
	Code                  string
	CurrentRevision       uint64
	CurrentSourceRevision string
	ConflictScope         string
	CurrentState          View
}

func (*ConflictError) Error

func (e *ConflictError) Error() string

type Environment

type Environment struct {
	ID   string `json:"environment_id"`
	Name string `json:"name"`
	Kind string `json:"kind"`
}

type Field

type Field struct {
	Path                       string
	Type                       string
	Nullable                   bool
	EnvironmentOverrideAllowed bool
	Required                   bool
	Default                    any
	Enum                       []any
	MinLength                  *int
	MaxLength                  *int
	Minimum                    *float64
	Maximum                    *float64
}

type FieldState

type FieldState struct {
	Path                       string             `json:"path"`
	PackDefault                FieldValuePresence `json:"pack_default"`
	Baseline                   FieldValuePresence `json:"baseline"`
	DraftBaseline              FieldValuePresence `json:"draft_baseline"`
	EnvironmentOverride        FieldValuePresence `json:"environment_override"`
	DraftEnvironmentOverride   FieldValuePresence `json:"draft_environment_override"`
	Effective                  FieldValuePresence `json:"effective"`
	Origin                     string             `json:"origin"`
	EnvironmentOverrideAllowed bool               `json:"environment_override_allowed"`
	IsEnvironmentOverridden    bool               `json:"is_environment_overridden"`
	SourceRevision             string             `json:"source_revision"`
	Nullable                   bool               `json:"nullable"`
}

type FieldValuePresence

type FieldValuePresence struct {
	Present bool `json:"present"`
	Value   any  `json:"value,omitempty"`
}

func (FieldValuePresence) MarshalJSON

func (p FieldValuePresence) MarshalJSON() ([]byte, error)

type LayerState

type LayerState struct {
	Source   ConfigurationPresence `json:"source"`
	Draft    ConfigurationPresence `json:"draft"`
	Resolved ConfigurationPresence `json:"resolved"`
	Dirty    bool                  `json:"dirty"`
}

type Mutation

type Mutation struct {
	ExpectedRevision       uint64
	ExpectedSourceRevision string
	Scope                  string
	Action                 string
	Configuration          json.RawMessage
	// Prepare runs under the same lock and source snapshot as precondition
	// checks. It lets higher-level resources turn a focused operation into the
	// complete replacement required by the draft model.
	Prepare func(View) (json.RawMessage, error)
	// Validate runs after structural validation and before the replacement is
	// committed. Domain services use it for rules outside the Pack structure.
	Validate func(map[string]any) error
}

type Save

type Save struct {
	ExpectedRevision       uint64
	ExpectedSourceRevision string
	Commit                 func(SourceSnapshot, State) (SourceSnapshot, error)
}

Save commits the current view's two visible draft replacements to a source. The caller-owned saver executes while the draft lock is held so the source snapshot, preconditions, draft cleanup, and returned view share one observation boundary.

type Schema

type Schema struct {
	PackRef  string
	Defaults map[string]any
	Fields   []Field
}

type SourceProvider

type SourceProvider func() (SourceSnapshot, error)

type SourceSnapshot

type SourceSnapshot struct {
	Revision             string
	Baseline             map[string]any
	EnvironmentOverrides map[string]map[string]any
}

type State

type State struct {
	Revision             uint64
	Baseline             map[string]any
	BaselinePresent      bool
	EnvironmentOverrides map[string]map[string]any
}

type Store

type Store struct {
	// contains filtered or unexported fields
}

func NewMemory

func NewMemory(source SourceSnapshot, state State) *Store

func Open

func Open(path string, source SourceProvider) (*Store, error)

Open uses .conflow/draft.json only until Spec 005 defines the managed-file format. SourceProvider deliberately remains separate: draft replacements are not a source adapter and must not become the source of truth.

func (*Store) Mutate

func (s *Store) Mutate(schema Schema, environments []Environment, environmentID string, mutation Mutation) (View, uint64, error)

func (*Store) SaveToSource

func (s *Store) SaveToSource(schema Schema, environments []Environment, environmentID string, save Save) (View, uint64, error)

func (*Store) View

func (s *Store) View(schema Schema, environments []Environment, environmentID string) (View, uint64, error)

type StructuralError

type StructuralError struct {
	Code    string `json:"code"`
	Path    string `json:"path"`
	Scope   string `json:"scope"`
	Message string `json:"message"`
}

func ValidateReplacement

func ValidateReplacement(schema Schema, scope string, raw json.RawMessage) (map[string]any, []StructuralError)

type ValidationError

type ValidationError struct{ Details []StructuralError }

func (*ValidationError) Error

func (e *ValidationError) Error() string

type View

type View struct {
	EnvironmentID        string         `json:"environment_id"`
	PackRef              string         `json:"pack_ref"`
	SourceRevision       string         `json:"source_revision"`
	Dirty                bool           `json:"dirty"`
	DirtyScopes          []string       `json:"dirty_scopes"`
	Baseline             LayerState     `json:"baseline"`
	EnvironmentOverride  LayerState     `json:"environment_override"`
	Effective            map[string]any `json:"effective"`
	FieldStates          []FieldState   `json:"field_states"`
	AffectedEnvironments []Environment  `json:"affected_environments"`
}

func BuildView

func BuildView(schema Schema, environments []Environment, source SourceSnapshot, state State, environmentID string) (View, error)

Jump to

Keyboard shortcuts

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