-
Notifications
You must be signed in to change notification settings - Fork 0
/
oas3.go
114 lines (85 loc) · 1.91 KB
/
oas3.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
package manager
import (
"errors"
"gopkg.in/yaml.v2"
"io/ioutil"
)
type OAS3 struct {
}
func (instance OAS3) GetSupportTypes() []string {
return []string{"version"}
}
func (instance OAS3) GetVersion(object string, filename string) (string, error) {
if object != "version" {
return "", errors.New("object[name='" + object + "'] isn't supported")
}
binary, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(binary, &m)
if err != nil {
return "", err
}
for _, each := range m {
if each.Key == "info" {
for _, s := range each.Value.(yaml.MapSlice) {
if s.Key == "version" {
return s.Value.(string), nil
}
}
}
}
return "1.0.0", nil
}
func (instance OAS3) SetVersion(object string, filename string, value string) error {
if object != "version" {
return errors.New("object[name='" + object + "'] isn't supported")
}
binary, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(binary, &m)
if err != nil {
return err
}
infoIndex := -1
var infoObject yaml.MapSlice
for index, each := range m {
if each.Key == "info" {
infoIndex = index
infoObject = each.Value.(yaml.MapSlice)
}
}
if infoObject == nil {
infoObject = make([]yaml.MapItem, 0)
}
isAssigned := false
for index, item := range infoObject {
if item.Key == "version" {
infoObject[index] = yaml.MapItem{Key: "version", Value: value}
isAssigned = true
break
}
}
if !isAssigned {
infoObject = append(infoObject, yaml.MapItem{Key: "version", Value: value})
}
if infoIndex == -1 {
m = append(m, yaml.MapItem{Key: "info", Value: infoObject})
} else {
m[infoIndex] = yaml.MapItem{Key: "info", Value: infoObject}
}
binary, err = yaml.Marshal(m)
if err != nil {
return err
}
err = ioutil.WriteFile(filename, binary, 0644)
if err != nil {
return err
}
return nil
}