-
Notifications
You must be signed in to change notification settings - Fork 2
/
xml.go
146 lines (130 loc) · 3.78 KB
/
xml.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
138
139
140
141
142
143
144
145
146
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// XML is a metadata object that allows for more fine-tuned XML model
// definitions.
//
// When using arrays, XML element names are not inferred (for singular/plural
// forms) and the name property SHOULD be used to add that information. See
// examples for expected behavior.
type XML struct {
Extensions `json:"-"`
Location `json:"-"`
// Replaces the name of the element/attribute used for the described schema
// property. When defined within items, it will affect the name of the
// individual XML elements within the list. When defined alongside type
// being array (outside the items), it will affect the wrapping element and
// only if wrapped is true. If wrapped is false, it will be ignored.
Name Text `json:"name,omitempty"`
// The URI of the namespace definition. This MUST be in the form of an
// absolute URI.
Namespace Text `json:"namespace,omitempty"`
// The prefix to be used for the name.
Prefix Text `json:"prefix,omitempty"`
// Declares whether the property definition translates to an attribute
// instead of an element. Default value is false.
Attribute *bool `json:"attribute,omitempty"`
// MAY be used only for an array definition. Signifies whether the array is
// wrapped (for example, <books><book/><book/></books>) or unwrapped
// (<book/><book/>). Default value is false. The definition takes effect
// only when defined alongside type being array (outside the items).
Wrapped *bool `json:"wrapped,omitempty"`
}
func (xml *XML) Clone() *XML {
if xml == nil {
return nil
}
var a *bool
if xml.Attribute != nil {
*a = *xml.Attribute
}
var w *bool
if xml.Wrapped != nil {
*w = *xml.Wrapped
}
return &XML{
Extensions: cloneExtensions(xml.Extensions),
Location: xml.Location,
Name: xml.Name.Clone(),
Namespace: xml.Namespace.Clone(),
Prefix: xml.Prefix.Clone(),
Attribute: a,
Wrapped: w,
}
}
func (*XML) Anchors() (*Anchors, error) { return nil, nil }
// func (x *XML) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return x.resolveNodeByPointer(ptr)
// }
// func (x *XML) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return x, nil
// }
// tok, _ := ptr.NextToken()
// return nil, newErrNotResolvable(x.Location.AbsoluteLocation(), tok)
// }
func (*XML) Kind() Kind { return KindXML }
func (*XML) mapKind() Kind { return KindUndefined }
func (*XML) sliceKind() Kind { return KindUndefined }
func (x XML) MarshalJSON() ([]byte, error) {
type xml XML
return marshalExtendedJSON(xml(x))
}
func (x *XML) UnmarshalJSON(data []byte) error {
type xml XML
var v xml
err := unmarshalExtendedJSON(data, &v)
if err != nil {
return err
}
*x = XML(v)
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (xml XML) MarshalYAML() (interface{}, error) {
j, err := xml.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 satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (xml *XML) 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, xml)
}
func (xml *XML) setLocation(loc Location) error {
if xml == nil {
return nil
}
xml.Location = loc
return nil
}
func (xml *XML) isNil() bool { return xml == nil }
func (xml *XML) Refs() []Ref { return nil }
func (xml *XML) Nodes() []Node {
if xml == nil {
return nil
}
return downcastNodes(xml.nodes())
}
func (xml *XML) nodes() []node { return nil }
var _ node = (*XML)(nil)