effect.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package model
  2. import (
  3. "encoding/json"
  4. "time"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  6. )
  7. type Effect struct {
  8. ID primitive.ObjectID `json:"id,omitempty" bson:"_id,omitempty"`
  9. ForeignID string `json:"foreignId" bson:"foreignId,omitempty"`
  10. CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"`
  11. LastModifiedAt time.Time `json:"lastModifiedAt" bson:"lastModifiedAt,omitempty"`
  12. EffectType string `json:"effectType" bson:"effectType,omitempty"`
  13. Manufacturer string `json:"manufacturer" bson:"manufacturer,omitempty"`
  14. Model string `json:"model" bson:"model,omitempty"`
  15. Tags []string `json:"tags" bson:"tags,omitempty"`
  16. Comment string `json:"comment" bson:"comment,omitempty"`
  17. Image string `json:"image" bson:"image,omitempty"`
  18. Connector string `json:"connector" bson:"connector,omitempty"`
  19. Voltage string `json:"voltage" bson:"voltage,omitempty"`
  20. Current string `json:"current" bson:"current,omitempty"`
  21. }
  22. func NewEffect() Effect {
  23. effect := Effect{
  24. Tags: make([]string, 0),
  25. }
  26. return effect
  27. }
  28. //UnmarshallJSON unmarschal function
  29. func (s *Effect) UnmarshalJSON(data []byte) error {
  30. var dat map[string]interface{}
  31. if err := json.Unmarshal(data, &dat); err != nil {
  32. return err
  33. }
  34. if dat["id"] != nil {
  35. id, _ := primitive.ObjectIDFromHex(dat["id"].(string))
  36. s.ID = id
  37. }
  38. if dat["foreignId"] != nil {
  39. s.ForeignID = dat["foreignId"].(string)
  40. }
  41. if dat["createdAt"] != nil {
  42. switch v := dat["createdAt"].(type) {
  43. case string:
  44. layout := "2006-01-02T15:04:05.000Z"
  45. s.CreatedAt, _ = time.Parse(layout, v)
  46. case float64:
  47. s.CreatedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
  48. }
  49. }
  50. if dat["lastModifiedAt"] != nil {
  51. switch v := dat["lastModifiedAt"].(type) {
  52. case string:
  53. layout := "2006-01-02T15:04:05.000Z"
  54. s.LastModifiedAt, _ = time.Parse(layout, v)
  55. case float64:
  56. s.LastModifiedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
  57. }
  58. }
  59. if dat["voltage"] != nil {
  60. s.Manufacturer = dat["voltage"].(string)
  61. }
  62. if dat["manufacturer"] != nil {
  63. s.Manufacturer = dat["manufacturer"].(string)
  64. }
  65. if dat["tags"] != nil {
  66. values := dat["tags"].([]interface{})
  67. s.Tags = make([]string, len(values))
  68. for i, d := range values {
  69. s.Tags[i] = d.(string)
  70. }
  71. }
  72. if dat["image"] != nil {
  73. s.Model = dat["image"].(string)
  74. }
  75. if dat["model"] != nil {
  76. s.Model = dat["model"].(string)
  77. }
  78. if dat["comment"] != nil {
  79. s.Comment = dat["comment"].(string)
  80. }
  81. if dat["connector"] != nil {
  82. s.Connector = dat["connector"].(string)
  83. }
  84. if dat["current"] != nil {
  85. s.Current = dat["current"].(string)
  86. }
  87. if dat["effectType"] != nil {
  88. s.EffectType = dat["effectType"].(string)
  89. }
  90. return nil
  91. }