Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

v0 source pure

Package bylaws stores a DAO's governing documents — bylaws and mandates — as named plaintext files and amends them wi...

Overview

Package bylaws stores a DAO's governing documents — bylaws and mandates — as named plaintext files and amends them with verifiable diff patches.

Documents are keyed by a slash-separated path ("mandates/treasury.md"); folders are a naming convention over the path, not stored objects — a folder exists exactly when a document path has it as a prefix. The only mutation is Apply: a Patch carries the sha256 of the document text it was diffed against plus the edit script that transforms that text into the proposed one. Apply rejects the patch when the document has changed since (optimistic concurrency, no clobbering) and otherwise replays the script. An amendment whose result is empty removes the document, so a stored document is never empty.

The package is governance-agnostic: it decides nothing about WHO may amend. A consuming realm (e.g. a DAO) gates Apply behind its own vote and keeps the *Bylaws handle private — Apply mutates, so the handle must never be exposed to untrusted callers.

Constants 3

const MaxPathLen, MaxDocLen

1const (
2	// MaxPathLen bounds a document path's byte length.
3	MaxPathLen = 200
4
5	// MaxDocLen bounds a document's byte length. Bylaws are human-written
6	// prose; the cap keeps documents renderable and patch replay bounded.
7	MaxDocLen = 64 * 1024
8)
source

const MaxOps

1const MaxOps = 16 * 1024
source

MaxOps bounds the number of ops a decoded patch may carry. DiffTexts output always stays far below it (see maxMyersRunes), so the cap only rejects hand-built pathological payloads at the wire boundary.

const OpKeep, OpDelete, OpInsert

1const (
2	OpKeep   OpType = 'K' // keep the next N runes of the base text
3	OpDelete OpType = 'D' // delete the next N runes of the base text
4	OpInsert OpType = 'I' // insert literal text
5)
source

Variables 1

Functions 5

func HashText

1func HashText(text string) string
source

HashText returns the hex sha256 of a text.

func IsValidPath

1func IsValidPath(path string) bool
source

IsValidPath reports whether a path names a document: one or more non-empty "/"-separated segments of [a-zA-Z0-9._-] characters, where no segment is "." or "..". The restricted charset keeps paths render- and link-safe and the patch encoding delimiter-free.

func New

1func New() *Bylaws
source

New creates an empty document set.

func DecodePatch

1func DecodePatch(s string) (Patch, error)
source

DecodePatch parses an Encode payload back into a Patch, validating the path, the base hash shape, and every op (counts must be canonical, so DecodePatch accepts exactly Encode's output shape; insert literals must be valid UTF-8, keeping documents plaintext and Keep's rune math byte-faithful). Replay validity against the actual document is Apply's job; DecodePatch only guarantees the patch is well-formed.

func DiffTexts

1func DiffTexts(path, base, proposed string, exists bool) (Patch, error)
source

DiffTexts builds the patch transforming base into proposed for the document at path. exists reports whether the document currently exists (base must be "" when it does not); a patch built with exists=false creates the document. Both texts must be valid UTF-8 — documents are plaintext, and the invariant keeps Keep/Delete rune math byte-faithful.

Types 4

type Bylaws

struct
1type Bylaws struct {
2	docs *bptree.BPTree // path (string) -> document text (string, never empty)
3}
source

Bylaws is one DAO's set of governing documents, keyed by path.

Methods on Bylaws

func Apply

method on Bylaws
1func (b *Bylaws) Apply(p Patch) error
source

Apply verifies the patch and amends the document set: the target's current text must hash to the patch base ("" base means the document must not exist), and the edit script must consume exactly that text. An empty result removes the document. Apply is the package's only mutation; on any error the set is unchanged.

func Diff

method on Bylaws
1func (b *Bylaws) Diff(path, proposed string) (Patch, error)
source

Diff builds the patch that changes the document at path to the proposed text: it diffs against the document's current text and pins its hash. A new path yields a create patch; empty proposed text yields a remove patch.

func Get

method on Bylaws
1func (b *Bylaws) Get(path string) (string, bool)
source

Get returns a document's text and whether it exists.

func Has

method on Bylaws
1func (b *Bylaws) Has(path string) bool
source

Has reports whether a document exists.

func Hash

method on Bylaws
1func (b *Bylaws) Hash(path string) string
source

Hash returns the hex sha256 of a document's text, or an empty string when the document does not exist. It is the base a Patch must pin to amend the document (an empty hash pins "the document must not exist").

func Iterate

method on Bylaws
1func (b *Bylaws) Iterate(prefix string, fn func(path, text string) bool) bool
source

Iterate walks the documents under a prefix in sorted path order until fn returns true. It returns true when the walk was stopped by fn. The set must not be amended during iteration (no Apply from fn).

func List

method on Bylaws
1func (b *Bylaws) List(prefix string) []string
source

List returns the sorted document paths under a prefix. An empty prefix lists every document. The prefix is a raw path prefix: include the trailing slash to scope to a folder (e.g. "mandates/"), or "mandates" also matches a sibling file like "mandates-old.md".

func Size

method on Bylaws
1func (b *Bylaws) Size() int
source

Size returns the number of documents.

type Op

struct
1type Op struct {
2	Type OpType
3	N    int    // rune count (Keep and Delete only)
4	Text string // inserted literal (Insert only)
5}
source

Op is one run of a patch's edit script. Keep and Delete address the base text positionally (a rune count), so only Insert carries bytes — a small edit to a large document stays a small patch.

type OpType

ident
1type OpType byte
source

OpType is the kind of a patch operation.

type Patch

struct
1type Patch struct {
2	Path string // target document path
3	Base string // hex sha256 of the base text; "" pins "document absent" (create)
4	Ops  []Op   // edit script, replayed in order against the base text
5}
source

Patch is a verifiable amendment to one document: the edit script that transforms the document's base text into the proposed text, pinned to that base by hash. Apply rejects the patch unless the target currently hashes to Base, so a patch can never be applied to text it was not diffed against.

Methods on Patch

func Encode

method on Patch
1func (p Patch) Encode() string
source

Encode serializes the patch to a compact single-string payload fit for a transaction argument: "v0:<path>:<base>:" followed by one segment per op — "K<n>;" and "D<n>;" carry rune counts, "I<len>:<bytes>;" carries the inserted literal length-prefixed by its byte length (no escaping needed). DecodePatch is the exact inverse.

func Format

method on Patch
1func (p Patch) Format(base string) (string, error)
source

Format renders the patch against its base text as a plain-text change summary: kept runs collapse to a marker, deleted and inserted text is shown literally with every line marker-prefixed ("- "/"+ "), so a multi-line literal cannot masquerade as the summary's own markers (an insertion containing "\n- fake" renders as "+ …" and "+ - fake"). It fails like replay when the script does not fit the base. The output is raw text — callers rendering markdown must escape it (the content is document text, and insertions are proposer-controlled).

func IsCreate

method on Patch
1func (p Patch) IsCreate() bool
source

IsCreate reports whether the patch creates the document (its base pins "document absent").

func IsNoop

method on Patch
1func (p Patch) IsNoop() bool
source

IsNoop reports whether applying the patch leaves the document set unchanged (the script only keeps text, or creates an empty document). The check is shape-based: a hand-built script that deletes and reinserts identical text is not detected (Diff never produces one).

func IsRemove

method on Patch
1func (p Patch) IsRemove() bool
source

IsRemove reports whether applying the patch removes the document (the script deletes the whole base text and inserts nothing).

Imports 8

Source Files 3