-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpressions.go
More file actions
43 lines (36 loc) · 946 Bytes
/
expressions.go
File metadata and controls
43 lines (36 loc) · 946 Bytes
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
package main
import (
"fmt"
"regexp"
)
var (
RegexDate string = `\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d(?:\.\d+)?Z?`
RegexUUID string = `[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}`
)
type Replacer struct {
Expression string
Generator func() string
}
func ApplyReplacer(replacer Replacer, text string) string {
re := regexp.MustCompile(replacer.Expression)
textReplaced := re.ReplaceAllFunc([]byte(text), func(b []byte) []byte {
value := replacer.Generator()
return []byte(value)
})
return string(textReplaced)
}
func GetReplacers(replacerType string) (Replacer, error) {
if replacerType == "date" {
return Replacer{
Expression: RegexDate,
Generator: GetRandomDate,
}, nil
}
if replacerType == "uuid" {
return Replacer{
Expression: RegexUUID,
Generator: GetRandomUUID,
}, nil
}
return Replacer{}, fmt.Errorf("invalid replacer type passed %s", replacerType)
}