go-urn – github.com/leodido/go-urn Index | Examples | Files

package urn

import "github.com/leodido/go-urn"

Index

Examples

Types

type Machine

type Machine interface {
	Error() error
	Parse(input []byte) (*URN, error)
}

Machine is the interface representing the FSM

func NewMachine

func NewMachine() Machine

NewMachine creates a new FSM able to parse RFC 2141 strings.

type URN

type URN struct {
	ID string // Namespace identifier
	SS string // Namespace specific string
	// contains filtered or unexported fields
}

URN represents an Uniform Resource Name.

The general form represented is:

urn:<id>:<ss>

Details at https://tools.ietf.org/html/rfc2141.

func Parse

func Parse(u []byte) (*URN, bool)

Parse is responsible to create an URN instance from a byte array matching the correct URN syntax.

Example

Code:

{
	var uid = "URN:foo:a123,456"

	if u, ok := urn.Parse([]byte(uid)); ok {
		fmt.Println(u.ID)
		fmt.Println(u.SS)
	}

	// Output: foo
	// a123,456
}

Output:

foo
a123,456

func (*URN) Equal

func (u *URN) Equal(x *URN) bool

Equal checks the lexical equivalence of the current URN with another one.

Example

Code:

{
	var uid1 = "URN:foo:a123,456"
	var uid2 = "URN:FOO:a123,456"

	u1, ok := urn.Parse([]byte(uid1))
	if !ok {
		panic("invalid urn")
	}

	u2, ok := urn.Parse([]byte(uid2))
	if !ok {
		panic("invalid urn")
	}

	if u1.Equal(u2) {
		fmt.Printf("%s equals %s", u1.String(), u2.String())
	}

	// Output: URN:foo:a123,456 equals URN:FOO:a123,456
}

Output:

URN:foo:a123,456 equals URN:FOO:a123,456

func (URN) MarshalJSON

func (u URN) MarshalJSON() ([]byte, error)

MarshalJSON marshals the URN to JSON string form (e.g. `"urn:oid:1.2.3.4"`).

Example

Code:

{
	var uid = "URN:foo:a123,456"

	if u, ok := urn.Parse([]byte(uid)); ok {
		json, err := u.MarshalJSON()
		if err != nil {
			panic("invalid urn")
		}
		fmt.Println(string(json))
	}

	// Output: "URN:foo:a123,456"
}

Output:

"URN:foo:a123,456"

func (*URN) Normalize

func (u *URN) Normalize() *URN

Normalize turns the receiving URN into its norm version.

Which means: lowercase prefix, lowercase namespace identifier, and immutate namespace specific string chars (except <hex> tokens which are lowercased).

func (*URN) String

func (u *URN) String() string

String reassembles the URN into a valid URN string.

This requires both ID and SS fields to be non-empty. Otherwise it returns an empty string.

Default URN prefix is "urn".

func (*URN) UnmarshalJSON

func (u *URN) UnmarshalJSON(bytes []byte) error

MarshalJSON unmarshals a URN from JSON string form (e.g. `"urn:oid:1.2.3.4"`).

Source Files

machine.go urn.go

Version
v1.2.3
Published
Apr 5, 2023
Platform
js/wasm
Imports
3 packages
Last checked
now

Tools for package owners.