-
Notifications
You must be signed in to change notification settings - Fork 28
/
read.go
64 lines (55 loc) · 1.48 KB
/
read.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
package openapi2
import (
"encoding/json"
"os"
"regexp"
"strings"
"sigs.k8s.io/yaml"
)
func ReadOpenAPI2SpecFile(filename string) (*Specification, error) {
spec, err := ReadOpenAPI2SpecFileDirect(filename)
return &spec, err
}
func ReadSwagger2SpecFile(filepath string) (Specification, error) {
return ReadOpenAPI2SpecFileDirect(filepath)
}
func ReadOpenAPI2SpecFileDirect(filename string) (Specification, error) {
var spec Specification
bytes, err := os.ReadFile(filename)
if err != nil {
return spec, err
}
rx := regexp.MustCompile(`.ya?ml$`)
if rx.MatchString(strings.ToLower(strings.TrimSpace(filename))) {
err = yaml.Unmarshal(bytes, &spec)
} else {
err = json.Unmarshal(bytes, &spec)
}
return spec, err
}
/*func ReadSwagger2Spec(filepath string) (Specification, error) {
bytes, err := os.ReadFile(filepath)
if err != nil {
return Specification{}, err
}
return NewSpecificationFromBytes(bytes)
}*/
func ReadOpenAPI2KinSpecFile(filename string) (*Spec, error) {
var swag Spec
bytes, err := os.ReadFile(filename)
if err != nil {
return &swag, err
}
if FilenameIsYAML(filename) {
err = yaml.Unmarshal(bytes, &swag)
} else {
err = json.Unmarshal(bytes, &swag)
}
return &swag, err
}
var rxYAMLExtension = regexp.MustCompile(`.ya?ml$`)
// FilenameIsYAML checks to see if a filename ends
// in `.yml` or `.yaml` with a case-insensitive match.
func FilenameIsYAML(filename string) bool {
return rxYAMLExtension.MatchString(strings.ToLower(strings.TrimSpace(filename)))
}