/
randomstr.go
40 lines (31 loc) · 874 Bytes
/
randomstr.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
package function
import (
"math/rand"
"time"
"github.com/ysugimoto/vintage/runtime/core"
)
const Randomstr_Name = "randomstr"
var Randomstr_default_characters = []rune(
"abcdedfhijklmnopqrstuvwxyzABCDEDFHIJKLMNOPQRSTUVWXYZ0123456789-_",
)
// Fastly built-in function implementation of randomstr
// Arguments may be:
// - INTEGER
// - INTEGER, STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/randomness/randomstr/
func Randomstr[T core.EdgeRuntime](
ctx *core.Runtime[T],
length int64,
optional ...string,
) (string, error) {
characters := Randomstr_default_characters
if len(optional) > 0 {
characters = []rune(optional[0])
}
rand.New(rand.NewSource(time.Now().UnixNano()))
ret := make([]rune, int(length))
for i := 0; i < int(length); i++ {
ret[i] = characters[rand.Intn(len(characters)-1)]
}
return string(ret), nil
}