-
Notifications
You must be signed in to change notification settings - Fork 0
/
reference.go
56 lines (45 loc) · 1.11 KB
/
reference.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
package malak
import (
"crypto/rand"
"encoding/base64"
"fmt"
gonanoid "github.com/matoous/go-nanoid/v2"
)
// DEPRECATED
func GenerateReference(e EntityType) string {
return fmt.Sprintf("%s_%s", e.String(), gonanoid.Must())
}
// ENUM(
// workspace,invoice,
// team,invite,contact,
// update,link,room,
// recipient,schedule,list,
// list_email, update_stat,
// recipient_stat,recipient_log,
// deck,deck_preference)
type EntityType string
type Reference string
func (r Reference) String() string { return string(r) }
type ReferenceGeneratorOperation interface {
Generate(EntityType) Reference
ShortLink() string
}
type ReferenceGenerator struct{}
func NewReferenceGenerator() *ReferenceGenerator {
return &ReferenceGenerator{}
}
func (r *ReferenceGenerator) Generate(e EntityType) Reference {
return Reference(
fmt.Sprintf(
"%s_%s",
e.String(),
gonanoid.Must()))
}
func (r *ReferenceGenerator) ShortLink() string {
b := make([]byte, 8) // 6 bytes = 8 characters in base64
_, err := rand.Read(b)
if err != nil {
panic(err)
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString(b)
}