diff --git a/app.go b/app.go index d69e27e..927a6c8 100644 --- a/app.go +++ b/app.go @@ -8,7 +8,7 @@ import ( "go.mongodb.org/mongo-driver/mongo/options" ) -const Version = "1.0.3" +const Version = "1.0.4" type Client struct { Db *mongo.Client // 驱动 diff --git a/time.go b/time.go index d5972de..32cb769 100644 --- a/time.go +++ b/time.go @@ -2,13 +2,49 @@ package gomongo import ( "github.com/dtapps/gotime" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/bsontype" "time" ) // BsonTime 类型 -type BsonTime string +type BsonTime time.Time -// BsonTime 时间类型 -func (c *Client) BsonTime(value time.Time) string { - return gotime.SetCurrent(value).Bson() +// Value 时间类型 +func (t BsonTime) Value() string { + return gotime.SetCurrent(time.Time(t)).Bson() +} + +// MarshalJSON 实现json序列化 +func (t BsonTime) MarshalJSON() ([]byte, error) { + //log.Println("MarshalJSON") + b := make([]byte, 0) + b = append(b, gotime.SetCurrent(time.Time(t)).Bson()...) + return b, nil +} + +// UnmarshalJSON 实现json反序列化 +func (t *BsonTime) UnmarshalJSON(data []byte) (err error) { + //log.Println("UnmarshalJSON") + t1 := gotime.SetCurrentParse(string(data)) + *t = BsonTime(t1.Time) + return +} + +// MarshalBSONValue 实现bson序列化 +func (t BsonTime) MarshalBSONValue() (bsontype.Type, []byte, error) { + //log.Println("MarshalBSONValue") + targetTime := gotime.SetCurrent(time.Time(t)).Bson() + return bson.MarshalValue(targetTime) +} + +// UnmarshalBSONValue 实现bson反序列化 +func (t *BsonTime) UnmarshalBSONValue(t2 bsontype.Type, data []byte) error { + //log.Println("UnmarshalBSONValue") + t1 := gotime.SetCurrentParse(string(data)) + //if string(data) == "" { + // return errors.New(fmt.Sprintf("%s, %s, %s", "读取数据失败:", t2, data)) + //} + *t = BsonTime(t1.Time) + return nil }