浏览代码

add time unmarshal

sainw 6 年之前
父节点
当前提交
104b78ad4e
共有 1 个文件被更改,包括 31 次插入1 次删除
  1. 31 1
      vo.go

+ 31 - 1
vo.go

@@ -1,6 +1,9 @@
 package syncer
 
-import "time"
+import (
+	"encoding/json"
+	"time"
+)
 
 const (
 	SYNC_ACTION_INSERT = "Insert"
@@ -46,3 +49,30 @@ type ClientPriceUpdate struct {
 	RecCreatedAt  time.Time `json:"rec_create_at"`
 	RecUpdatedAt  time.Time `json:"rec_update_at"`
 }
+
+// Custom unmarshal for date time of the record
+func (s *ClientPriceUpdate) UnmarshalJSON(data []byte) error {
+	type Alias ClientPriceUpdate
+	aux := &struct {
+		RecCreatedAt string `json:"rec_create_at"`
+		RecUpdatedAt string `json:"rec_update_at"`
+		*Alias
+	}{
+		Alias: (*Alias)(s),
+	}
+	if err := json.Unmarshal(data, &aux); err != nil {
+		return err
+	}
+	createdAt, e := time.Parse("2006-01-02 15:04:05.999999999-07:00", aux.RecCreatedAt)
+	if e != nil {
+		return e
+	}
+	updatedAt, e := time.Parse("2006-01-02 15:04:05.999999999-07:00", aux.RecUpdatedAt)
+	if e != nil {
+		return e
+	}
+
+	s.RecCreatedAt = createdAt
+	s.RecUpdatedAt = updatedAt
+	return nil
+}