-
Notifications
You must be signed in to change notification settings - Fork 2
/
discriminator.go
137 lines (117 loc) · 3.28 KB
/
discriminator.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// Discriminator can be used to aid in serialization, deserialization, and
// validation of request bodies or response payloads which may be one of a
// number of different schemas. The discriminator is a specific object in a
// schema which is used to inform the consumer of the document of an alternative
// schema based on the value associated with it.
type Discriminator struct {
Extensions `json:"-"`
Location `json:"-"`
// The name of the property in the payload that will hold the discriminator
// value.
//
// *required
PropertyName Text `json:"propertyName"`
// An object to hold mappings between payload values and schema names or
// references.
Mapping *Map[Text] `json:"mapping,omitempty"`
}
func (d *Discriminator) Clone() *Discriminator {
if d == nil {
return nil
}
var m *Map[Text]
if d.Mapping != nil {
m := Map[Text]{
Items: make([]KeyValue[Text], len(d.Mapping.Items)),
}
copy(m.Items, d.Mapping.Items)
}
return &Discriminator{
Extensions: d.Extensions,
Location: Location{
absolute: *d.Location.absolute.Clone(),
relative: d.Location.relative,
},
PropertyName: d.PropertyName.Clone(),
Mapping: m,
}
}
func (d *Discriminator) setLocation(loc Location) error {
if d == nil {
return nil
}
d.Location = loc
return nil
}
// MarshalJSON marshals d into JSON
func (d Discriminator) MarshalJSON() ([]byte, error) {
type discriminator Discriminator
return marshalExtendedJSON(discriminator(d))
}
// UnmarshalJSON unmarshals json into d
func (d *Discriminator) UnmarshalJSON(data []byte) error {
type discriminator Discriminator
v := discriminator{}
if err := unmarshalExtendedJSON(data, &v); err != nil {
return err
}
*d = Discriminator(v)
return nil
}
func (d Discriminator) MarshalYAML() (interface{}, error) {
j, err := d.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML implements yaml.Unmarshaler
func (d *Discriminator) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, d)
}
func (d *Discriminator) Anchors() (*Anchors, error) { return nil, nil }
func (*Discriminator) Kind() Kind { return KindDiscriminator }
func (*Discriminator) Refs() []Ref { return nil }
// func (d *Discriminator) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return d.resolveNodeByPointer(ptr)
// }
// func (d *Discriminator) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return d, nil
// }
// tok, _ := ptr.NextToken()
// return nil, newErrNotResolvable(d.Location.AbsoluteLocation(), tok)
// }
func (d *Discriminator) Nodes() []Node {
if d == nil {
return nil
}
return downcastNodes(d.nodes())
}
func (d *Discriminator) nodes() []node { return nil }
func (d *Discriminator) isNil() bool { return d == nil }
func (*Discriminator) mapKind() Kind { return KindUndefined }
func (*Discriminator) sliceKind() Kind { return KindUndefined }
var _ node = (*Discriminator)(nil)