forked from juju/names
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag.go
123 lines (113 loc) · 3 KB
/
tag.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package names
import (
"fmt"
"strings"
)
// A Tag tags things that are taggable.
type Tag interface {
// Kind returns the kind of the tag.
// This method is for legacy compatibility, callers should
// use equality or type assertions to verify the Kind, or type
// of a Tag.
Kind() string
// Id returns an identifier for this Tag.
// The contents and format of the identifier are specific
// to the implementer of the Tag.
Id() string
fmt.Stringer // all Tags should be able to print themselves
}
// TagKind returns one of the *TagKind constants for the given tag, or
// an error if none matches.
func TagKind(tag string) (string, error) {
i := strings.Index(tag, "-")
if i <= 0 || !validKinds(tag[:i]) {
return "", fmt.Errorf("%q is not a valid tag", tag)
}
return tag[:i], nil
}
func validKinds(kind string) bool {
switch kind {
case UnitTagKind, MachineTagKind, ServiceTagKind, EnvironTagKind, UserTagKind, RelationTagKind, NetworkTagKind, ActionTagKind, DiskTagKind, CharmTagKind:
return true
}
return false
}
func splitTag(tag string) (string, string, error) {
kind, err := TagKind(tag)
if err != nil {
return "", "", err
}
return kind, tag[len(kind)+1:], nil
}
// ParseTag parses a string representation into a Tag.
func ParseTag(tag string) (Tag, error) {
kind, id, err := splitTag(tag)
if err != nil {
return nil, invalidTagError(tag, "")
}
switch kind {
case UnitTagKind:
id = unitTagSuffixToId(id)
if !IsValidUnit(id) {
return nil, invalidTagError(tag, kind)
}
return NewUnitTag(id), nil
case MachineTagKind:
id = machineTagSuffixToId(id)
if !IsValidMachine(id) {
return nil, invalidTagError(tag, kind)
}
return NewMachineTag(id), nil
case ServiceTagKind:
if !IsValidService(id) {
return nil, invalidTagError(tag, kind)
}
return NewServiceTag(id), nil
case UserTagKind:
if !IsValidUser(id) {
return nil, invalidTagError(tag, kind)
}
return NewUserTag(id), nil
case EnvironTagKind:
if !IsValidEnvironment(id) {
return nil, invalidTagError(tag, kind)
}
return NewEnvironTag(id), nil
case RelationTagKind:
id = relationTagSuffixToKey(id)
if !IsValidRelation(id) {
return nil, invalidTagError(tag, kind)
}
return NewRelationTag(id), nil
case NetworkTagKind:
if !IsValidNetwork(id) {
return nil, invalidTagError(tag, kind)
}
return NewNetworkTag(id), nil
case ActionTagKind:
if !IsValidAction(id) {
return nil, invalidTagError(tag, kind)
}
return NewActionTag(id), nil
case DiskTagKind:
if !IsValidDisk(id) {
return nil, invalidTagError(tag, kind)
}
return NewDiskTag(id), nil
case CharmTagKind:
if !IsValidCharm(id) {
return nil, invalidTagError(tag, kind)
}
return NewCharmTag(id), nil
default:
return nil, invalidTagError(tag, "")
}
}
func invalidTagError(tag, kind string) error {
if kind != "" {
return fmt.Errorf("%q is not a valid %s tag", tag, kind)
}
return fmt.Errorf("%q is not a valid tag", tag)
}