textpath

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package textpath finds the collaborative-text documents of an entity row and reads and writes them by instance path.

A (syncplane.v1.field_behavior).text field holds one document per row when the path to it crosses only singular messages, and one document per element when it crosses a keyed message list or a message-valued map. An instance path names one such document: the dotted schema path with the element key inserted after each list or map it crosses ("notes.<key>.body"), in the segment encoding of engine/internal/lww. That is the form a PATCH mask and a FieldDelta name a document by.

Field and Instance enumerate the documents a row holds; Ref resolves one instance path given from outside. Nothing here is safe for concurrent use on the same message, as protoreflect messages are not.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func KeepClearedVersions

func KeepClearedVersions(fields []Field, prev map[string][]byte, next protoreflect.Message) error

KeepClearedVersions replaces each document prev holds and next has cleared with an empty document that carries the cleared one's version vector, so a writer that reads the cleared field allocates counters past the old ones. prev is a Snapshot taken before the write; next is edited in place. A document whose element next no longer holds is not touched.

It returns an error, as `text field "<path>": stored document: <err>`, when a document in prev does not decode.

func Snapshot

func Snapshot(fields []Field, msg protoreflect.Message) map[string][]byte

Snapshot returns the non-empty documents of msg by instance path. When two elements share a key, the first one wins. It returns nil for empty fields, and an empty map when msg holds no document.

The returned slices are the ones msg holds; a later Set on msg replaces them without changing them.

func Union

func Union(
	fields []Field,
	base, next protoreflect.Message,
	merge func(path string, base, next []byte) ([]byte, error),
) error

Union unions every document of base into next in place, pairing documents by instance path. Where both hold a document, next gets the result of merge(path, baseDoc, nextDoc). Where only base holds one, next gets base's. A position only next holds keeps next's, and an element base holds and next does not stays absent.

Union returns the first error merge returns, wrapped with the instance path as `text field "<path>": <err>`. next may then hold the merges done before it.

Types

type Candidate

type Candidate struct {
	// Path is the dotted schema path.
	Path string
	// Field is the text field itself.
	Field protoreflect.FieldDescriptor
	// Oneof names the oneof the field or a message on its path is a member
	// of, or is empty when there is none.
	Oneof string
	// UnkeyedList is the dotted schema path of the innermost message list
	// without a key on the field's path, or is empty when there is none.
	UnkeyedList string
}

Candidate is a text field Collect found, handed to the caller's check before Collect accepts it.

type Field

type Field struct {
	// Path is the dotted schema path, without element keys.
	Path string
	// contains filtered or unexported fields
}

Field is one collaborative-text field of a message type, as Collect finds it. A Field is immutable and safe for concurrent use.

func Collect

func Collect(md protoreflect.MessageDescriptor, check func(Candidate) error) ([]Field, error)

Collect returns every text field of md, in field order with nested fields after the field that holds them.

The walk descends through singular message fields, keyed and unkeyed message lists and message-valued maps, and enters no opaque field, no reference and no message type already on the path. It calls check for every text field it finds, oneof members and elements of unkeyed lists included, and returns the first error check returns, unchanged.

func (Field) Instances

func (f Field) Instances(msg protoreflect.Message) []Instance

Instances returns every document position of f in msg, in walk order: list order, and map entries sorted by encoded key. A field that is not Nested has exactly one position, set or not; a Nested field has one per element msg holds, so none when a list or map on the way is empty or unset. Two elements with the same key give two instances with the same path.

Enumeration creates nothing in msg.

func (Field) Nested

func (f Field) Nested() bool

Nested reports whether the path to f crosses a keyed list or a map, so that a row holds one document per element instead of exactly one.

type Instance

type Instance struct {
	// Path is the instance path.
	Path string
	// contains filtered or unexported fields
}

Instance is one document position of a Field in one message. Set and Clear write through to the message the Instance came from.

func Instances

func Instances(fields []Field, msg protoreflect.Message) []Instance

Instances returns the instances of every field in fields, field by field, each in the order Field.Instances gives.

func (Instance) Bytes

func (ti Instance) Bytes() []byte

Bytes returns the document bytes, or nil when the field or a singular message on the way to it is unset.

func (Instance) Clear

func (ti Instance) Clear()

Clear unsets the document. It creates nothing: an unset message on the way already means the document is unset.

func (Instance) Set

func (ti Instance) Set(b []byte)

Set writes the document bytes, creating any unset singular message on the way. b is retained.

type Ref

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

Ref addresses one text document inside a message by instance path. The zero Ref addresses nothing; use ParseRef. A Ref is immutable and safe for concurrent use.

func ParseRef

func ParseRef(desc protoreflect.MessageDescriptor, path string) (Ref, error)

ParseRef resolves an instance path against desc. It returns an error, as `<message name> has no text field "<path>"`, when path does not end on a (syncplane.v1.field_behavior).text field, names a field desc does not have, or crosses a list without a key, a map without message values, or a map key that does not decode.

func (Ref) Bytes

func (r Ref) Bytes(msg protoreflect.Message) []byte

Bytes returns the document bytes in msg, or nil when the field, or a message, list element or map entry on the way to it, is absent.

func (Ref) Set

func (r Ref) Set(msg protoreflect.Message, value []byte) error

Set writes the document bytes in msg, creating any unset singular message on the way. value is retained. A list element or map entry on the way must already be in msg, because an element is more than its key: Set returns an error, as `text field "<path>" addresses a list element the value does not carry` (or `a map entry`), when one is missing. Set may have created singular messages in msg before it returns that error.

Jump to

Keyboard shortcuts

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