-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathspace.go
50 lines (41 loc) · 1.13 KB
/
space.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
// Copyright 2015 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package names
import (
"fmt"
"regexp"
)
const (
SpaceTagKind = "space"
SpaceSnippet = "(?:[a-z0-9]+(?:-[a-z0-9]+)*)"
)
var validSpace = regexp.MustCompile("^" + SpaceSnippet + "$")
// IsValidSpace reports whether name is a valid space name.
func IsValidSpace(name string) bool {
return validSpace.MatchString(name)
}
type SpaceTag struct {
name string
}
func (t SpaceTag) String() string { return t.Kind() + "-" + t.Id() }
func (t SpaceTag) Kind() string { return SpaceTagKind }
func (t SpaceTag) Id() string { return t.name }
// NewSpaceTag returns the tag of a space with the given name.
func NewSpaceTag(name string) SpaceTag {
if !IsValidSpace(name) {
panic(fmt.Sprintf("%q is not a valid space name", name))
}
return SpaceTag{name: name}
}
// ParseSpaceTag parses a space tag string.
func ParseSpaceTag(spaceTag string) (SpaceTag, error) {
tag, err := ParseTag(spaceTag)
if err != nil {
return SpaceTag{}, err
}
nt, ok := tag.(SpaceTag)
if !ok {
return SpaceTag{}, invalidTagError(spaceTag, SpaceTagKind)
}
return nt, nil
}