-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
168 lines (155 loc) · 4.05 KB
/
tag.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"math"
"reflect"
"strings"
"unicode"
"github.com/sergi/go-diff/diffmatchpatch"
// "github.com/uh-zz/tagscanner/examples/jsondecoder/runtimescan"
"gitlab.com/osaki-lab/tagscanner/runtimescan"
)
type FieldType string
const (
JsonField FieldType = "json"
IgnoreField FieldType = "-"
)
type defaultFieldType int
const (
lowerCase defaultFieldType = iota + 1
hyphenatedPascalCase
hyphenatedLowerCase
noField
)
func (t defaultFieldType) Convert(source string) string {
switch t {
case lowerCase:
return strings.ToLower(source)
case hyphenatedLowerCase:
words := splitWord(source)
var result []string
for _, word := range words {
result = append(result, strings.ToLower(word))
}
return strings.Join(result, "-")
case hyphenatedPascalCase:
words := splitWord(source)
var result []string
for _, word := range words {
result = append(result, strings.Title(word))
}
return strings.Join(result, "-")
case noField:
return ""
}
return ""
}
func splitWord(source string) []string {
var word []rune
var result []string
for _, c := range source {
if unicode.IsUpper(c) {
if len(word) != 0 {
result = append(result, string(word))
}
word = []rune{c}
} else {
word = append(word, c)
}
}
if len(word) > 0 {
result = append(result, string(word))
}
return result
}
var fieldTypes = map[FieldType]struct {
Type defaultFieldType
Optional bool
}{
JsonField: {
Type: lowerCase,
Optional: false,
},
}
func nearest(tagName string) string {
dmp := diffmatchpatch.New()
var nearest string
var dist = math.MaxInt64
for fieldType := range fieldTypes {
diffs := dmp.DiffMain(tagName, string(fieldType), false)
lv := dmp.DiffLevenshtein(diffs)
if lv < dist {
dist = lv
nearest = string(fieldType)
}
}
return nearest
}
type JsonTag struct {
Type FieldType
EType reflect.Type
Name string
Default string
Optional bool
Base bool
}
func ParseJsonTag(fieldName, tagSource, fullPath string, eType reflect.Type) (*JsonTag, error) {
// fmt.Println("ParseJsonTag", fieldName, tagSource, fullPath, eType)
sources := strings.Split(tagSource, ",")
if len(sources) > 1 {
tagSource = sources[0]
}
tags := strings.Split(tagSource, ":")
t := JsonTag{
Type: IgnoreField,
EType: eType,
}
var hasDefault bool
switch len(tags) {
case 1:
t.Type = FieldType(tags[0])
if f, ok := fieldTypes[t.Type]; ok {
hasDefault = f.Optional
t.Name = f.Type.Convert(fieldName)
} else {
return nil, fmt.Errorf("tag name '%s' of fieldName '%s' is invalid. did you mean '%s'?: %w", tags[0], fullPath, nearest(tags[0]), runtimescan.ErrParseTag)
}
case 2:
t.Type = FieldType(tags[0])
if f, ok := fieldTypes[t.Type]; ok {
hasDefault = f.Optional
if f.Type != noField {
t.Name = tags[1]
} else {
return nil, fmt.Errorf("tag type '%s' of fieldName '%s' can't have extra information '%s': %w", tags[0], fullPath, tags[1], runtimescan.ErrParseTag)
}
} else {
return nil, fmt.Errorf("tag type '%s' of fieldName '%s' is invalid. did you mean '%s'?: %w", tags[0], fullPath, nearest(tags[0]), runtimescan.ErrParseTag)
}
default:
return nil, fmt.Errorf("tag '%s' of fieldName '%s' is invalid. Zero or one colon ':' should be included: %w", tagSource, fullPath, runtimescan.ErrParseTag)
}
for i := 1; i < len(sources); i++ {
fragment := sources[i]
if strings.HasPrefix(fragment, "default:") {
if hasDefault {
t.Default = strings.TrimPrefix(fragment, "default:")
} else {
return nil, fmt.Errorf("tag type '%s' of fieldName '%s' can't have default value: %w", tags[0], fullPath, runtimescan.ErrParseTag)
}
} else if fragment == "optional" {
if hasDefault {
t.Optional = true
} else {
return nil, fmt.Errorf("tag type '%s' of fieldName '%s' can't be optional: %w", tags[0], fullPath, runtimescan.ErrParseTag)
}
} else if fragment == "base" {
if hasDefault {
t.Base = true
} else {
return nil, fmt.Errorf("tag type '%s' of fieldName '%s' can't be base: %w", tags[0], fullPath, runtimescan.ErrParseTag)
}
}
}
return &t, nil
}