Documentation
¶
Overview ¶
This file implements namespace-specific URN validation via a recursive continuation-passing validator chain.
Validator chain ¶
The core abstraction is NssElementValidator — a function with the signature:
func(nss []string) (remainder []string, next *NssSchema, err error)
Each validator processes the head of the NSS slice and returns the unconsumed tail plus the NssSchema to use for the next element. Returning next == nil signals that no further elements are expected. This is a continuation-passing style: the schema structure is a linked list of validators built at definition time, and validate() walks it recursively at validation time.
OR branching ¶
ComplexOrNssElementValidatorFunc implements branching by trying each alternative NssSchema in order and returning on the first success. It does not backtrack within a branch — once a validator succeeds it commits. This linear scan is sufficient because URN sub-namespaces are typically keyed on a fixed first element (e.g. "rfc", "params"), making branches mutually exclusive in practice.
SimpleOrNssElementValidatorFunc is an optimised variant for fixed-string branching backed by a map lookup (O(1)) rather than linear scan.
Termination ¶
validate() enforces exact element consumption: if next == nil but remainder is non-empty, validation fails with "too many nss elements". If next != nil but remainder is empty, it fails with "not enough nss elements". This ensures schemas are fully structural — every element must be accounted for.
Package urnfield parses, formats, and validates URNs (Uniform Resource Names) per RFC 8141.
Parsing ¶
Parse uses a single anchored regex (Pattern) to capture the five URN components in one pass: NID, NSS, query (?=), resolvers (?+), and fragment (#). The NSS capture is then split into a []string by scanning for ":" or "/" delimiters — whichever appears first is used exclusively (mixed delimiters are not supported). The chosen delimiter is recorded in NssSlashDelimiter for faithful round-trip formatting. Query and resolver components are parsed as "&"-delimited key=value pairs; keys without a "=" are stored with an empty value slice.
Formatting ¶
Format is the inverse of Parse. Query and resolver map keys are sorted alphabetically before output. The URN spec does not require a key order, but sorting makes the output deterministic — without it, map iteration order would make round-trip equality tests unreliable. The sort is applied in writeKeyValuesMap and is the only place the library intentionally diverges from strict spec neutrality.
Validation ¶
Structural validation (well-formedness) is handled by IsWellFormed. Namespace- specific validation is provided separately via the Schema type in schema.go.
Index ¶
- Constants
- type NssElementValidator
- func ComplexOrNssElementValidatorFunc(alternatives []*NssSchema) NssElementValidator
- func EqualsNssElementValidatorFunc(nssEquals string, next *NssSchema) NssElementValidator
- func GlobNssElementValidatorFunc(glob glob.Glob) NssElementValidator
- func RegexNssElementValidatorFunc(pattern *regexp.Regexp, next *NssSchema) NssElementValidator
- func SimpleOrNssElementValidatorFunc(alternatives map[string]*NssSchema) NssElementValidator
- type NssSchema
- type Schema
- type Urn
Constants ¶
const Pattern = `` /* 145-byte string literal not displayed */
Pattern is the regex used to parse a complete URN string into its components. It is anchored with ^ and $ so it matches the full input string only; strings with surrounding content will not match.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type NssElementValidator ¶
NssElementValidator is a function that validates one or more NSS elements. It returns the remaining unprocessed elements and the NssSchema to use for the next element, or a non-nil error if validation fails. When no further elements are expected, next is nil and nssRemainder should be empty.
func ComplexOrNssElementValidatorFunc ¶
func ComplexOrNssElementValidatorFunc(alternatives []*NssSchema) NssElementValidator
ComplexOrNssElementValidatorFunc returns an NssElementValidator that tries each of the provided NssSchemas in order, returning the result of the first one that succeeds. Use this when alternatives need their own validation logic beyond a fixed string match (e.g. "rfc" followed by digits, vs "params" followed by an opaque glob). For simple fixed-string branching use SimpleOrNssElementValidatorFunc instead.
func EqualsNssElementValidatorFunc ¶
func EqualsNssElementValidatorFunc(nssEquals string, next *NssSchema) NssElementValidator
EqualsNssElementValidatorFunc returns an NssElementValidator that requires the current NSS element to equal nssEquals exactly. The value operates on a single pre-split element — do not include ":" or "/" in nssEquals.
func GlobNssElementValidatorFunc ¶
func GlobNssElementValidatorFunc(glob glob.Glob) NssElementValidator
GlobNssElementValidatorFunc returns an NssElementValidator that matches all remaining NSS elements (joined with ":") against the given glob pattern. This validator always terminates the chain — it consumes every remaining element in one match, so no next NssSchema is accepted. This is intentional: glob patterns are used for opaque or arbitrarily deep sub-namespaces (e.g. the IETF "params" sub-namespace) where per-element structure is not defined. For single-element pattern matching use RegexNssElementValidatorFunc instead. See https://github.com/gobwas/glob for pattern syntax.
func RegexNssElementValidatorFunc ¶
func RegexNssElementValidatorFunc(pattern *regexp.Regexp, next *NssSchema) NssElementValidator
RegexNssElementValidatorFunc returns an NssElementValidator that matches the current NSS element against the given compiled regex pattern. Patterns operate on individual pre-split elements — do not include ":" or "/" in the pattern, as these delimiters are consumed by Parse before validation.
func SimpleOrNssElementValidatorFunc ¶
func SimpleOrNssElementValidatorFunc(alternatives map[string]*NssSchema) NssElementValidator
SimpleOrNssElementValidatorFunc returns an NssElementValidator that matches the current NSS element against a map of allowed string values, each mapping to the next NssSchema to use. Use this when each alternative is a fixed string (e.g. "rfc", "fyi", "std"). For alternatives that require their own sub-validation logic, use ComplexOrNssElementValidatorFunc instead.
type NssSchema ¶
type NssSchema struct {
Description string
ElementValidator NssElementValidator
}
NssSchema defines the validation rules for a set of NSS elements.
type Schema ¶
Schema defines a valid URN in a specific namespace. Note that Schema does not validate the query, resolvers, or fragment components.
func (*Schema) ValidateUrn ¶
ValidateUrn validates a parsed Urn against the schema.
type Urn ¶
type Urn struct {
// Nid is the Namespace Identifier.
Nid string
// NssSlashDelimiter indicates if this URN uses "/" as the NSS delimiter instead of ":".
NssSlashDelimiter bool
// Nss holds the Namespace-Specific String elements in order.
Nss []string
// Query holds the query component ("?=") if one exists.
Query map[string][]string
// Resolvers holds the resolvers component ("?+") if one exists.
Resolvers map[string][]string
// Fragment holds the fragment component ("#") if one exists.
Fragment string
}
Urn represents a parsed URN see https://tools.ietf.org/html/rfc8141
func Parse ¶
Parse parses a complete URN string and returns the parsed Urn struct or an error. The input must be a standalone URN — strings with surrounding content will not match. If *any* NSS separators are "/" then NssSlashDelimiter will be true.
func (Urn) Format ¶
Format formats the Urn as a URN string per RFC 8141. Returns an error if the Urn is not well-formed. If NssSlashDelimiter is true, all NSS delimiters will be "/" instead of ":".
func (*Urn) IsWellFormed ¶
IsWellFormed reports whether u is well-formed per RFC 8141, returning a descriptive error if not. Returns an error if u is nil.