-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.go
More file actions
46 lines (36 loc) · 776 Bytes
/
helpers.go
File metadata and controls
46 lines (36 loc) · 776 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
44
45
46
package stdapi
import (
"crypto/rand"
"crypto/sha1"
"fmt"
"reflect"
"runtime"
"strings"
"github.com/pkg/errors"
)
func coalesce(ss ...string) string {
for _, s := range ss {
if s != "" {
return s
}
}
return ""
}
func functionName(fn interface{}) string {
sig := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
parts := strings.Split(sig, ".")
name := parts[len(parts)-1]
name = strings.TrimSuffix(name, "-fm")
return name
}
func generateId(length int) (string, error) {
data := make([]byte, 1024)
if _, err := rand.Read(data); err != nil {
return "", errors.WithStack(err)
}
key := fmt.Sprintf("%x", sha1.Sum(data))
if len(key) < length {
return "", errors.WithStack(fmt.Errorf("key too long"))
}
return key[0:length], nil
}