-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaggeneration.go
125 lines (100 loc) · 2.43 KB
/
swaggeneration.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
package swaggeneration
import (
"bytes"
"log"
"os"
"path"
"path/filepath"
"text/template"
"gopkg.in/yaml.v2"
)
type Builder struct {
manager *template.Template
data *data
outputFileName string
}
type data struct {
collection map[string]interface{}
api []map[string]interface{}
}
func Init(baseTemplatePath, dataPath, outputFileName string) *Builder {
return &Builder{
manager: loadBaseTemplate(baseTemplatePath),
data: loadData(dataPath, outputFileName),
outputFileName: outputFileName,
}
}
func (b *Builder) Generate() {
outputFile, err := os.Create(b.outputFileName)
if err != nil {
panic(err)
}
defer outputFile.Close()
var bufferList []*bytes.Buffer
// generate api collection doc
var master bytes.Buffer
err = b.manager.ExecuteTemplate(&master, "_api-collection.yaml", b.data.collection)
if err != nil {
panic(err)
}
bufferList = append(bufferList, &master)
// generate api doc
for _, m := range b.data.api {
var apiBuffer bytes.Buffer
err = b.manager.ExecuteTemplate(&apiBuffer, "_api.yaml", m)
if err != nil {
panic(err)
}
bufferList = append(bufferList, &apiBuffer)
}
if err != nil {
panic(err)
}
for _, buffer := range bufferList {
_, errWrite := outputFile.Write(buffer.Bytes())
if errWrite != nil {
panic(errWrite)
}
}
}
func loadBaseTemplate(baseTemplatePath string) *template.Template {
tmp, err := template.New("templates").Funcs(templateFunc()).ParseGlob(filepath.Join(baseTemplatePath, "*.yaml"))
if err != nil {
panic(err)
}
return template.Must(tmp, err)
}
func loadData(dataPath, outputFileName string) *data {
fileList, errReadDir := os.ReadDir(dataPath)
if errReadDir != nil {
panic(errReadDir)
}
d := &data{}
for _, entry := range fileList {
if path.Ext(entry.Name()) != ".yaml" {
continue
}
if entry.Name() == "collection.yaml" {
d.collection = loadYAML(dataPath, entry)
continue
}
if entry.Name() == filepath.Base(outputFileName) {
continue
}
d.api = append(d.api, loadYAML(dataPath, entry))
}
return d
}
func loadYAML(basePath string, entry os.DirEntry) map[string]interface{} {
f, err := os.ReadFile(filepath.Join(basePath, entry.Name()))
if err != nil {
log.Fatalf("Error reading file: %v", err)
}
// Unmarshal YAML content into struct
var data map[string]interface{}
err = yaml.Unmarshal(f, &data)
if err != nil {
log.Fatalf("Error unmarshaling YAML: %v", err)
}
return data
}