@@ -10,56 +10,73 @@ import (
10
10
"crypto/sha256"
11
11
12
12
"github.com/aohorodnyk/uid"
13
- "github.com/dustinkirkland/golang-petname"
13
+ petname "github.com/dustinkirkland/golang-petname"
14
14
)
15
15
16
16
type Identifier struct {
17
- name string
18
- salt string
17
+ prefix string
18
+ name string
19
+ salt string
19
20
}
20
21
21
22
var ErrWrongIdentifier = errors .New ("wrong identifier" )
22
23
23
24
const (
24
- maximumLongLength = 50
25
- shortLength = 16
26
- nameLength = maximumLongLength - shortLength - uint32 (len ("tpi---" ))
25
+ defaultIdentifierPrefix = "tpi"
26
+ maximumLongLength = 50
27
+ shortLength = 16
28
+ nameLength = maximumLongLength - shortLength - uint32 (len ("tpi---" ))
27
29
)
28
30
31
+ // ParseIdentifier parses the string representation of the identifier.
29
32
func ParseIdentifier (identifier string ) (Identifier , error ) {
30
- re := regexp .MustCompile (`(?s)^tpi -([a-z0-9]+(?:[a-z0-9-]*[a-z0-9])?)-([a-z0-9]+)-([a-z0-9]+)$` )
33
+ re := regexp .MustCompile (`(?s)^([a-z0-9]{3}) -([a-z0-9]+(?:[a-z0-9-]*[a-z0-9])?)-([a-z0-9]+)-([a-z0-9]+)$` )
31
34
32
- if match := re .FindStringSubmatch (string (identifier )); len (match ) > 0 && hash (match [1 ]+ match [2 ], shortLength / 2 ) == match [3 ] {
33
- return Identifier {name : match [1 ], salt : match [2 ]}, nil
35
+ if match := re .FindStringSubmatch (string (identifier )); len (match ) > 0 && hash (match [2 ]+ match [3 ], shortLength / 2 ) == match [4 ] {
36
+ return Identifier {prefix : match [1 ], name : match [2 ], salt : match [ 3 ]}, nil
34
37
}
35
38
36
39
return Identifier {}, ErrWrongIdentifier
37
40
}
38
41
42
+ // NewDeterministicIdentifierWithPrefix returns a new deterministic Identifier, with
43
+ // the specified prefix, using the provided name as a seed. Repeated calls to this
44
+ // function are guaranteed to generate the same Identifier.
45
+ func NewDeterministicIdentifierWithPrefix (prefix , name string ) Identifier {
46
+ seed := normalize (name , nameLength )
47
+ return Identifier {prefix : prefix [0 :3 ], name : name , salt : hash (seed , shortLength / 2 )}
48
+ }
49
+
39
50
// NewDeterministicIdentifier returns a new deterministic Identifier, using the
40
51
// provided name as a seed. Repeated calls to this function are guaranteed to
41
52
// generate the same Identifier.
42
53
func NewDeterministicIdentifier (name string ) Identifier {
43
- seed := normalize (name , nameLength )
44
- return Identifier {name : name , salt : hash (seed , shortLength / 2 )}
54
+ return NewDeterministicIdentifierWithPrefix (defaultIdentifierPrefix , name )
45
55
}
46
56
47
- // NewRandomIdentifier returns a new random Identifier. Repeated calls to this
48
- // function are guaranteed to generate different Identifiers, as long as there
49
- // are no collisions.
50
- func NewRandomIdentifier (name string ) Identifier {
57
+ // NewRandomIdentifierWithPrefix returns a new random Identifier with the
58
+ // specified prefix. Only the first 3 symbols of the prefix are used.
59
+ // Repeated calls to this function are guaranteed to generate different
60
+ // Identifiers, as long as there are no collisions.
61
+ func NewRandomIdentifierWithPrefix (prefix , name string ) Identifier {
51
62
seed := uid .NewProvider36Size (8 ).MustGenerate ().String ()
52
63
if name == "" {
53
64
petname .NonDeterministicMode ()
54
65
name = petname .Generate (3 , "-" )
55
66
}
67
+ return Identifier {prefix : prefix [0 :3 ], name : name , salt : hash (seed , shortLength / 2 )}
68
+ }
56
69
57
- return Identifier {name : name , salt : hash (seed , shortLength / 2 )}
70
+ // NewRandomIdentifier returns a new random Identifier.
71
+ // Repeated calls to this function are guaranteed to generate different
72
+ // Identifiers, as long as there are no collisions.
73
+ func NewRandomIdentifier (name string ) Identifier {
74
+ return NewRandomIdentifierWithPrefix (defaultIdentifierPrefix , name )
58
75
}
59
76
60
77
func (i Identifier ) Long () string {
61
78
name := normalize (i .name , nameLength )
62
- return fmt .Sprintf ("tpi -%s-%s-%s" , name , i .salt , hash (name + i .salt , shortLength / 2 ))
79
+ return fmt .Sprintf ("%s -%s-%s-%s" , i . prefix , name , i .salt , hash (name + i .salt , shortLength / 2 ))
63
80
}
64
81
65
82
func (i Identifier ) Short () string {
0 commit comments