-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrefiners.go
More file actions
108 lines (82 loc) · 2.04 KB
/
refiners.go
File metadata and controls
108 lines (82 loc) · 2.04 KB
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
package xparse
import (
"os"
"strings"
"github.com/iancoleman/strcase"
"github.com/thoas/go-funk"
)
type RefOpts struct {
methods []string
hintType int
promptCfg *PromptConfig
}
type RefOptFunc func(o *RefOpts)
func bindRefOpts(opt *RefOpts, opts ...RefOptFunc) {
for _, f := range opts {
f(opt)
}
}
func WithMethods(marr []string) RefOptFunc {
return func(o *RefOpts) {
o.methods = append(o.methods, marr...)
}
}
func WithHintType(i int) RefOptFunc {
return func(o *RefOpts) {
o.hintType = i
}
}
func WithRefPromptConfig(cfg *PromptConfig) RefOptFunc {
return func(o *RefOpts) {
o.promptCfg = cfg
}
}
// UpdateRefiners binds all refiners to parser
func UpdateRefiners(parser any, opts ...RefOptFunc) {
opt := RefOpts{hintType: 1}
bindRefOpts(&opt, opts...)
Invoke(parser, "Scan")
attrs, _ := GetField(parser, "AttrToBeRefined").Interface().([]string)
attrs = append(attrs, opt.methods...)
bindRefiners(parser, attrs, opts...)
}
func bindRefiners(parser any, attrs []string, opts ...RefOptFunc) {
opt := RefOpts{hintType: 1}
bindRefOpts(&opt, opts...)
refiners, _ := GetField(parser, "Refiners").Interface().(map[string]func(raw ...any) any)
missing := []string{}
//nolint:revive,stylecheck
for _, mtd_name := range attrs {
mtdName := GetCamelRefinerName(mtd_name)
method := GetMethod(parser, mtdName)
if funk.IsEmpty(method) {
// missing[mtd_name] = mtdName
missing = append(missing, mtdName)
continue
}
refiners[mtdName], _ = method.Interface().(func(raw ...any) any)
}
promptMissingRefiners(parser, missing, opt)
if len(missing) > 0 {
os.Exit(0)
}
}
func GetCamelRefinerName(input string) string {
return fixAcronyms(strcase.ToCamel(input))
}
func GetLowerCamelRefinerName(input string) string {
return fixAcronyms(strcase.ToLowerCamel(input))
}
var commonAcronyms = map[string]string{
"Id": "ID",
"Url": "URL",
"Uri": "URI",
"Json": "JSON",
// Add more as needed
}
func fixAcronyms(s string) string {
for k, v := range commonAcronyms {
s = strings.ReplaceAll(s, k, v)
}
return s
}