-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.go
134 lines (121 loc) · 3.69 KB
/
generator.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
package generator
import (
"github.com/asyncapi/parser-go/pkg/jsonpath"
v2 "github.com/asyncapi/parser-go/pkg/parser/v2"
)
type Generator interface {
Generate(asyncApiSpecPath string, out string) (string, error)
}
func LoadAsyncApiSpecWithParser(asyncApiSpecPath string) (map[string]interface{}, error) {
refloader := jsonpath.NewRefLoader(nil)
parser := v2.NewParser(refloader)
data, err := parser.Load(asyncApiSpecPath)
if err != nil {
return nil, err
}
err = parser.Parse(data)
if err != nil {
return nil, err
}
return data, nil
}
func GetMessages(data map[string]interface{}) []map[string]interface{} {
var out []map[string]interface{}
for _, channel := range data["channels"].(map[string]interface{}) {
operation := channel.(map[string]interface{})
if operation["publish"] != nil {
operationProperties := operation["publish"].(map[string]interface{})
message := operationProperties["message"].(map[string]interface{})
out = append(out, message)
} else if operation["subscribe"] != nil {
operationProperties := operation["subscribe"].(map[string]interface{})
message := operationProperties["message"].(map[string]interface{})
out = append(out, message)
}
}
return out
}
func CheckRequired(propertyName string, required []interface{}) bool {
for _, requiredProperty := range required {
reqProp := requiredProperty.(string)
if propertyName == reqProp {
return true
}
}
return false
}
func GetNestedObjects(messages []map[string]interface{}) []map[string]interface{} {
var nestedObject []map[string]interface{}
for _, message := range messages {
pay := message["payload"].(map[string]interface{})
for _, prop := range pay["properties"].(map[string]interface{}) {
property := prop.(map[string]interface{})
if property["type"] == "object" {
if property["additionalProperties"] != nil {
apb, ok := property["additionalProperties"].(bool)
if ok {
if !apb {
property["parent"] = message["name"]
nestedObject = append(nestedObject, property)
}
}
} else {
property["parent"] = message["name"]
nestedObject = append(nestedObject, property)
}
}
}
}
return nestedObject
}
func GetItemObjects(messages []map[string]interface{}) []map[string]interface{} {
var nestedObject []map[string]interface{}
for _, message := range messages {
pay := message["payload"].(map[string]interface{})
for _, prop := range pay["properties"].(map[string]interface{}) {
property := prop.(map[string]interface{})
if property["type"] == "array" {
if property["additionalProperties"] != nil {
apb, ok := property["additionalProperties"].(bool)
if ok {
if !apb {
propItems := property["items"].(map[string]interface{})
if propItems["type"] == "object" {
property["parent"] = message["name"]
nestedObject = append(nestedObject, property)
}
}
}
} else {
propItems := property["items"].(map[string]interface{})
if propItems["type"] == "object" {
property["parent"] = message["name"]
nestedObject = append(nestedObject, property)
}
}
}
}
}
return nestedObject
}
func HasDefault(property map[string]interface{}) bool {
return property["default"] != nil
}
func HasProducer(data map[string]interface{}) bool {
for _, channel := range data["channels"].(map[string]interface{}) {
operation := channel.(map[string]interface{})
if operation["subscribe"] != nil {
return true
}
}
return false
}
func HasConsumer(data map[string]interface{}) bool {
for _, channel := range data["channels"].(map[string]interface{}) {
operation := channel.(map[string]interface{})
if operation["publish"] != nil {
return true
}
}
return false
}