-
Notifications
You must be signed in to change notification settings - Fork 43
/
entry.go
195 lines (165 loc) · 3.93 KB
/
entry.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package addr
import (
"encoding/xml"
"github.com/PaloAltoNetworks/pango/util"
"github.com/PaloAltoNetworks/pango/version"
)
// Entry is a normalized, version independent representation of an address
// object.
type Entry struct {
Name string
Value string
Type string
Description string
Tags []string // ordered
}
// Copy copies the information from source Entry `s` to this object. As the
// Name field relates to the XPATH of this object, this field is not copied.
func (o *Entry) Copy(s Entry) {
o.Value = s.Value
o.Type = s.Type
o.Description = s.Description
o.Tags = s.Tags
}
/** Structs / functions for normalization. **/
func (o Entry) Specify(v version.Number) (string, interface{}) {
_, fn := versioning(v)
return o.Name, fn(o)
}
type normalizer interface {
Normalize() []Entry
Names() []string
}
type container_v1 struct {
Answer []entry_v1 `xml:"entry"`
}
func (o *container_v1) Names() []string {
ans := make([]string, 0, len(o.Answer))
for i := range o.Answer {
ans = append(ans, o.Answer[i].Name)
}
return ans
}
func (o *container_v1) Normalize() []Entry {
ans := make([]Entry, 0, len(o.Answer))
for i := range o.Answer {
ans = append(ans, o.Answer[i].normalize())
}
return ans
}
type container_v2 struct {
Answer []entry_v2 `xml:"entry"`
}
func (o *container_v2) Names() []string {
ans := make([]string, 0, len(o.Answer))
for i := range o.Answer {
ans = append(ans, o.Answer[i].Name)
}
return ans
}
func (o *container_v2) Normalize() []Entry {
ans := make([]Entry, 0, len(o.Answer))
for i := range o.Answer {
ans = append(ans, o.Answer[i].normalize())
}
return ans
}
type entry_v1 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
IpNetmask *valType `xml:"ip-netmask"`
IpRange *valType `xml:"ip-range"`
Fqdn *valType `xml:"fqdn"`
Description string `xml:"description"`
Tags *util.MemberType `xml:"tag"`
}
type valType struct {
Value string `xml:",chardata"`
}
func specify_v1(e Entry) interface{} {
ans := entry_v1{
Name: e.Name,
Description: e.Description,
Tags: util.StrToMem(e.Tags),
}
vt := &valType{e.Value}
switch e.Type {
case IpNetmask:
ans.IpNetmask = vt
case IpRange:
ans.IpRange = vt
case Fqdn:
ans.Fqdn = vt
}
return ans
}
func (e *entry_v1) normalize() Entry {
ans := Entry{
Name: e.Name,
Description: e.Description,
Tags: util.MemToStr(e.Tags),
}
switch {
case e.IpNetmask != nil:
ans.Type = IpNetmask
ans.Value = e.IpNetmask.Value
case e.IpRange != nil:
ans.Type = IpRange
ans.Value = e.IpRange.Value
case e.Fqdn != nil:
ans.Type = Fqdn
ans.Value = e.Fqdn.Value
}
return ans
}
type entry_v2 struct {
XMLName xml.Name `xml:"entry"`
Name string `xml:"name,attr"`
IpNetmask *valType `xml:"ip-netmask"`
IpRange *valType `xml:"ip-range"`
Fqdn *valType `xml:"fqdn"`
IpWildcard *valType `xml:"ip-wildcard"`
Description string `xml:"description"`
Tags *util.MemberType `xml:"tag"`
}
func (e *entry_v2) normalize() Entry {
ans := Entry{
Name: e.Name,
Description: e.Description,
Tags: util.MemToStr(e.Tags),
}
switch {
case e.IpNetmask != nil:
ans.Type = IpNetmask
ans.Value = e.IpNetmask.Value
case e.IpRange != nil:
ans.Type = IpRange
ans.Value = e.IpRange.Value
case e.Fqdn != nil:
ans.Type = Fqdn
ans.Value = e.Fqdn.Value
case e.IpWildcard != nil:
ans.Type = IpWildcard
ans.Value = e.IpWildcard.Value
}
return ans
}
func specify_v2(e Entry) interface{} {
ans := entry_v2{
Name: e.Name,
Description: e.Description,
Tags: util.StrToMem(e.Tags),
}
vt := &valType{e.Value}
switch e.Type {
case IpNetmask:
ans.IpNetmask = vt
case IpRange:
ans.IpRange = vt
case Fqdn:
ans.Fqdn = vt
case IpWildcard:
ans.IpWildcard = vt
}
return ans
}