codes

package
v0.85.0 Latest Latest
Warning

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

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

Documentation

Overview

Package codes writes the content-code registry (content_codes) for the packages that own content: contenturl for host content, groups and content for ContentKit's own records. Codes are generated in Postgres (contentkit_content_code), so one generator serves runtime and migrations.

Index

Constants

View Source
const MaxSlug = 80

MaxSlug is the longest slug, in bytes (slugs are ASCII).

Variables

View Source
var (
	ErrInvalid  = errors.New("contenturl: invalid input")
	ErrNotFound = errors.New("contenturl: not found")
	ErrConflict = errors.New("contenturl: conflict")
)

Functions

func Drain

func Drain(rows Rows, err error) error

Drain consumes and closes rows.

func Language

func Language(l string) (string, error)

Language normalizes a slug language key.

func Merge

func Merge(ctx context.Context, q Querier, qs, tenant string, from, into Key) error

Merge makes from's code redirect to into's final code and re-points codes already redirected to from, so every redirect is one hop. ErrNotFound when either has no code; ErrConflict when into already redirects to from.

func Put

func Put(ctx context.Context, q Querier, qs, tenant string, entries []Entry) (map[Key]string, error)

Put assigns a code to every entry without one and sets its slugs, in the caller's transaction; it returns every entry's code. qs is the quoted schema. Code collisions and, under READ COMMITTED, concurrent registrations of the same content are retried; stricter isolation levels report the latter as a serialization failure for the caller to retry.

func PutAliases added in v0.83.0

func PutAliases(ctx context.Context, q Querier, qs, tenant string, aliases []Alias) error

PutAliases records aliases in the caller's transaction. An alias is written once: repeating it is a no-op, pointing it at another target (or locator) is ErrConflict, a target without a code ErrNotFound; roll back on any error.

func Reserve added in v0.83.0

func Reserve(ctx context.Context, q Querier, qs, tenant string, keep map[Key]string) error

Reserve gives each key the code an import kept, in the caller's transaction, before Put slugs it: a key keeps a code it already has, so a different one is ErrConflict, as is a code another key holds.

func Slugify

func Slugify(s string) string

Slugify turns text into a URL slug: NFKD, diacritics dropped, a few letters folded (ß -> ss), apostrophes removed, every other run of non [a-z0-9] one hyphen, at most MaxSlug bytes cut at a word boundary. Text without Latin letters or digits gives "".

func ValidAlias added in v0.83.0

func ValidAlias(source, kind, key string) error

ValidAlias checks an alias's source, identifier space and key.

func ValidCode added in v0.83.0

func ValidCode(code string) bool

ValidCode reports whether code is a content code contentkit_content_code can produce.

func ValidSlug

func ValidSlug(s string) bool

ValidSlug reports whether s is a slug Slugify can produce ("" included).

Types

type Alias added in v0.83.0

type Alias struct {
	Source  string
	Kind    string
	Key     string
	Locator string
	Target  Key
}

Alias is an identifier of another system resolving to a registered key.

type Entry

type Entry struct {
	Key
	Slug  string
	Slugs map[string]string
}

Entry is one content record's slugs. Slugs nil keeps the stored per-language slugs; a non-nil map replaces them.

type Key

type Key struct{ Kind, ID string }

Key names content within a tenant.

type PgxQuerier

type PgxQuerier interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}

PgxQuerier is a pgx pool, connection or transaction.

type Querier

type Querier interface {
	Query(ctx context.Context, sql string, args ...any) (Rows, error)
}

Querier runs one statement. Every registry statement returns rows.

func Pgx

func Pgx(q PgxQuerier) Querier

Pgx adapts a pgx pool, connection or transaction.

func SQL

func SQL(tx *sql.Tx) Querier

SQL adapts a database/sql transaction (pgx stdlib driver).

type Rows

type Rows interface {
	Next() bool
	Scan(...any) error
	Err() error
	Close()
}

Rows is the row iterator the registry reads.

Jump to

Keyboard shortcuts

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