Help us shape the future of Go! Take the 2026 Go Developer Survey

canonify

package
v1.321.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

SPDX-FileCopyrightText: 2025 Forkbomb BV

SPDX-License-Identifier: AGPL-3.0-or-later

SPDX-FileCopyrightText: 2025 Forkbomb BV

SPDX-License-Identifier: AGPL-3.0-or-later

Index

Constants

This section is empty.

Variables

View Source
var CanonifyPaths = map[string]PathTemplate{
	"users": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		PathLength:      1,
	},
	"organizations": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		PathLength:      1,
	},
	"credential_issuers": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"credentials": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "credential_issuers", Field: "credential_issuer"},
		PathLength:      3,
	},
	"custom_checks": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"wallets": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"wallet_actions": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "wallets", Field: "wallet"},
		PathLength:      3,
	},
	"wallet_versions": {
		Field:           "tag",
		CanonifiedField: "canonified_tag",
		Parent:          &Parent{Collection: "wallets", Field: "wallet"},
		PathLength:      3,
	},
	"verifiers": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"use_cases_verifications": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "verifiers", Field: "verifier"},
		PathLength:      3,
	},
	"pipelines": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"pipeline_results": {
		Field:           "workflow_id",
		CanonifiedField: "canonified_identifier",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"news": {
		Field:           "title",
		CanonifiedField: "canonified_title",
		PathLength:      1,
	},
	"mobile_runners": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "organizations", Field: "owner"},
		PathLength:      2,
	},
	"mobile_devices": {
		Field:           "name",
		CanonifiedField: "canonified_name",
		Parent:          &Parent{Collection: "mobile_runners", Field: "runner"},
		PathLength:      3,
	},
}
View Source
var DefaultOptions = CanonifyOptions{
	Separator:   '-',
	MinLen:      1,
	Fallback:    "item-name",
	MaxAttempts: 1000000,
}
View Source
var ErrExhaustedAttempts = errors.New("could not find unique name: exhausted attempts")

ErrExhaustedAttempts returned when we couldn't find a free name within MaxAttempts.

Functions

func BuildPath

func BuildPath(
	app core.App,
	rec *core.Record,
	tpl PathTemplate,
	candidateName string,
) (string, error)

BuildPath constructs the path for a record

func Canonify

func Canonify(in string, exists ExistsFunc) (string, error)

Canonify converts an arbitrary input string into a deterministic, compact, lowercased, ASCII-friendly slug and ensures uniqueness by consulting exists(name).

The exists callback will be called for each candidate (base name and sequential suffixes).

func CanonifyPlain

func CanonifyPlain(in string) string

CanonifyPlain computes the canonical string without checking uniqueness.

func CanonifyWithOptions

func CanonifyWithOptions(in string, exists ExistsFunc, opts CanonifyOptions) (string, error)

CanonifyWithOptions same as Canonify but with configurable options.

func NormalizePath

func NormalizePath(path string) string

func RegisterCanonifyHooks

func RegisterCanonifyHooks(app core.App)

RegisterCanonifyHooks registers hooks for canonifying names in specified collections.

For each collection, it registers two hooks: one for after creating a record, and one for after updating a record. Both hooks canonify the name of the record using the provided function, and save the updated record to persist the canonified field. The canonified field is named "canonified_name" or "canonified_title" depending on the source field name. The existsFunc parameter is used to check if a canonized name already exists in the collection. If the canonized name already exists, the function returns an error. If the source field is empty, the hooks skip the record and do not perform any action.

The function takes a PocketBase application as a parameter, and registers the hooks for the specified collections.

func Resolve

func Resolve(app core.App, path string) (*core.Record, error)

func Validate

func Validate(app core.App, path string) (*core.Record, error)

Types

type CanonifyOptions

type CanonifyOptions struct {
	Separator   rune
	MinLen      int
	Fallback    string
	MaxAttempts int
}

CanonifyOptions controls behaviour of Canonify.

type ExistsFunc

type ExistsFunc func(name string) (bool, error)

ExistsFunc is the callback used to ask if a candidate already exists in DB. Should be deterministic. A non-nil error means the lookup itself failed; Canonify returns it instead of trying further candidates.

func MakeExistsFunc

func MakeExistsFunc(
	app core.App,
	collectionName string,
	rec *core.Record,
	excludeID string,
) ExistsFunc

MakeExistsFunc reports whether candidateName is already used by another record of collectionName under the same parent path. It returns an error when the path cannot be built (for example a missing parent record) or the lookup fails, because no other candidate name can succeed then.

type Parent

type Parent struct {
	Collection string
	Field      string
}

type PathTemplate

type PathTemplate struct {
	Field           string
	CanonifiedField string
	Parent          *Parent
	PathLength      int
}

Jump to

Keyboard shortcuts

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