diff --git a/types/date.go b/types/date.go
index 6ad852f6..96302520 100644
--- a/types/date.go
+++ b/types/date.go
@@ -2,6 +2,7 @@ package types
import (
"encoding/json"
+ "encoding/xml"
"time"
)
@@ -29,6 +30,23 @@ func (d *Date) UnmarshalJSON(data []byte) error {
return nil
}
+func (d Date) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
+ return encoder.EncodeElement(d.Time.Format(DateFormat), start)
+}
+
+func (d *Date) UnmarshalXML(decoder *xml.Decoder, start xml.StartElement) error {
+ var dateStr string
+ if err := decoder.DecodeElement(&dateStr, &start); err != nil {
+ return err
+ }
+ parsed, err := time.Parse(DateFormat, dateStr)
+ if err != nil {
+ return err
+ }
+ d.Time = parsed
+ return nil
+}
+
func (d Date) String() string {
return d.Time.Format(DateFormat)
}
diff --git a/types/date_test.go b/types/date_test.go
index 21177652..4683bffb 100644
--- a/types/date_test.go
+++ b/types/date_test.go
@@ -2,6 +2,7 @@ package types
import (
"encoding/json"
+ "encoding/xml"
"fmt"
"testing"
"time"
@@ -32,6 +33,29 @@ func TestDate_UnmarshalJSON(t *testing.T) {
assert.Equal(t, testDate, b.DateField.Time)
}
+func TestDate_MarshalXML(t *testing.T) {
+ testDate := time.Date(2019, 4, 1, 0, 0, 0, 0, time.UTC)
+ b := struct {
+ DateField Date `xml:"date"`
+ }{
+ DateField: Date{testDate},
+ }
+ xmlBytes, err := xml.Marshal(b)
+ assert.NoError(t, err)
+ assert.Equal(t, `2019-04-01`, string(xmlBytes))
+}
+
+func TestDate_UnmarshalXML(t *testing.T) {
+ testDate := time.Date(2019, 4, 1, 0, 0, 0, 0, time.UTC)
+ xmlStr := `2019-04-01`
+ b := struct {
+ DateField Date `xml:"date"`
+ }{}
+ err := xml.Unmarshal([]byte(xmlStr), &b)
+ assert.NoError(t, err)
+ assert.Equal(t, testDate, b.DateField.Time)
+}
+
func TestDate_Stringer(t *testing.T) {
t.Run("nil date", func(t *testing.T) {
var d *Date