-
Notifications
You must be signed in to change notification settings - Fork 89
/
nsid.go
75 lines (65 loc) · 1.98 KB
/
nsid.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package syntax
import (
"fmt"
"regexp"
"strings"
)
var nsidRegex = regexp.MustCompile(`^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z]{0,61}[a-zA-Z])?)$`)
// String type which represents a syntaxtually valid Namespace Identifier (NSID), as would pass Lexicon syntax validation.
//
// Always use [ParseNSID] instead of wrapping strings directly, especially when working with input.
//
// Syntax specification: https://atproto.com/specs/nsid
type NSID string
func ParseNSID(raw string) (NSID, error) {
if len(raw) > 317 {
return "", fmt.Errorf("NSID is too long (317 chars max)")
}
if !nsidRegex.MatchString(raw) {
return "", fmt.Errorf("NSID syntax didn't validate via regex")
}
return NSID(raw), nil
}
// Authority domain name, in regular DNS order, not reversed order, normalized to lower-case.
func (n NSID) Authority() string {
parts := strings.Split(string(n), ".")
if len(parts) < 2 {
// something has gone wrong (would not validate); return empty string instead
return ""
}
// NSID must have at least two parts, verified by ParseNSID
parts = parts[:len(parts)-1]
// reverse
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
parts[i], parts[j] = parts[j], parts[i]
}
return strings.ToLower(strings.Join(parts, "."))
}
func (n NSID) Name() string {
parts := strings.Split(string(n), ".")
return parts[len(parts)-1]
}
func (n NSID) String() string {
return string(n)
}
func (n NSID) Normalize() NSID {
parts := strings.Split(string(n), ".")
if len(parts) < 2 {
// something has gone wrong (would not validate); just return the whole identifier
return n
}
name := parts[len(parts)-1]
prefix := strings.ToLower(strings.Join(parts[:len(parts)-1], "."))
return NSID(prefix + "." + name)
}
func (n NSID) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
func (n *NSID) UnmarshalText(text []byte) error {
nsid, err := ParseNSID(string(text))
if err != nil {
return err
}
*n = nsid
return nil
}