-
Notifications
You must be signed in to change notification settings - Fork 52
/
urn.go
79 lines (67 loc) · 1.59 KB
/
urn.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
76
77
78
79
package urn
import (
"errors"
"fmt"
"strings"
)
var ErrInvalidURN = errors.New("invalid URN")
const Namespace = "opni"
type UpdateType string
const (
Plugin UpdateType = "plugin"
Agent UpdateType = "agent"
)
func AllUpdateTypes() []UpdateType {
return []UpdateType{Agent, Plugin}
}
type OpniURN struct {
Namespace string
Type UpdateType
Strategy string
Component string
}
func ParseString(urn string) (OpniURN, error) {
splitURN := strings.Split(urn, ":")
if len(splitURN) != 5 {
return OpniURN{}, fmt.Errorf("%w: incorrect number of fields", ErrInvalidURN)
}
u := OpniURN{
Namespace: splitURN[1],
Type: UpdateType(splitURN[2]),
Strategy: splitURN[3],
Component: splitURN[4],
}
if err := u.Validate(); err != nil {
return OpniURN{}, err
}
return u, nil
}
func (u OpniURN) Validate() error {
if u.Namespace == "" {
return fmt.Errorf("%w: missing namespace", ErrInvalidURN)
}
if u.Namespace != Namespace {
return fmt.Errorf("%w: invalid namespace: %s", ErrInvalidURN, u.Namespace)
}
if u.Type == "" {
return fmt.Errorf("%w: missing type", ErrInvalidURN)
}
if u.Strategy == "" {
return fmt.Errorf("%w: missing strategy", ErrInvalidURN)
}
if u.Component == "" {
return fmt.Errorf("%w: missing component", ErrInvalidURN)
}
return nil
}
func NewOpniURN(updateType UpdateType, strategy, component string) OpniURN {
return OpniURN{
Namespace: Namespace,
Type: updateType,
Strategy: strategy,
Component: component,
}
}
func (u OpniURN) String() string {
return fmt.Sprintf("urn:%s:%s:%s:%s", u.Namespace, u.Type, u.Strategy, u.Component)
}