-
Notifications
You must be signed in to change notification settings - Fork 0
/
of.go
69 lines (58 loc) · 1.13 KB
/
of.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
package chioas
import (
"github.com/go-andiamo/chioas/yaml"
)
type OfType uint
const (
OneOf OfType = iota
AnyOf
AllOf
)
func (ot OfType) TagName() string {
switch ot {
case AllOf:
return tagNameAllOf
case AnyOf:
return tagNameAnyOf
}
return tagNameOneOf
}
// Ofs is a representation of OAS oneOf, allOf or anyOf
type Ofs struct {
// OfType is the type - can be OneOf (default), AnyOf or AllOf
OfType OfType
// Of is the ordered slice of OfSchema items
//
// an OfSchema can be either a *Schema or an OfRef
//
// Note: the Ofs will not be written if the Of is empty!
Of []OfSchema
}
func (ofs *Ofs) writeYaml(w yaml.Writer) {
if len(ofs.Of) > 0 {
w.WriteTagStart(ofs.OfType.TagName())
for _, of := range ofs.Of {
if of.IsRef() {
writeItemRef(tagNameSchemas, of.Ref(), w)
} else {
of.Schema().writeOfYaml(w)
}
}
w.WriteTagEnd()
}
}
type OfSchema interface {
IsRef() bool
Ref() string
Schema() *Schema
}
type OfRef string
func (o OfRef) Ref() string {
return string(o)
}
func (o OfRef) Schema() *Schema {
panic("OfRef does not contain schema")
}
func (o OfRef) IsRef() bool {
return true
}