-
Notifications
You must be signed in to change notification settings - Fork 27
/
utils.go
121 lines (104 loc) · 2.41 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
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
package gen
import (
"strings"
"unicode"
"github.com/dave/jennifer/jen"
"github.com/iancoleman/strcase"
"github.com/amarnathcjd/gogram/internal/cmd/tlgen/tlparser"
)
func createParamsStructFromMethod(method tlparser.Method) tlparser.Object {
return tlparser.Object{
Name: method.Name + "Params",
CRC: method.CRC,
Parameters: method.Parameters,
}
}
func haveOptionalParams(params []tlparser.Parameter) bool {
for _, param := range params {
if param.IsOptional {
return true
}
}
return false
}
func maxBitflag(params []tlparser.Parameter) int {
max := 0
for _, param := range params {
if param.BitToTrigger > max {
max = param.BitToTrigger
}
}
return max
}
func goify(name string, public bool) string {
delim := strcase.ToDelimited(name, '|')
delim = strings.ReplaceAll(delim, ".", "|") // strace не видит точки!!
splitted := strings.Split(delim, "|")
for i, item := range splitted {
item = strings.ToLower(item)
if SliceContains(capitalizePatterns, item) {
item = strings.ToUpper(item)
}
itemRunes := []rune(item)
if i == 0 && !public {
// потому что aPI, uRL, это криворуко
itemRunes = []rune(strings.ToLower(item))
} else {
itemRunes[0] = unicode.ToUpper(itemRunes[0])
}
splitted[i] = string(itemRunes)
}
return strings.Join(splitted, "")
}
func (g *Generator) typeIdFromSchemaType(t string) *jen.Statement {
item := &jen.Statement{}
switch t {
case "Bool":
item = jen.Bool()
case "long":
item = jen.Int64()
case "double":
item = jen.Float64()
case "int":
item = jen.Int32()
case "string":
item = jen.String()
case "bytes":
item = jen.Index().Byte()
case "bitflags":
panic("bitflags cant be generated or even cath in this part")
case "true":
item = jen.Bool()
default:
if _, ok := g.schema.Enums[t]; ok {
item = jen.Id(goify(t, true))
break
}
if _, ok := g.schema.Types[t]; ok {
item = jen.Id(goify(t, true))
break
}
found := false
for _, _struct := range g.schema.SingleInterfaceTypes {
if _struct.Interface == t {
item = jen.Id("*" + goify(_struct.Name, true))
found = true
break
}
}
if found {
break
}
//pp.Fprintln(os.Stderr, g.schema)
panic("пробовали обработать '" + t + "'")
}
return item
}
func SliceContains(slice []string, item string) bool {
for _, i := range slice {
if i == item {
return true
}
}
return false
}