-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathoperation.go
52 lines (42 loc) · 1.4 KB
/
operation.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
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package names
import (
"fmt"
"regexp"
)
const OperationTagKind = "operation"
// OperationSnippet defines the regexp for a valid Operation Id.
// Operations are identified by a unique, incrementing number.
const OperationSnippet = NumberSnippet
var validOperation = regexp.MustCompile("^" + OperationSnippet + "$")
type OperationTag struct {
// Tags that are serialized need to have fields exported.
ID string
}
// NewOperationTag returns the tag of an operation with the given id.
func NewOperationTag(id string) OperationTag {
if !validOperation.MatchString(id) {
panic(fmt.Sprintf("invalid operation id %q", id))
}
return OperationTag{ID: id}
}
// ParseOperationTag parses an operation tag string.
func ParseOperationTag(operationTag string) (OperationTag, error) {
tag, err := ParseTag(operationTag)
if err != nil {
return OperationTag{}, err
}
at, ok := tag.(OperationTag)
if !ok {
return OperationTag{}, invalidTagError(operationTag, OperationTagKind)
}
return at, nil
}
func (t OperationTag) String() string { return t.Kind() + "-" + t.Id() }
func (t OperationTag) Kind() string { return OperationTagKind }
func (t OperationTag) Id() string { return t.ID }
// IsValidOperation returns whether id is a valid operation id.
func IsValidOperation(id string) bool {
return validOperation.MatchString(id)
}