/
util.go
78 lines (59 loc) · 1.53 KB
/
util.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"os"
"path/filepath"
"errors"
"fmt"
"strings"
)
func CreateBasePath(filename string) error {
basePath := filepath.Dir(filename)
if err := os.MkdirAll(basePath, 0700); err != nil {
return errors.New(fmt.Sprintf("Creating base path %s: %s", basePath, err))
}
return nil
}
func GetImportBasePath(absolutePath string) (string, error) {
n := strings.LastIndex(absolutePath, "/src/")
if n == -1 {
return "", errors.New(fmt.Sprintf("Path /src/ not found in %s", absolutePath))
}
return absolutePath[n+5:], nil
}
func GetCommonImportPath(outputDir string, serviceName string) (string, error) {
baseSourceDir := outputDir + "/"
absPath, err := filepath.Abs(baseSourceDir)
if err != nil {
return "", errors.New(fmt.Sprintf("When get abs(%s): %s", baseSourceDir, err))
}
importBasePath, err := GetImportBasePath(absPath)
if err != nil {
return "", errors.New(fmt.Sprintf("When getting import path from %s: %s", absPath, err))
}
commonImportPath := importBasePath + "/" + serviceName + "_common"
return commonImportPath, nil
}
func GetPathWithoutParameter(path string) string {
// Input example: "/get_method_4/{par1}"
// Result: "/get_method_4"
n := strings.Index(path, "/{")
if n == -1 {
return path
}
result := path[:n]
return result
}
func CamelToSnake(text string) string {
result := ""
for _, chr := range text {
if chr >= 'A' && chr <= 'Z' {
if result != "" {
result += "_"
}
result += string(chr + ('a' - 'A'))
} else {
result += string(chr)
}
}
return result
}