forked from braintree-go/braintree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'use-time-structs-for-times' of git://github.com/jszwedk…
…o/braintree-go into jszwedko-use-time-structs-for-times
- Loading branch information
Showing
10 changed files
with
125 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package braintree | ||
|
||
import ( | ||
"encoding/xml" | ||
"time" | ||
) | ||
|
||
// Date wraps a time object but handles deserializing dates returned from the Braintree API | ||
// e.g. "2014-02-09" | ||
type Date struct { | ||
time.Time | ||
} | ||
|
||
// UnmarshalXML interprets Braintree's date format from XML to initialize the Date | ||
// e.g. "2014-02-09" | ||
func (d *Date) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error { | ||
var v string | ||
dec.DecodeElement(&v, &start) | ||
|
||
parse, err := time.Parse("2006-01-02", v) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*d = Date{Time: parse} | ||
return nil | ||
} | ||
|
||
// MarshalXML Outputs the date in a format the Braintree expects to see date | ||
// e.g. "2014-02-09" | ||
func (d *Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | ||
return e.EncodeElement(d.Format("2006-01-02"), start) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package braintree | ||
|
||
import ( | ||
"encoding/xml" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDateUnmarshalXML(t *testing.T) { | ||
date := &Date{} | ||
|
||
dateXML := []byte(`<?xml version="1.0" encoding="UTF-8"?><foo>2014-02-09</foo></xml>`) | ||
if err := xml.Unmarshal(dateXML, date); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if date.Format("2006-01-02") != "2014-02-09" { | ||
t.Fatalf("expected 2014-02-09 got %s", date) | ||
} | ||
} | ||
|
||
func TestDateMarshalXML(t *testing.T) { | ||
date := &Date{Time: time.Date(2014, 2, 9, 0, 0, 0, 0, time.Local)} | ||
expected := `<Date>2014-02-09</Date>` | ||
|
||
b, err := xml.Marshal(date) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if string(b) != expected { | ||
t.Fatalf("expected %s got %s", expected, string(b)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
package braintree | ||
|
||
import "time" | ||
|
||
const ( | ||
ModificationKindDiscount = "discount" | ||
ModificationKindAddOn = "add_on" | ||
) | ||
|
||
type Modification struct { | ||
Id string `xml:"id,omitempty"` | ||
Amount float64 `xml:"amount,omitempty"` | ||
Description string `xml:"description,omitempty"` | ||
Kind string `xml:"kind,omitempty"` | ||
Name string `xml:"name,omitempty"` | ||
NeverExpires bool `xml:"never-expires,omitempty"` | ||
Quantity int `xml:"quantity,omitempty"` | ||
UpdatedAt string `xml:"updated_at,omitempty"` | ||
Id string `xml:"id,omitempty"` | ||
Amount float64 `xml:"amount,omitempty"` | ||
Description string `xml:"description,omitempty"` | ||
Kind string `xml:"kind,omitempty"` | ||
Name string `xml:"name,omitempty"` | ||
NeverExpires bool `xml:"never-expires,omitempty"` | ||
Quantity int `xml:"quantity,omitempty"` | ||
UpdatedAt *time.Time `xml:"updated_at,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters