...
1 package urn
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 )
8
9 const errInvalidURN = "invalid URN: %s"
10
11
12
13
14
15
16
17
18 type URN struct {
19 prefix string
20 ID string
21 SS string
22 norm string
23 kind Kind
24 scim *SCIM
25 rComponent string
26 qComponent string
27 fComponent string
28 rStart bool
29 qStart bool
30 tolower []int
31 }
32
33
34
35
36 func (u *URN) Normalize() *URN {
37 return &URN{
38 prefix: "urn",
39 ID: strings.ToLower(u.ID),
40 SS: u.norm,
41
42
43
44 }
45 }
46
47
48 func (u *URN) Equal(x *URN) bool {
49 if x == nil {
50 return false
51 }
52 nu := u.Normalize()
53 nx := x.Normalize()
54
55 return nu.prefix == nx.prefix && nu.ID == nx.ID && nu.SS == nx.SS
56 }
57
58
59
60
61
62
63
64 func (u *URN) String() string {
65 var res string
66 if u.ID != "" && u.SS != "" {
67 if u.prefix == "" {
68 res += "urn"
69 }
70 res += u.prefix + ":" + u.ID + ":" + u.SS
71 if u.rComponent != "" {
72 res += "?+" + u.rComponent
73 }
74 if u.qComponent != "" {
75 res += "?=" + u.qComponent
76 }
77 if u.fComponent != "" {
78 res += "#" + u.fComponent
79 }
80 }
81
82 return res
83 }
84
85
86 func Parse(u []byte, options ...Option) (*URN, bool) {
87 urn, err := NewMachine(options...).Parse(u)
88 if err != nil {
89 return nil, false
90 }
91
92 return urn, true
93 }
94
95
96 func (u URN) MarshalJSON() ([]byte, error) {
97 return json.Marshal(u.String())
98 }
99
100
101 func (u *URN) UnmarshalJSON(bytes []byte) error {
102 var str string
103 if err := json.Unmarshal(bytes, &str); err != nil {
104 return err
105 }
106 if value, ok := Parse([]byte(str)); !ok {
107 return fmt.Errorf(errInvalidURN, str)
108 } else {
109 *u = *value
110 }
111
112 return nil
113 }
114
115 func (u *URN) IsSCIM() bool {
116 return u.kind == RFC7643
117 }
118
119 func (u *URN) SCIM() *SCIM {
120 if u.kind != RFC7643 {
121 return nil
122 }
123
124 return u.scim
125 }
126
127 func (u *URN) RFC() Kind {
128 return u.kind
129 }
130
131 func (u *URN) FComponent() string {
132 return u.fComponent
133 }
134
135 func (u *URN) QComponent() string {
136 return u.qComponent
137 }
138
139 func (u *URN) RComponent() string {
140 return u.rComponent
141 }
142
View as plain text