/
specasyncapiprocessor.go
152 lines (138 loc) · 3.76 KB
/
specasyncapiprocessor.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
package apic
import (
"net/url"
"strconv"
"strings"
)
type asyncAPIProcessor struct {
asyncapiDef map[string]interface{}
spec []byte
}
func newAsyncAPIProcessor(asyncapiDef map[string]interface{}, spec []byte) *asyncAPIProcessor {
return &asyncAPIProcessor{asyncapiDef: asyncapiDef, spec: spec}
}
func (p *asyncAPIProcessor) GetResourceType() string {
return AsyncAPI
}
// GetVersion -
func (p *asyncAPIProcessor) GetVersion() string {
info := p.asyncapiDef["info"]
if info != nil {
if infoDetail, ok := info.(map[string]interface{}); ok {
version := infoDetail["version"]
if version != nil {
return version.(string)
}
}
}
return ""
}
// GetDescription -
func (p *asyncAPIProcessor) GetDescription() string {
info := p.asyncapiDef["info"]
if info != nil {
if infoDetail, ok := info.(map[string]interface{}); ok {
description := infoDetail["description"]
if description != nil {
return description.(string)
}
}
}
return ""
}
// GetEndPoints -
func (p *asyncAPIProcessor) GetEndpoints() ([]EndpointDefinition, error) {
endpoints := make([]EndpointDefinition, 0)
var err error
servers := p.asyncapiDef["servers"]
if servers != nil {
if serverList, ok := servers.(map[string]interface{}); ok {
endpoints, err = p.parseServerList(serverList)
if err != nil {
return nil, err
}
}
}
return endpoints, nil
}
func (p *asyncAPIProcessor) parseServerList(serverList map[string]interface{}) ([]EndpointDefinition, error) {
endpoints := make([]EndpointDefinition, 0)
for _, value := range serverList {
serverObjInterface, ok := value.(map[string]interface{})
if ok {
endpoint, err := p.parseServerObject(serverObjInterface)
if err != nil {
return nil, err
}
endpoints = append(endpoints, endpoint)
}
}
return endpoints, nil
}
func (p *asyncAPIProcessor) parseServerObject(serverObjInterface map[string]interface{}) (EndpointDefinition, error) {
var err error
protocol := ""
serverURL := ""
var serverVariables map[string]string
serverDetails := map[string]interface{}{}
for key, valueInterface := range serverObjInterface {
value, ok := valueInterface.(string)
if ok {
if key == "protocol" {
protocol = value
}
if key == "url" {
serverURL = value
}
}
if key == "variables" {
variablesInterface, ok := valueInterface.(map[string]interface{})
if ok {
serverVariables, _ = p.parseVariables(variablesInterface)
}
}
if key == "bindings" {
serverDetails = valueInterface.(map[string]interface{})
}
}
endpoint := EndpointDefinition{}
endpoint.Protocol = protocol
// variable substitution
for varName, varValue := range serverVariables {
serverURL = strings.ReplaceAll(serverURL, "{"+varName+"}", varValue)
}
parseURL, err := url.Parse(protocol + "://" + serverURL)
endpoint.Host = parseURL.Hostname()
port, _ := strconv.Atoi(parseURL.Port())
endpoint.Port = int32(port)
endpoint.BasePath = parseURL.Path
endpoint.Details = serverDetails
return endpoint, err
}
func (p *asyncAPIProcessor) parseVariables(variablesObjInterface map[string]interface{}) (map[string]string, error) {
serverVars := make(map[string]string)
for varName, varObject := range variablesObjInterface {
varObjectInterface, ok := varObject.(map[string]interface{})
if ok {
varValue := p.parseVariableObject(varObjectInterface)
serverVars[varName] = varValue
}
}
return serverVars, nil
}
func (p *asyncAPIProcessor) parseVariableObject(serverObjInterface map[string]interface{}) string {
varDefaultValue := ""
for key, valueInterface := range serverObjInterface {
if key == "default" {
value, ok := valueInterface.(string)
if ok {
varDefaultValue = value
}
}
}
return varDefaultValue
}
// GetSpecBytes -
func (p *asyncAPIProcessor) GetSpecBytes() []byte {
return p.spec
}