forked from BoltApp/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modification_request.go
69 lines (62 loc) · 1.77 KB
/
modification_request.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 braintree
import (
"encoding/xml"
)
type ModificationRequest struct {
Amount *Decimal `xml:"amount,omitempty"`
NumberOfBillingCycles int `xml:"number-of-billing-cycles,omitempty"`
Quantity int `xml:"quantity,omitempty"`
NeverExpires bool `xml:"never-expires,omitempty"`
}
type AddModificationRequest struct {
ModificationRequest
InheritedFromID string `xml:"inherited-from-id,omitempty"`
}
type UpdateModificationRequest struct {
ModificationRequest
ExistingID string `xml:"existing-id,omitempty"`
}
type ModificationsRequest struct {
Add []AddModificationRequest
Update []UpdateModificationRequest
RemoveExistingIDs []string
}
func (m ModificationsRequest) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
type addSchema struct {
Type string `xml:"type,attr"`
Modifications []AddModificationRequest `xml:"modification"`
}
type updateSchema struct {
Type string `xml:"type,attr"`
Modifications []UpdateModificationRequest `xml:"modification"`
}
type removeSchema struct {
Type string `xml:"type,attr"`
ExistingIDs []string `xml:"modification"`
}
type schema struct {
Add *addSchema `xml:"add,omitempty"`
Update *updateSchema `xml:"update,omitempty"`
Remove *removeSchema `xml:"remove,omitempty"`
}
x := schema{}
if len(m.Add) > 0 {
x.Add = &addSchema{
Type: "array",
Modifications: m.Add,
}
}
if len(m.Update) > 0 {
x.Update = &updateSchema{
Type: "array",
Modifications: m.Update,
}
}
if len(m.RemoveExistingIDs) > 0 {
x.Remove = &removeSchema{
Type: "array",
ExistingIDs: m.RemoveExistingIDs,
}
}
return e.EncodeElement(x, start)
}