forked from aau-network-security/haaukins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
62 lines (48 loc) · 1.08 KB
/
store.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
// Copyright (c) 2018-2019 Aalborg University
// Use of this source code is governed by a GPLv3
// license that can be found in the LICENSE file.
package store
import (
"errors"
"fmt"
"regexp"
)
var (
tagRawRegexp = `^[a-z0-9][a-z0-9-]*[a-z0-9]$`
tagRegex = regexp.MustCompile(tagRawRegexp)
TagEmptyErr = errors.New("Tag cannot be empty")
)
type InvalidTagSyntaxErr struct {
tag string
}
func (ite *InvalidTagSyntaxErr) Error() string {
return fmt.Sprintf("Invalid syntax for tag \"%s\", allowed syntax: %s", ite.tag, tagRawRegexp)
}
type EmptyVarErr struct {
Var string
Type string
}
func (eve *EmptyVarErr) Error() string {
if eve.Type == "" {
return fmt.Sprintf("%s cannot be empty", eve.Var)
}
return fmt.Sprintf("%s cannot be empty for %s", eve.Var, eve.Type)
}
type Tag string
func NewTag(s string) (Tag, error) {
t := Tag(s)
if err := t.Validate(); err != nil {
return "", err
}
return t, nil
}
func (t Tag) Validate() error {
s := string(t)
if s == "" {
return TagEmptyErr
}
if !tagRegex.MatchString(s) {
return &InvalidTagSyntaxErr{s}
}
return nil
}