-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_parse.go
90 lines (86 loc) · 2.78 KB
/
model_parse.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
package main
func parseModelToNonDuplicatedContents(value interface{}) (map[string]*TEnum, map[string]*TStruct, map[string]*TException) {
enums := make(map[string]*TEnum)
structs := make(map[string]*TStruct)
exceptions := make(map[string]*TException)
switch content := value.(type) {
case *TService:
for _, m := range content.Methods {
es, ss, e := parseModelToNonDuplicatedContents(m)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
case *TStruct:
structs[content.Name] = content
for _, f := range content.Fields {
es, ss, e := parseModelToNonDuplicatedContents(f)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
case *TEnum:
enums[content.Name] = content
case *TMap:
{
es, ss, e := parseModelToNonDuplicatedContents(content.KeyType)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
{
es, ss, e := parseModelToNonDuplicatedContents(content.ValueType)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
case *TList:
es, ss, e := parseModelToNonDuplicatedContents(content.Type)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
case *TSet:
es, ss, e := parseModelToNonDuplicatedContents(content.Type)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
case *TField:
es, ss, e := parseModelToNonDuplicatedContents(content.Type)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
case *TMethod:
{
es, ss, e := parseModelToNonDuplicatedContents(content.Response)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
{
es, ss, e := parseModelToNonDuplicatedContents(content.Exception)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
{
for _, f := range content.Parameters {
es, ss, e := parseModelToNonDuplicatedContents(f)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
}
case *TException:
exceptions[content.Name] = content
for _, f := range content.Fields {
es, ss, e := parseModelToNonDuplicatedContents(f)
updateEnumAndStructs(enums, structs, exceptions, es, ss, e)
}
}
return enums, structs, exceptions
}
func updateEnumAndStructs(basee map[string]*TEnum, bases map[string]*TStruct, baseex map[string]*TException, newe map[string]*TEnum, news map[string]*TStruct, newex map[string]*TException) {
for k, v := range newe {
basee[k] = v
}
for k, v := range news {
bases[k] = v
}
for k, v := range newex {
baseex[k] = v
}
}
func FormatThriftService(service *TService) string {
enums, structs, exceptions := parseModelToNonDuplicatedContents(service)
var txt string
for _, enum := range enums {
txt += enum.TString() + "\n\n"
}
for _, s := range structs {
txt += s.TString() + "\n\n"
}
for _, s := range exceptions {
txt += s.TString() + "\n\n"
}
return txt + service.TString()
}