/
translator.go
108 lines (83 loc) · 3 KB
/
translator.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
package apiclient
import (
"sort"
"github.com/Jeffail/gabs/v2"
)
func Translate(content []byte) (*interface{}, error) {
jsonParsed, err := gabs.ParseJSON(content)
if err != nil {
return nil, err
}
response := gabs.New()
response.Set(jsonParsed.Path("packageName").Data(), "packageName")
if jsonParsed.Exists("documentation") {
response.Set(jsonParsed.Path("documentation").Data(), "documentation")
}
response.Set(jsonParsed.Path("name").Data(), "name")
if jsonParsed.Exists("properties") {
response.Array("properties")
propertiesMap := jsonParsed.S("properties").ChildrenMap()
propertiesMapKeys := sortMapKeys(propertiesMap)
for _, key := range propertiesMapKeys {
functionParamsResponse := gabs.New()
functionParamsResponse.Set(propertiesMap[key].Path("dataType").Data(), "type")
functionParamsResponse.Set(key, "name")
response.ArrayAppend(functionParamsResponse.Data(), "properties")
}
}
if jsonParsed.Exists("subClients") {
response.Array("subClients")
subClients := jsonParsed.S("subClients").Children()
sortArrayByName(subClients)
for _, subClient := range subClients {
subClientResponse := gabs.New()
subClientResponse.Set(subClient.Path("name").Data(), "name")
if subClient.Exists("function") {
subClientResponse.Array("functionParams")
parameterMap := subClient.S("function", "parameters").ChildrenMap()
parameterMapKeys := sortMapKeys(parameterMap)
for _, key := range parameterMapKeys {
parameter := parameterMap[key]
functionParamsResponse := gabs.New()
functionParamsResponse.Set(parameter.Path("dataType").Data(), "type")
functionParamsResponse.Set(key, "name")
subClientResponse.ArrayAppend(functionParamsResponse.Data(), "functionParams")
}
}
if subClient.Exists("properties") {
subClientResponse.Array("properties")
propertiesMap := subClient.S("properties").ChildrenMap()
propertiesMapKeys := sortMapKeys(propertiesMap)
for _, key := range propertiesMapKeys {
property := propertiesMap[key]
propertiesResponse := gabs.New()
propertiesResponse.Set(property.Path("dataType").Data(), "type")
propertiesResponse.Set(key, "name")
if property.Exists("parentProperty") {
propertiesResponse.Set(property.Path("parentProperty").Data(), "parentProperty")
}
if property.Exists("functionParameter") {
propertiesResponse.Set(property.Path("functionParameter").Data(), "functionParameter")
}
subClientResponse.ArrayAppend(propertiesResponse.Data(), "properties")
}
}
response.ArrayAppend(subClientResponse.Data(), "subClients")
}
}
data := response.Data()
return &data, nil
}
func sortMapKeys(data map[string]*gabs.Container) []string {
keys := make([]string, 0, len(data))
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func sortArrayByName(array []*gabs.Container) {
sort.Slice(array[:], func(i, j int) bool {
return array[i].Path("name").Data().(string) < array[j].Path("name").Data().(string)
})
}