-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
64 lines (56 loc) · 2.14 KB
/
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
52
53
54
55
56
57
58
59
60
61
62
63
64
package jsontogo
import (
"bytes"
"reflect"
"regexp"
"strings"
)
const (
camelCaseReplace = "_"
replaceString = "_"
replaceFirstLetter = "A"
noNameStruct = "NameEmpty"
)
var firstLetterValid = regexp.MustCompile(`[A-Za-z_]`)
var nonValidIdentifiers = regexp.MustCompile(`[^A-Za-z0-9_]`)
var tagNonValidIdentifiers = regexp.MustCompile(`[^A-Za-z0-9_-]`)
// toGoStructCorrectName converts structName to a correct go struct name.
func toGoStructCorrectName(structName string) string {
return toGoFieldCorrectName(structName)
}
// toGoFieldCorrectName converts fieldName to a correct go field name.
func toGoFieldCorrectName(fieldName string) string {
correctFieldName := nonValidIdentifiers.ReplaceAllString(fieldName, replaceString) // Remove non valid identifiers
correctFieldNameBuffer := bytes.Buffer{}
correctFieldNameCamelCase := strings.Split(correctFieldName, camelCaseReplace)
for _, correctFieldNamePiece := range correctFieldNameCamelCase {
if len(correctFieldNamePiece) < 1 {
continue
}
firstLetterFieldNamePiece := strings.ToUpper(correctFieldNamePiece[:1])
glueFieldNamePiece := correctFieldNamePiece[1:]
if correctFieldNameBuffer.Len() == 0 && !firstLetterValid.MatchString(firstLetterFieldNamePiece) {
correctFieldNameBuffer.WriteString(replaceFirstLetter)
correctFieldNameBuffer.WriteString(glueFieldNamePiece)
} else {
correctFieldNameBuffer.WriteString(firstLetterFieldNamePiece)
correctFieldNameBuffer.WriteString(glueFieldNamePiece)
}
}
if correctFieldNameBuffer.Len() == 0 {
return noNameStruct
}
return correctFieldNameBuffer.String()
}
// toGoTagCorrectName converts tagName to a correct tag name.
func toGoTagCorrectName(tagName string) string {
return tagNonValidIdentifiers.ReplaceAllString(tagName, replaceString) // Remove non valid identifiers
}
// isScalar returns if the specified kind is scalar
func isScalar(kind reflect.Kind) bool {
switch kind {
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return true
}
return false
}