-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlfix.go
121 lines (102 loc) · 2.91 KB
/
yamlfix.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
package main
import (
"bufio"
"bytes"
"encoding/json"
oas "github.com/getkin/kin-openapi/openapi3"
"io"
"os"
"strings"
"yamlfix/misc"
)
func main() {
InitLog()
// LIFO order for defer
defer misc.DeferError(xLogFile.Close)
defer misc.DeferError(xLogBuffer.Flush)
InitFlags()
xApi, err := oas.NewLoader().LoadFromFile(GetFlagString("infile"))
if nil == xApi || nil != err {
if nil != err {
xLog.Printf("failed to load %s because %s",
GetFlagString("infile"), err.Error())
}
os.Exit(-1)
}
if FlagDebug {
var sb strings.Builder
sb.WriteString("debug_pre_")
sb.WriteString(GetFlagString("outfile"))
writeJsonOASFile(xApi, sb.String())
xLog.Printf("Writing debug output file to %s\n", sb.String())
}
if !FlagNoTables {
switch GetFlagString("format") {
case "SIMPLEX":
SimplexEnhanceDescriptions(xApi)
break
default:
xLog.Printf("Huh? Somehow an unrecognized format [ %s ] was requested?",
GetFlagString("format"))
}
}
if FlagApiReport {
if FlagDebug {
xLog.Println("Creating API report")
}
ApiReport(xApi)
}
if FlagDereference {
if FlagDebug {
xLog.Println("Writing output file with all references replaced, and unmodified file as debug_post_reference_")
writeJsonOASFile(xApi, "debug_post_reference_"+GetFlagString("outfile"))
} else {
xLog.Print("Writing output file with internal referenced expanded")
}
StripReferences(xApi)
writeJsonOASFile(xApi, GetFlagString("outfile"))
} else {
writeJsonOASFile(xApi, GetFlagString("outfile"))
if FlagDebug {
xLog.Print("Writing output file with references, and expanded file as debug_post_dereference_")
StripReferences(xApi)
writeJsonOASFile(xApi, "debug_post_dereference_"+GetFlagString("outfile"))
} else {
xLog.Print("writing output file with internal references")
}
}
}
// reformat JSON & output to file
func writeJsonOASFile(api *oas.T, fileName string) {
output, err := api.MarshalJSON()
if nil != err {
xLog.Fatalf("Attempting to reconstruct API spec failed because: %s", err.Error())
}
// decoder does its own buffering
decJson := json.NewDecoder(bytes.NewReader(output))
outFile, err := os.Create(fileName)
if nil != err {
xLog.Fatalf("Failed to create output JSON file %s because %s",
fileName, err.Error())
}
defer misc.DeferError(outFile.Close)
bufferOutFile := bufio.NewWriter(outFile)
defer misc.DeferError(bufferOutFile.Flush)
encJson := json.NewEncoder(bufferOutFile)
encJson.SetIndent("", FlagIndentString)
encJson.SetEscapeHTML(false)
var m map[string]interface{}
// this would be best understood as a do-while loop:
// do { decode; encode; } while (nil == err01);
for err = nil; nil == err; err = encJson.Encode(&m) {
err = decJson.Decode(&m)
if nil != err {
break
}
}
// this error could be from EITHER encJson.Encode or decJson.Decode
if io.EOF != err {
xLog.Fatalf("Could not output JSON file %s because: %s",
fileName, err.Error())
}
}