-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsetters.go
158 lines (121 loc) · 3.73 KB
/
setters.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
package main
import (
"fmt"
"os"
"regexp"
"strings"
)
type tgSetter struct {
structType string
fieldName string
fieldSnakeCaseName string
fieldType string
}
type tgSetters []tgSetter
const typeStructPattern = `
type (\w+) struct {
(.+?)
}
`
const fieldPattern = "(\\w+) ([\\*A-Za-z\\[\\]]+) `json:\"(\\w+)"
var (
typeStructRegexp = regexp.MustCompile(preparePattern(typeStructPattern))
fieldRegexp = regexp.MustCompile(fieldPattern)
)
func generateSetters(typesData string, desiredStructs []string) tgSetters {
structsGroups := typeStructRegexp.FindAllStringSubmatch(typesData, -1)
logInfof("Structs count: %d", len(structsGroups))
var setters tgSetters
for _, structsGroup := range structsGroups {
structType := structsGroup[1]
if len(desiredStructs) > 0 {
found := contains(desiredStructs, structType)
if !found {
continue
}
}
fields := structsGroup[2]
fieldsGroups := fieldRegexp.FindAllStringSubmatch(fields, -1)
for _, fieldsGroup := range fieldsGroups {
setter := tgSetter{
structType: structType,
fieldName: fieldsGroup[1],
fieldSnakeCaseName: fieldsGroup[3],
fieldType: fieldsGroup[2],
}
if (strings.HasPrefix(setter.structType, "InlineQueryResult") ||
strings.HasPrefix(setter.structType, "InputMedia") ||
setter.structType == "MenuButtonWebApp") &&
setter.fieldName == "Type" {
continue
}
setters = append(setters, setter)
}
}
logInfof("Setters count: %d", len(setters))
return setters
}
func contains(slice []string, a string) bool {
found := false
for _, s := range slice {
if s == a {
found = true
break
}
}
return found
}
func writeSetters(file *os.File, setters tgSetters, receiverDefault bool, noPointerStructs []string) {
data := strings.Builder{}
data.WriteString(`package telego` + "\n\n")
for _, setter := range setters {
data.WriteString(fmt.Sprintf("// With%s adds %s parameter\n", setter.fieldName,
strings.ReplaceAll(setter.fieldSnakeCaseName, "_", " ")))
if setter.fieldName == "Type" && setter.structType == "SendPollParams" {
setter.fieldName = "XXXType"
}
if strings.HasPrefix(setter.fieldType, "[]") {
setter.fieldType = strings.Replace(setter.fieldType, "[]", "...", 1)
}
r := "p"
if !receiverDefault {
r = firstToLower(string(setter.structType[0]))
}
noPointer := contains(noPointerStructs, setter.structType)
convertToPtr := setter.fieldType == "*bool" || setter.fieldType == "*string" || setter.fieldType == "*int"
var s string
if setter.fieldType != "bool" {
if convertToPtr {
setter.fieldType = strings.TrimPrefix(setter.fieldType, "*")
}
s = fmt.Sprintf("func (%s *%s) With%s(%s %s) *%s {\n", r, setter.structType,
setter.fieldName, firstToLower(setter.fieldName), setter.fieldType, setter.structType)
if convertToPtr {
setter.fieldType = "*" + setter.fieldType
}
} else {
s = fmt.Sprintf("func (%s *%s) With%s() *%s {\n", r, setter.structType,
setter.fieldName, setter.structType)
}
if noPointer {
s = strings.Replace(s, "*"+setter.structType+")", setter.structType+")", 1)
s = strings.Replace(s, "*"+setter.structType+" ", setter.structType+" ", 1)
}
if len(s) > maxLineLen+11 {
s = strings.Replace(s, ") *", ",\n) *", 1)
}
data.WriteString(s)
if setter.fieldType != "bool" {
value := firstToLower(setter.fieldName)
if convertToPtr {
value = "ToPtr(" + value + ")"
}
data.WriteString(fmt.Sprintf("\t%s.%s = %s\n", r, setter.fieldName, value))
} else {
data.WriteString(fmt.Sprintf("\t%s.%s = true\n", r, setter.fieldName))
}
data.WriteString(fmt.Sprintf("\treturn %s\n}\n\n", r))
}
_, err := file.WriteString(uppercaseWords(data.String()))
exitOnErr(err)
}