-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
183 lines (163 loc) · 5.97 KB
/
main.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strconv"
clipboard "github.com/atotto/clipboard"
"github.com/gorilla/mux"
)
type jsonRequest struct {
JSON interface{} `json:"json_schema"`
}
func main() {
routes := mux.NewRouter()
server := os.Getenv("PORT")
routes.HandleFunc("/convert", convertToSwagger).Methods("POST", "OPTIONS")
routes.HandleFunc("/ping", ping).Methods("GET")
log.Printf("server running on %v", server)
err := http.ListenAndServe(":"+server, routes)
if err != nil {
log.Fatalf("Unable to run http server: %v", err)
}
log.Println("Stopping API Service...")
}
func ping(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers")
fmt.Fprintf(w, "pong")
}
func convertToSwagger(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type,access-control-allow-origin, access-control-allow-headers")
decoder := json.NewDecoder(r.Body)
jsonSchema := jsonRequest{}
err := decoder.Decode(&jsonSchema)
if err != nil {
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, err.Error())
fmt.Println(err.Error())
return
}
yamlSchema := converToYAML(jsonSchema.JSON.(map[string]interface{}))
fmt.Fprintf(w, yamlSchema)
fmt.Println("success")
}
func converToYAML(oneLevelJSON map[string]interface{}) string {
var yamlSchema string
for k, v := range oneLevelJSON {
var dataType string
switch v.(type) {
case string:
dataType = "string"
yamlSchema += fmt.Sprintf("%v:\n type: %v\n", k, dataType)
case int, int16, int32, int64, int8, float32, float64, uint, uint16, uint32, uint64, uint8:
dataType = "integer"
yamlSchema += fmt.Sprintf("%v:\n type: %v\n", k, dataType)
case bool:
dataType = "boolean"
yamlSchema += fmt.Sprintf("%v:\n type: %v\n", k, dataType)
case []interface{}:
temp := (v.([]interface{}))
var ex string
for _, v := range temp {
switch v.(type) {
case float64:
ex = strconv.FormatFloat(v.(float64), 'f', 0, 64)
dataType = "array\n items: {}\n example:\n - " + ex
yamlSchema += fmt.Sprintf("%v:\n type: %v\n", k, dataType)
break
case string:
ex = v.(string)
dataType = "array\n items: {}\n example:\n - " + ex
yamlSchema += fmt.Sprintf("%v:\n type: %v\n", k, dataType)
break
case map[string]interface{}:
properties := (v.(map[string]interface{}))
var dataProperties string
for k, v := range properties {
dataProperties += parse(k, v, 6, 8)
}
yamlSchema += fmt.Sprintf("%v:\n type: array\n items:\n type: object\n properties:\n%v", k, dataProperties)
break
}
}
case map[string]interface{}:
properties := (v.(map[string]interface{}))
var dataProperties string
for k, v := range properties {
dataProperties += parse(k, v, 4, 6)
}
yamlSchema += fmt.Sprintf("%v:\n type: object\n properties:\n%v", k, dataProperties)
}
}
clipboard.WriteAll(yamlSchema)
return yamlSchema
}
func parse(key interface{}, value interface{}, indentationOne int, indentationTwo int) string {
var firstIndentation int = indentationOne
var secondIndentation int = indentationTwo
var yamlSchema string
switch value.(type) {
case string:
dataType := "string"
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: %v\n", firstIndentation, "", key, secondIndentation, "", dataType)
firstIndentation += 2
secondIndentation += 2
case int, int16, int32, int64, int8, float32, float64, uint, uint16, uint32, uint64, uint8:
dataType := "integer"
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: %v\n", firstIndentation, "", key, secondIndentation, "", dataType)
firstIndentation += 2
secondIndentation += 2
case bool:
dataType := "boolean"
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: %v\n", firstIndentation, "", key, secondIndentation, "", dataType)
firstIndentation += 2
secondIndentation += 2
case []interface{}:
temp := (value.([]interface{}))
var ex string
for _, v := range temp {
switch v.(type) {
case float64:
ex = strconv.FormatFloat(v.(float64), 'f', 0, 64)
dataType := fmt.Sprintf("array\n%*sitems: {}\n%*sexample:\n%*s - %v", secondIndentation, "", secondIndentation, "", secondIndentation, "", ex)
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: %v\n", firstIndentation, "", key, secondIndentation, "", dataType)
firstIndentation += 2
secondIndentation += 2
break
case string:
ex = v.(string)
dataType := fmt.Sprintf("array\n%*sitems: {}\n%*sexample:\n%*s - %v", secondIndentation, "", secondIndentation, "", secondIndentation, "", ex)
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: %v\n", firstIndentation, "", key, secondIndentation, "", dataType)
firstIndentation += 2
secondIndentation += 2
break
case map[string]interface{}:
properties := (v.(map[string]interface{}))
var dataProperties string
for k, v := range properties {
dataProperties += parse(k, v, firstIndentation+6, secondIndentation+6)
}
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: array\n%*sitems:\n%*stype: object\n%*sproperties:\n%v", firstIndentation, "", key, secondIndentation, "", secondIndentation, "", secondIndentation+2, "", secondIndentation+2, "", dataProperties)
firstIndentation += 2
secondIndentation += 2
break
}
}
case map[string]interface{}:
properties := (value.(map[string]interface{}))
var dataProperties string
for k, v := range properties {
dataProperties += parse(k, v, firstIndentation+4, secondIndentation+4)
}
yamlSchema += fmt.Sprintf("%*s%v:\n%*stype: object\n%*sproperties:\n%v", firstIndentation, "", key, secondIndentation, "", secondIndentation, "", dataProperties)
firstIndentation += 2
secondIndentation += 2
}
return yamlSchema
}