/
parser_yaml.go
109 lines (101 loc) · 2.55 KB
/
parser_yaml.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
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
type YAMLDoc struct {
Paths map[ /* path */ string]map[ /* method */ string]struct {
// Tags []string
Summary string
Description string
Parameters []struct {
In string
Name string
Description string
Schema YAMLSchemaDef
Required bool
}
RequestBody struct {
Required bool
Content map[ /* content type */ string]struct {
Schema struct {
Ref string `yaml:"$ref"`
}
}
} `yaml:"requestBody"`
Responses map[ /* status code */ string]struct {
Description string
Content map[ /* content type */ string]struct {
Schema struct {
Ref string `yaml:"$ref"`
}
}
}
}
Components struct {
Schemas YAMLSchemaDefs
}
}
// bool or []string
type YAMLRequiredUnmarshaller struct {
List []string
Bool bool
}
func (m *YAMLRequiredUnmarshaller) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case 0:
return nil
case yaml.ScalarNode:
var b bool
if err := node.Decode(&b); err != nil {
return fmt.Errorf("failed to decode in YAMLRequiredUnmarshaller as ScalarNode as bool: yamlNode: %+v: %w", node, err)
}
m.Bool = b
return nil
case yaml.SequenceNode:
var list []string
if err := node.Decode(&list); err != nil {
return fmt.Errorf("failed to decode in YAMLRequiredUnmarshaller as SequenceNode as []string: yamlNode: %+v: %w", node, err)
}
m.List = list
return nil
default:
return fmt.Errorf("invalid yaml node %+v", node)
}
}
type YAMLSchemaDef struct {
Name string
Type string
Required YAMLRequiredUnmarshaller
Description string
Format string
Example interface{}
// if type is object
Properties YAMLSchemaDefs
// if type is array
Items *YAMLSchemaDef
Ref RefDef `yaml:"$ref"`
}
type YAMLSchemaDefs []YAMLSchemaDef
func (d *YAMLSchemaDefs) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {
case 0:
return nil
case yaml.MappingNode:
list := make(YAMLSchemaDefs, 0, len(node.Content)/2)
for i := 0; i < len(node.Content); i += 2 {
var v YAMLSchemaDef
if err := node.Content[i+1].Decode(&v); err != nil {
return fmt.Errorf("failed to decode in YAMLSchemaDefs.UnmarshalYAML with YAMLSchemaDef type: yamlNode: %+v: %w", node, err)
}
if err := node.Content[i].Decode(&v.Name); err != nil {
return fmt.Errorf("failed to decode in YAMLSchemaDefs.UnmarshalYAML with YAMLSchemaDef.Name: yamlNode: %+v: %w", node, err)
}
list = append(list, v)
}
*d = list
return nil
default:
return fmt.Errorf("invalid yaml node %+v", node)
}
}