Documentation
¶
Index ¶
- func IsComponent(s string) bool
- func Validates(s string) bool
- type Option
- type URN
- func (u *URN) Equal(o *URN) bool
- func (u *URN) FComponent() string
- func (u *URN) IsZero() bool
- func (u *URN) MarshalJSON() ([]byte, error)
- func (u *URN) QComponent() string
- func (u *URN) RComponent() string
- func (u *URN) SetFComponent(c string) error
- func (u *URN) SetQComponent(c string) error
- func (u *URN) SetRComponent(c string) error
- func (u *URN) String() string
- func (u *URN) UnmarshalJSON(data []byte) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsComponent ¶
IsComponent verifies whether s is a valid URN fragment.
func Validates ¶
Validates verifies whether s can be parsed as URN.
Example ¶
package main
import (
"fmt"
"github.com/golistic/urn"
)
func main() {
if urn.Validates("urn:ietf:rfc:8141#section-3") {
fmt.Println("valid!")
}
if !urn.Validates("urn:ie+tf:rfc:8141#section-3") {
fmt.Println("not valid!")
}
}
Output: valid! not valid!
Types ¶
type Option ¶
type Option func(*urnOptions)
Option is the functional option type used with New().
func WithFragment ¶
WithFragment is a functional option setting the f-component of the URN; the part introduced by the number sign `#` (same syntax as the URI fragment component).
func WithNotLowerCaseNSS ¶
func WithNotLowerCaseNSS() Option
WithNotLowerCaseNSS will not lower-case the NSS, but leave it as specified to New() or Parse(). Note that when check whether URNs are equivalent, the NSS is considered case-insensitive.
func WithQuery ¶
WithQuery is a functional option setting the q-component of the URN; the part introduced by `?=` (not `?` alone as is the case for a URI).
func WithResolution ¶
WithResolution is a functional option setting the r-component of the URN; the part after introduced by `?+`.
type URN ¶
type URN struct {
NID string
NSS string
// Original is only set when parsing a URN from a string using Parse.
Original string
// contains filtered or unexported fields
}
URN is the representation of a URN as defined by RFC 8141.
The syntax of a URN is: `urn:<NID>:<NSS>#fragment`. NID stands for Namespace Identifier, and NSS for Namespace Specific String.
See RFC 8141 https://tools.ietf.org/html/rfc8141
func New ¶
New returns a new instance of URN with nid as Namespace Identifier and nss as Namespace Specific String. The r-, q-, and f-components can be set through their respective functional options WithResolution, WithQuery, and WithFragment. The NSS is lower cased according the RFC. If this is not wanted, use the option WithNotLowerCaseNSS.
Example ¶
package main
import (
"fmt"
"log"
"github.com/golistic/urn"
)
func main() {
u, err := urn.New("ietf", "rfc:8141")
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s", u)
}
Output: urn:ietf:rfc:8141
func Parse ¶
Parse tries to parse s into a URN object.
Example ¶
package main
import (
"fmt"
"log"
"github.com/golistic/urn"
)
func main() {
u, err := urn.Parse("urn:ietf:rfc:8141#section-3")
if err != nil {
log.Fatalln(err)
}
fmt.Printf("%s : %v\n", u, u.Equal(&urn.URN{NID: "ietf", NSS: "rfc:8141"}))
}
Output: urn:ietf:rfc:8141#section-3 : true
func (*URN) Equal ¶
Equal reports whether o and u represent the same URN.
Example ¶
package main
import (
"fmt"
"github.com/golistic/urn"
)
func main() {
// err handling left out for sake of brevity
u, _ := urn.New("ietf", "rfc:8141")
equalNID, _ := urn.New("IETF", "rfc:8141", urn.WithNotLowerCaseNSS())
fmt.Printf("%s == %s : %v\n", u, equalNID, u.Equal(equalNID))
notEqualNSS, _ := urn.New("ietf", "RFC:8141")
fmt.Printf("%s == %s : %v\n", u, notEqualNSS, u.Equal(notEqualNSS))
// components are ignored when determining equivalence
withComponent, _ := urn.New("ietf", "rfc:8141", urn.WithFragment("section-3"))
fmt.Printf("%s == %s : %v\n", u, withComponent, u.Equal(withComponent))
}
Output: urn:ietf:rfc:8141 == urn:IETF:rfc:8141 : true urn:ietf:rfc:8141 == urn:ietf:RFC:8141 : false urn:ietf:rfc:8141 == urn:ietf:rfc:8141#section-3 : true
func (*URN) FComponent ¶
func (*URN) MarshalJSON ¶
MarshalJSON returns the JSON encoding of u.
func (*URN) QComponent ¶
func (*URN) RComponent ¶
func (*URN) SetFComponent ¶
func (*URN) SetQComponent ¶
func (*URN) SetRComponent ¶
func (*URN) UnmarshalJSON ¶
UnmarshalJSON parses the JSON-encoded data and stores the result in u.
Note that the NID and NSS are being lower-cased; optional components not. The Original field of u will contain the original.