-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine_mapper.go
76 lines (65 loc) · 1.47 KB
/
engine_mapper.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
package engine
import (
"reflect"
"strings"
"unicode"
)
var (
reservedFieldName = map[string]string{
"OCSP": "ocsp",
}
reservedMethodNames = map[string]string{
"JSON": "json",
"HTML": "html",
"URL": "url",
"OCSP": "ocsp",
}
)
func FieldName(_ reflect.Type, f reflect.StructField) string {
if f.PkgPath != "" {
return ""
}
if tag := f.Tag.Get("js"); tag != "" {
if tag == "-" {
return ""
}
return tag
}
if exception, ok := reservedFieldName[f.Name]; ok {
return exception
}
return toLowerCase(f.Name)
}
// MethodName Returns the JS name for an exported method.
func MethodName(_ reflect.Type, m reflect.Method) string {
if m.Name[0] == 'X' {
return m.Name[1:]
}
if exception, ok := reservedMethodNames[m.Name]; ok {
return exception
}
return toLowerCase(m.Name)
}
type EngineFieldMapper struct{}
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
func (EngineFieldMapper) FieldName(t reflect.Type, f reflect.StructField) string {
return FieldName(t, f)
}
// https://godoc.org/github.com/dop251/goja#FieldNameMapper
func (EngineFieldMapper) MethodName(t reflect.Type, m reflect.Method) string { return MethodName(t, m) }
func toLowerCase(name string) string {
runes := []rune(name)
n := 0
for i := 0; i < len(runes); i++ {
if !unicode.IsUpper(runes[i]) && !unicode.IsDigit(runes[i]) {
if i > 1 {
n = i - 1
} else {
n = i
}
break
}
}
out := strings.ToLower(string(runes[:n])) + string(runes[n:])
return out
}