transfer

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package transfer is the domain of moving a project's configuration in and out of the platform as one project document: the document's types, its byte-stable encodings in JSON and YAML, and its strict decoding. The document is a contract read by people and tools the server never sees, so it is versioned apart from the management API and its names are spelled out here and not borrowed from any other type.

Index

Constants

View Source
const (
	// WarningOwnerReplaced is the warning of an owner the document names who
	// may not own in the project.
	WarningOwnerReplaced = "owner_replaced"
	// WarningProtectionKept is the warning of an existing environment the
	// document protects, or stops protecting, differently from the project:
	// an import never changes that.
	WarningProtectionKept = "protection_kept"
)

The warnings an import answers with.

View Source
const (
	SourceDocument = "document"
)

Source is what an import read its changes from.

View Source
const Version = 1

Version is the schema_version this server writes.

Variables

View Source
var SupportedVersions = []int{1}

SupportedVersions are the schema_versions this server reads: every one it ever wrote, since a document kept in git outlives the server that wrote it.

Functions

func CheckCreate

func CheckCreate(d Document) error

CheckCreate returns the refusal of d as the document to make a new project of, nil when it holds what a document alone can tell. Every fault is reported at once, with a pointer into the document, in the order of the document.

It judges what no context judges alone: which sections a create needs, keys repeated within a kind, a unit type and an attribute sharing a key, a standard attribute declared as another type, a configuration for an environment the document does not have, and the form of the rule identifiers and salts. The rules each context holds of its own entities are judged where they are written.

func CheckMerge

func CheckMerge(d, current Document) error

CheckMerge returns the refusal of d as a document to merge into the project whose document is current, nil when it holds what a document alone, read with the project, can tell. Every fault is reported at once, with a pointer into the document, in the order of the document. No section is required.

It judges what no context judges alone, as CheckCreate does, and besides a type that changes, which an existing flag or attribute never does.

func Encode

func Encode(f Format, d Document) ([]byte, error)

Encode returns d written in f. The bytes depend on the document alone: two documents of one project are the same file wherever their lists were built in another order, so a diff of two exports shows real changes. The environments keep their order, which is their sort order, and so do the lists whose order serves (variations, rules, tags, an experiment's weights); every other list is by key, in byte order, and a map is too.

func FlagConditions

func FlagConditions(in []Condition) []flags.Condition

FlagConditions returns the conditions as the flags context holds them.

func Pointer

func Pointer(parts ...any) string

Pointer returns the JSON Pointer to the member or element parts name: a name or the index of an element.

Types

type Attribute

type Attribute struct {
	Key         string `json:"key"`
	Type        string `json:"type"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

Attribute is a declared attribute. The standard attributes every project has are never listed.

type Changes

type Changes struct {
	Environments KindChanges
	UnitTypes    KindChanges
	Attributes   KindChanges
	Segments     KindChanges
	Flags        KindChanges
	Experiments  KindChanges
}

Changes are what an import did, by kind of entity.

func (Changes) Counts

func (c Changes) Counts() map[string]any

Counts returns how many entities of each kind the import created, updated and deleted, as the summary entry of the audit log records them.

type Condition

type Condition struct {
	Kind      string `json:"kind"`
	Attribute string `json:"attribute,omitempty"`
	Op        string `json:"op,omitempty"`
	// Value is a string, a number, a boolean or a list; absent for an
	// operator that takes none.
	Value     any    `json:"value,omitempty"`
	Segment   string `json:"segment,omitempty"`
	Flag      string `json:"flag,omitempty"`
	Variation string `json:"variation,omitempty"`
}

Condition is one comparison inside a rule or a segment, spelled as the management API spells it so that a person moving between the two sees one shape.

type Configuration

type Configuration struct {
	On            bool           `json:"on"`
	OffVariation  string         `json:"off_variation"`
	Fallthrough   string         `json:"fallthrough"`
	Prerequisites []Prerequisite `json:"prerequisites"`
	Overrides     []Override     `json:"overrides"`
	Rules         []Rule         `json:"rules"`
}

Configuration is what a flag serves in one environment.

func (Configuration) Input

Input returns the configuration as a write gives it whole. A rule keeps the identifier and a rollout the salt the document names; the flags context gives the rest.

type Date

type Date struct{ time.Time }

Date is a calendar day, written as 2006-01-02.

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

MarshalJSON writes the day.

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(data []byte) error

UnmarshalJSON reads a day, or an instant of which the UTC day counts: a YAML reader hands a bare date over as an instant.

type Decision

type Decision struct {
	Outcome         string `json:"outcome"`
	WinnerVariation string `json:"winner_variation"`
	Note            string `json:"note"`
}

Decision is what stopping an experiment decided.

type Document

type Document struct {
	SchemaVersion int           `json:"schema_version"`
	Project       *Project      `json:"project"`
	Environments  []Environment `json:"environments"`
	UnitTypes     []UnitType    `json:"unit_types"`
	Attributes    []Attribute   `json:"attributes"`
	Segments      []Segment     `json:"segments"`
	Flags         []Flag        `json:"flags"`
	Experiments   []Experiment  `json:"experiments"`
}

Document is a project's whole configuration: what an export writes and an import reads. Identity is the key; no entity identifier travels, except the rule identifiers and rollout salts that keep a unit in its bucket.

A section that is nil was absent from the document read, which an import into a project treats as untouched; an export always writes every section.

func Decode

func Decode(f Format, data []byte) (Document, error)

Decode reads a document from data written in f. It is strict, so that a document from a newer server fails loudly on an older one instead of importing with silent loss: a field the document does not define is refused naming its JSON Pointer, as is a value of the wrong type, and so is data after the document. Every fault found is reported at once.

A schema_version this server does not read is the invalid error schema_version_unsupported, naming the versions it does; any other fault is the invalid error invalid_document with a violation per fault.

func Effective

func Effective(current, doc Document) Document

Effective returns doc as it applies to the project whose document is current: what a document that names a flag leaves of the flag is its definition and the configurations it holds, so the configuration in an environment the document does not hold stays as the project has it; and what the document leaves open it takes from the project. A rule without an identifier that equals a rule the project has in that configuration is that rule, and a rollout without a salt keeps the one it has, so that a document written by hand, with neither, is the same project when imported again; a rule that equals none is new, and is given both when it is written. The order of overrides and prerequisites is the project's where the same ones are named, since it carries no meaning a document can write.

func FromSnapshot

func FromSnapshot(s Snapshot) Document

FromSnapshot returns the document that holds s, and nothing else: no identifier but the rules', no author of an override, no member, credential or history.

type Environment

type Environment struct {
	Key       string `json:"key"`
	Name      string `json:"name"`
	Protected bool   `json:"protected"`
}

Environment is an environment; its place in the document's list is its sort order.

type Experiment

type Experiment struct {
	Key               string                `json:"key"`
	Name              string                `json:"name"`
	Hypothesis        string                `json:"hypothesis"`
	Owner             string                `json:"owner"`
	Tags              []string              `json:"tags"`
	Flag              string                `json:"flag"`
	UnitType          string                `json:"unit_type"`
	Variations        []ExperimentVariation `json:"variations"`
	TrafficAllocation int                   `json:"traffic_allocation"`
	InclusionSalt     string                `json:"inclusion_salt"`
	VariationSalt     string                `json:"variation_salt"`
	Status            string                `json:"status"`
	ExpectedEndAt     *Date                 `json:"expected_end_at"`
	StartedAt         *time.Time            `json:"started_at"`
	StoppedAt         *time.Time            `json:"stopped_at"`
	ArchivedAt        *time.Time            `json:"archived_at"`
	Decision          *Decision             `json:"decision"`
}

Experiment is an experiment as it stands, whatever its status.

type ExperimentVariation

type ExperimentVariation struct {
	Variation string `json:"variation"`
	Weight    int    `json:"weight"`
}

ExperimentVariation is one variation's share of an experiment's allocated traffic, in basis points. The list's order fixes the cumulative bucket ranges, so it is kept.

type Flag

type Flag struct {
	Key         string   `json:"key"`
	Type        string   `json:"type"`
	Name        string   `json:"name"`
	Description string   `json:"description"`
	Owner       string   `json:"owner"`
	Tags        []string `json:"tags"`
	Lifetime    string   `json:"lifetime"`
	// ExpectedRemovalAt is null for a permanent flag.
	ExpectedRemovalAt *time.Time  `json:"expected_removal_at"`
	AutomaticExposure bool        `json:"automatic_exposure"`
	Archived          bool        `json:"archived"`
	Variations        []Variation `json:"variations"`
	// Configurations are keyed by environment key.
	Configurations map[string]Configuration `json:"configurations"`
}

Flag is a flag's definition with its configuration in every environment.

func (Flag) VariationInputs

func (f Flag) VariationInputs() *[]flags.VariationInput

VariationInputs returns the flag's variations as a write gives them, nil when the document lists none, which gives a boolean flag its two.

type Format

type Format string

Format is how a document is written: JSON is the canonical encoding and YAML is derived from it field for field.

const (
	JSON Format = "json"
	YAML Format = "yaml"
)

The formats.

func ParseFormat

func ParseFormat(name string) (Format, bool)

ParseFormat returns the format named name, and whether there is one.

func (Format) Extension

func (f Format) Extension() string

Extension is the file extension of a document in f, without the dot.

type KindChanges

type KindChanges struct {
	Created []string
	Updated []Update
	Deleted []string
}

KindChanges are what an import did to the entities of one kind, by key.

type MalformedError

type MalformedError struct{ Detail string }

MalformedError is input that is not a document in the format at all: text that does not parse, or that holds more than one document. The adapter answers it as a request it cannot decode, where a document that parses but breaks a rule is answered with pointers into it.

func (*MalformedError) Error

func (e *MalformedError) Error() string

type Mode

type Mode string

Mode is how an import applies a document to the platform.

const (
	// ModeCreate makes a new project of exactly the document.
	ModeCreate Mode = "create"
	// ModeMerge creates or updates what the document names in a project and
	// leaves the rest.
	ModeMerge Mode = "merge"
)

The modes.

type Override

type Override struct {
	UnitType  string `json:"unit_type"`
	UnitID    string `json:"unit_id"`
	Variation string `json:"variation"`
	Note      string `json:"note"`
}

Override pins a variation for one unit. Who wrote it does not travel: the importer is its author.

type PatchOperation

type PatchOperation struct {
	Op    string
	Path  string
	From  string
	Value any
}

PatchOperation is one operation of an RFC 6902 JSON Patch.

type Plan

type Plan struct {
	Environments SectionPlan
	UnitTypes    SectionPlan
	Attributes   SectionPlan
	Segments     SectionPlan
	Flags        SectionPlan
	Experiments  SectionPlan
}

Plan is what merging a document into a project would create and update. A merge deletes nothing.

func Compare

func Compare(current, doc Document) Plan

Compare returns what applying doc, as Effective leaves it, to the project whose document is current creates and updates. An entity the two agree on is in neither list: whether two entities agree is whether the document writes them alike, which is the patch between them being empty. A section doc lacks plans nothing.

type Planned

type Planned struct {
	// Index is the entity's place in its section of the document.
	Index int
	Key   string
	// Patch turns the entity as the project has it into the entity as the
	// merge leaves it, over the entity's entry of the document; nil for a
	// created entity.
	Patch []PatchOperation
}

Planned is an entity a merge creates or updates.

type Prerequisite

type Prerequisite struct {
	Flag      string `json:"flag"`
	Variation string `json:"variation"`
}

Prerequisite requires another flag to serve one of its variations.

type Project

type Project struct {
	Key         string `json:"key"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

Project is the project's own fields.

type Rule

type Rule struct {
	// ID keeps the rule's identity across instances, so an SDK's rule_id
	// stays stable; a hand-written document may leave it out.
	ID          string      `json:"id"`
	Description string      `json:"description"`
	Conditions  []Condition `json:"conditions"`
	Serve       Serve       `json:"serve"`
}

Rule is an ordered set of conditions, all of which must hold for it to serve.

type SectionPlan

type SectionPlan struct {
	Created []Planned
	Updated []Planned
}

SectionPlan is what a merge does to the entities of one section, in the order of the document.

type Segment

type Segment struct {
	Key         string      `json:"key"`
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Private     bool        `json:"private"`
	Conditions  []Condition `json:"conditions"`
}

Segment is a named set of attribute conditions.

type Serve

type Serve struct {
	Kind       string `json:"kind"`
	Variation  string `json:"variation,omitempty"`
	Experiment string `json:"experiment,omitempty"`
	UnitType   string `json:"unit_type,omitempty"`
	// Percentage is a rollout's share in basis points; nil for another
	// kind, since 0 is a share too.
	Percentage *int `json:"percentage,omitempty"`
	// Salt is a rollout's; it decides which units fall in the share, so it
	// travels. A hand-written document may leave it out.
	Salt string `json:"salt,omitempty"`
}

Serve is what a matching rule serves, spelled as the management API spells it.

type Snapshot

type Snapshot struct {
	Project      workspace.Project
	Environments []workspace.Environment
	UnitTypes    []workspace.UnitType
	// Attributes include the standard ones, which a document never lists.
	Attributes []workspace.Attribute
	Segments   []flags.Segment
	Flags      []flags.Flag
	// Configurations are each flag's, in the environments it has one in.
	Configurations map[flags.FlagID][]flags.EnvironmentConfiguration
	Experiments    []experiments.Experiment
	// Emails are the addresses of the members who own a flag or an
	// experiment: members do not travel, so an owner does as an address.
	Emails map[access.MemberID]string
}

Snapshot is a project's configuration as the contexts hold it, read in one consistent view.

type UnitType

type UnitType struct {
	Key         string `json:"key"`
	Name        string `json:"name"`
	Description string `json:"description"`
}

UnitType is a declared kind of unit.

type Update

type Update struct {
	Key   string
	Patch []PatchOperation
}

Update is an entity an import changed, with the patch that turns the entity as it was into the entity as it is, over the entity's entry of the document.

type Variation

type Variation struct {
	Key string `json:"key"`
	// Value is JSON of the flag's type.
	Value       json.RawMessage `json:"value"`
	Name        string          `json:"name"`
	Description string          `json:"description"`
}

Variation is one of the values a flag serves.

type Warning

type Warning struct {
	// Code is the stable name of the warning, such as owner_replaced.
	Code string
	// Pointer is where in the document it applies, "" for the document as a
	// whole.
	Pointer string
	Detail  string
	// Params are the values a client explains the warning with.
	Params map[string]any
}

Warning says something an import did differently from what the document asks, or could not do, without refusing it.

Jump to

Keyboard shortcuts

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