Repository navigation
Expand file tree
/
Copy pathdocument.go
More file actions
195 lines (151 loc) · 4.21 KB
/
Copy pathdocument.go
File metadata and controls
195 lines (151 loc) · 4.21 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
package openapi
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"github.com/goccy/go-yaml"
)
type OpenapiComponent struct {
}
type Document struct {
Openapi string `json:"openapi"`
Info Info `json:"info"`
Components map[string]OpenapiComponent `json:"components,omitempty"`
Paths map[string]map[string]Path `json:"paths,omitempty"`
// ^ PATH ^ METHOD
}
type Path struct {
Summary string `json:"summary"`
Description string `json:"description"`
Tags []string `json:"tags,omitempty"`
Responses map[string]Response `json:"responses,omitempty"`
Parameters []Parameter `json:"parameters,omitempty"`
RequestBody Body `json:"requestBody,omitempty"`
}
type Items struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Items *Items `json:"items,omitempty"`
Properties map[string]Schema `json:"properties,omitempty,omitzero"`
Ref string `json:"$ref,omitempty"`
}
// IsZero é o que o `omitzero` de Schema.Items consulta para não emitir um
// `items: {}` em schema que não é lista.
func (this Items) IsZero() bool {
return this.Type == "" &&
this.Format == "" &&
this.Ref == "" &&
this.Items == nil &&
len(this.Properties) == 0
}
type Schema struct {
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Items Items `json:"items,omitzero,omitempty"`
Properties map[string]Schema `json:"properties,omitempty"`
Ref string `json:"$ref,omitempty"`
}
func (this Schema) ToItems() Items {
items := Items{
Type: this.Type,
Properties: this.Properties,
Ref: this.Ref,
Format: this.Format,
}
// Lista de lista: o nível de dentro precisa descer junto, senão `[][]string`
// perde o tipo do item interno.
if !this.Items.IsZero() {
nested := this.Items
items.Items = &nested
}
return items
}
type MediaTypeObject struct {
Schema Schema `json:"schema,omitempty"`
// example, examples, encoding... podem entrar aqui depois
}
type Body struct {
Description string `json:"description"`
Required bool `json:"required"`
Content map[string]MediaTypeObject `json:"content,omitempty"`
MediaType string `json:"-"`
}
type Response struct {
Description string `json:"description"`
// key string is the media type - in this moment only application/json
Content map[string]MediaTypeObject `json:"content"`
}
type Parameter struct {
Name string `json:"name"`
In string `json:"in"`
Schema Schema `json:"schema"`
Required bool `json:"required"`
Description string `json:"description"`
}
type Info struct {
Title string `json:"title"`
Description string `json:"description"`
Version string `json:"version"`
}
type WriteOptions struct {
Formats []string
FolderPath string
FileName string
}
func (this *Document) Output(format string) ([]byte, error) {
var output []byte
var err error
switch format {
case "yaml":
output, err = yaml.Marshal(this)
case "json":
output, err = json.MarshalIndent(this, "", "\t")
}
return output, err
}
func (this *Document) Write(options ...WriteOptions) error {
option := WriteOptions{
Formats: []string{"yaml", "json"},
FolderPath: "specs",
FileName: "index",
} // default options
if len(options) > 0 {
overrideOpts := options[0]
option = Merge(option, overrideOpts)
}
for _, format := range []string{"yaml", "json"} {
if !slices.Contains(option.Formats, format) {
continue
}
output, err := this.Output(format)
if err != nil {
fmt.Println("Could not convert to Yaml")
return err
}
if option.FolderPath != "" {
err := os.MkdirAll(option.FolderPath, 0755)
if err != nil {
return err
}
}
fileName := filepath.Join(option.FolderPath, option.FileName+"."+format)
file, err := os.Create(fileName)
if err != nil {
return err
}
defer func() {
err := file.Close()
if err != nil {
fmt.Println("Error on closing file connection!")
}
}()
_, err = file.Write(output)
if err != nil {
fmt.Println("Trying to write in file failed")
return err
}
}
return nil
}