...

Package urn

import "github.com/leodido/go-urn"
Overview
Index
Examples
Subdirectories

Overview ▾

Constants

const DefaultParsingMode = RFC2141Only

type Kind

type Kind int
const (
    NONE Kind = iota
    RFC2141
    RFC7643
    RFC8141
)

type Machine

Machine is the interface representing the FSM

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

func NewMachine

func NewMachine(options ...Option) Machine

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

type Option

type Option func(Machine)

func WithParsingMode

func WithParsingMode(mode ParsingMode) Option

type ParsingMode

type ParsingMode int
const (
    Default ParsingMode = iota
    RFC2141Only
    RFC7643Only
    RFC8141Only
)

type SCIM

type SCIM struct {
    Type  scimschema.Type
    Name  string
    Other string
    // contains filtered or unexported fields
}

func (SCIM) MarshalJSON

func (s SCIM) MarshalJSON() ([]byte, error)

func (*SCIM) String

func (s *SCIM) String() string

func (*SCIM) UnmarshalJSON

func (s *SCIM) UnmarshalJSON(bytes []byte) error

type URN

URN represents an Uniform Resource Name.

The general form represented is:

urn:<id>:<ss>

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

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

func Parse

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

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

Example

Code:

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

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

Output:

foo
a123,456
<nil>

Example (Scim)

Code:

input := "urn:ietf:params:scim:api:messages:2.0:ListResponse"

u, ok := urn.Parse([]byte(input), urn.WithParsingMode(urn.RFC7643Only))
if !ok {
    panic("invalid SCIM urn")
}
data, err := u.MarshalJSON()
if err != nil {
    panic("couldn't marshal")
}
fmt.Println(string(data))
fmt.Println(u.IsSCIM())
scim := u.SCIM()
fmt.Println(scim.Type.String())
fmt.Println(scim.Name)
fmt.Println(scim.Other)

Output:

"urn:ietf:params:scim:api:messages:2.0:ListResponse"
true
api
messages
2.0:ListResponse

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

func (*URN) FComponent

func (u *URN) FComponent() string

func (*URN) IsSCIM

func (u *URN) IsSCIM() bool

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"

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) QComponent

func (u *URN) QComponent() string

func (*URN) RComponent

func (u *URN) RComponent() string

func (*URN) RFC

func (u *URN) RFC() Kind

func (*URN) SCIM

func (u *URN) SCIM() *SCIM

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

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

type URN8141

type URN8141 struct {
    *URN
}

func (URN8141) MarshalJSON

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

func (*URN8141) UnmarshalJSON

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

Subdirectories

Name Synopsis
..
scim
schema