rid

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package rid generates resource identifiers.

Format: {prefix}_{base62_id}

Default length is 12 base62 chars → 3.2×10²¹ space. Prefix constraints: 1–10 chars, [a-z][a-z0-9]*, total RID length ≤ 23.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasPrefix

func HasPrefix(rid, expect string) bool

HasPrefix checks whether a RID starts with the given prefix followed by '_'.

func New

func New(prefix string) string

New generates a prefixed RID: "{prefix}_{12-char base62}". Panics if prefix is invalid.

func NewRaw

func NewRaw() string

NewRaw generates a 12-char base62 string without prefix.

func NewWithLength

func NewWithLength(prefix string, n int) string

NewWithLength generates a prefixed RID with custom ID length. Panics if prefix is invalid or total length exceeds 23.

func Parse

func Parse(rid string) (prefix, id string, err error)

Parse splits a RID into prefix and id parts. Returns error if the RID has no underscore separator.

func Prefix

func Prefix(rid string) string

Prefix extracts the prefix from a RID. Returns empty string if no underscore is found.

func ValidatePrefix

func ValidatePrefix(prefix string, idLen int) error

ValidatePrefix checks prefix constraints without generating an ID. idLen is the intended random part length (used for total-length check). Returns nil if valid, error describing the problem otherwise.

func ValidateRID

func ValidateRID(s string) error

ValidateRID checks whether a full RID conforms to the schema:

  • total length 1–maxRIDLen
  • shape either "id" (no prefix) or "prefix_id" where prefix is valid per ValidatePrefix and id is at least one character of [A-Za-z0-9]

Used by callers (e.g. db.Model.BeforeCreate) to reject malformed user-supplied RIDs before they reach the database.

Types

This section is empty.

Jump to

Keyboard shortcuts

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