/
marshal.go
123 lines (102 loc) · 2.99 KB
/
marshal.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package oas2
import (
"bytes"
"encoding/json"
"github.com/getkin/kin-openapi/openapi2"
"sigs.k8s.io/yaml"
)
func MarshalJson(v2 *openapi2.Swagger) ([]byte, error) {
return json.Marshal(v2)
}
func MarshalJsonIndent(v2 *openapi2.Swagger, prefix, indent string) ([]byte, error) {
return json.MarshalIndent(v2, prefix, indent)
}
func MarshalYaml(v2 *openapi2.Swagger) ([]byte, error) {
var buf = bytes.NewBuffer(nil)
swagger, _ := yaml.Marshal(v2.Swagger)
buf.WriteString("swagger: ")
buf.Write(swagger)
info, _ := yaml.Marshal(v2.Info)
buf.WriteString("\ninfo:")
buf.Write(indent(info))
if len(v2.Host) > 0 {
host, _ := yaml.Marshal(v2.Host)
buf.WriteString("\nhost: ")
buf.Write(host)
}
if len(v2.BasePath) > 0 {
basePath, _ := yaml.Marshal(v2.BasePath)
buf.WriteString("\nbasePath: ")
buf.Write(basePath)
}
if len(v2.Schemes) > 0 {
schemes, _ := yaml.Marshal(v2.Schemes)
buf.WriteString("\nschemes:")
buf.Write(indent(schemes))
}
paths, _ := yaml.Marshal(v2.Paths)
buf.WriteString("\npaths:")
buf.Write(indent(paths))
if len(v2.Definitions) > 0 {
definitions, _ := yaml.Marshal(v2.Definitions)
buf.WriteString("\ndefinitions:")
buf.Write(indent(definitions))
}
if len(v2.Parameters) > 0 {
parameters, _ := yaml.Marshal(v2.Parameters)
buf.WriteString("\nparameters:")
buf.Write(indent(parameters))
}
if len(v2.Responses) > 0 {
responses, _ := yaml.Marshal(v2.Responses)
buf.WriteString("\nresponses:")
buf.Write(indent(responses))
}
if len(v2.SecurityDefinitions) > 0 {
securityDefinitions, _ := yaml.Marshal(v2.SecurityDefinitions)
buf.WriteString("\nsecurityDefinitions:")
buf.Write(indent(securityDefinitions))
}
if len(v2.Security) > 0 {
security, _ := yaml.Marshal(v2.Security)
buf.WriteString("\nsecurity:")
buf.Write(indent(security))
}
if len(v2.Tags) > 0 {
tags, _ := yaml.Marshal(v2.Tags)
buf.WriteString("\ntags:")
buf.Write(indent(tags))
}
if v2.ExternalDocs != nil {
externalDocs, _ := yaml.Marshal(v2.ExternalDocs)
buf.WriteString("\nexternalDocs:")
buf.Write(indent(externalDocs))
}
if len(v2.Extensions) > 0 {
extensions, _ := yaml.Marshal(v2.Extensions)
buf.Write(extensions)
}
return buf.Bytes(), nil
}
func indent(s []byte) []byte {
split := bytes.Split(s, []byte{'\n'})
var buf = bytes.NewBuffer(nil)
for _, ele := range split {
buf.WriteString("\n")
buf.WriteString(" ")
buf.Write(ele)
}
return buf.Bytes()
}