forked from leg100/otf
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathid.go
More file actions
52 lines (45 loc) · 1.47 KB
/
id.go
File metadata and controls
52 lines (45 loc) · 1.47 KB
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
package internal
import (
"reflect"
"regexp"
"strings"
)
// ReStringID is a regular expression used to validate common string ID patterns.
var ReStringID = regexp.MustCompile(`^[a-zA-Z0-9\-\._]+$`)
// GetID retrieves the ID field of a struct contained in s. If s is not a struct,
// or there is no ID field, then false is returned.
func GetID(s any) (string, bool) {
v := reflect.Indirect(reflect.ValueOf(s))
if v.Kind() != reflect.Struct {
return "", false
}
f := v.FieldByName("ID")
if !f.IsValid() {
return "", false
}
return f.String(), true
}
// base58 alphabet
var base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// NewID constructs resource IDs, composed of:
// (1) a symbol representing a resource type, e.g. "ws" for workspaces
// (2) a hyphen
// (3) a 16 character string composed of random characters from the base58 alphabet
func NewID(rtype string) string {
return rtype + "-" + GenerateRandomStringFromAlphabet(16, base58)
}
// ValidStringID checks if the given string pointer is non-nil and
// contains a typical string identifier.
func ValidStringID(v *string) bool {
return v != nil && ReStringID.MatchString(*v)
}
// ConvertID converts an ID for use with a different resource, e.g. convert
// run-123 to plan-123.
func ConvertID(id, resource string) string {
parts := strings.Split(id, "-")
// if ID not in expected form then just return it unchanged without error
if len(parts) != 2 {
return id
}
return resource + "-" + parts[1]
}