123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- package model
- import (
- "encoding/json"
- "time"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- type Effect struct {
- ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
- ForeignID string `json:"foreignId" bson:"foreignId,omitempty"`
- CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"`
- LastModifiedAt time.Time `json:"lastModifiedAt" bson:"lastModifiedAt,omitempty"`
- EffectType string `json:"effectType" bson:"effectType,omitempty"`
- Manufacturer string `json:"manufacturer" bson:"manufacturer,omitempty"`
- Model string `json:"model" bson:"model,omitempty"`
- Tags []string `json:"tags" bson:"tags,omitempty"`
- Comment string `json:"comment" bson:"comment,omitempty"`
- Image string `json:"image" bson:"image,omitempty"`
- Connector string `json:"connector" bson:"connector,omitempty"`
- Voltage string `json:"voltage" bson:"voltage,omitempty"`
- Current string `json:"current" bson:"current,omitempty"`
- }
- func NewEffect() Effect {
- effect := Effect{
- Tags: make([]string, 0),
- }
- return effect
- }
- //UnmarshallJSON unmarschal function
- func (s *Effect) UnmarshalJSON(data []byte) error {
- var dat map[string]interface{}
- if err := json.Unmarshal(data, &dat); err != nil {
- return err
- }
- if dat["id"] != nil {
- id, _ := primitive.ObjectIDFromHex(dat["id"].(string))
- s.ID = id
- }
- if dat["foreignId"] != nil {
- s.ForeignID = dat["foreignId"].(string)
- }
- if dat["createdAt"] != nil {
- switch v := dat["createdAt"].(type) {
- case string:
- layout := "2006-01-02T15:04:05.000Z"
- s.CreatedAt, _ = time.Parse(layout, v)
- case float64:
- s.CreatedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
- }
- }
- if dat["lastModifiedAt"] != nil {
- switch v := dat["lastModifiedAt"].(type) {
- case string:
- layout := "2006-01-02T15:04:05.000Z"
- s.LastModifiedAt, _ = time.Parse(layout, v)
- case float64:
- s.LastModifiedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
- }
- }
- if dat["voltage"] != nil {
- s.Manufacturer = dat["voltage"].(string)
- }
- if dat["manufacturer"] != nil {
- s.Manufacturer = dat["manufacturer"].(string)
- }
- if dat["tags"] != nil {
- values := dat["tags"].([]interface{})
- s.Tags = make([]string, len(values))
- for i, d := range values {
- s.Tags[i] = d.(string)
- }
- }
- if dat["image"] != nil {
- s.Model = dat["image"].(string)
- }
- if dat["model"] != nil {
- s.Model = dat["model"].(string)
- }
- if dat["comment"] != nil {
- s.Comment = dat["comment"].(string)
- }
- if dat["connector"] != nil {
- s.Connector = dat["connector"].(string)
- }
- if dat["current"] != nil {
- s.Current = dat["current"].(string)
- }
- if dat["effectType"] != nil {
- s.EffectType = dat["effectType"].(string)
- }
- return nil
- }
|