forked from simagix/hatchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
51 lines (45 loc) · 1022 Bytes
/
utils.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
// Copyright 2022-present Kuei-chun Chen. All rights reserved.
package hatchet
import (
"fmt"
"math/rand"
"path/filepath"
"strconv"
"strings"
"time"
)
const (
MAX_SIZE = 128
TAIL_SIZE = 7
)
// ToInt converts to int
func ToInt(num interface{}) int {
f := fmt.Sprintf("%v", num)
x, err := strconv.ParseFloat(f, 64)
if err != nil {
return 0
}
return int(x)
}
func replaceSpecialChars(name string) string {
for _, sep := range []string{"-", ".", " ", ":", ","} {
name = strings.ReplaceAll(name, sep, "_")
}
return name
}
func getHatchetName(filename string) string {
temp := filepath.Base(filename)
tableName := replaceSpecialChars(temp)
i := strings.LastIndex(tableName, "_log")
if i >= 0 {
tableName = replaceSpecialChars(tableName[0:i])
}
if len(tableName) > MAX_SIZE {
tableName = tableName[:MAX_SIZE-TAIL_SIZE]
}
rand.Seed(time.Now().UnixNano())
b := make([]byte, TAIL_SIZE)
rand.Read(b)
tail := fmt.Sprintf("%x", b)[:TAIL_SIZE-1]
return fmt.Sprintf("%v_%v", tableName, tail)
}