schematic.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package model
  2. import (
  3. "encoding/json"
  4. "time"
  5. "go.mongodb.org/mongo-driver/bson/primitive"
  6. )
  7. type Schematic 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. Manufacturer string `json:"manufacturer" bson:"manufacturer,omitempty"`
  13. Model string `json:"model" bson:"model,omitempty"`
  14. SubTitle string `json:"subtitle" bson:"subtitle,omitempty"`
  15. Tags []string `json:"tags" bson:"tags,omitempty"`
  16. Description string `json:"description" bson:"description,omitempty"`
  17. PrivateFile bool `json:"privateFile" bson:"privateFile,omitempty"`
  18. Owner string `json:"owner" bson:"owner,omitempty"`
  19. Files map[string]string `json:"files" bson:"files,omitempty"`
  20. BuildIn time.Time `json:"buildIn" bson:"buildIn,omitempty"`
  21. BuildTO time.Time `json:"buildTO" bson:"buildTO,omitempty"`
  22. }
  23. //UnmarshalJSON unmarshall a json to a schematic with some user properties
  24. func (s *Schematic) UnmarshalJSON(data []byte) error {
  25. var dat map[string]interface{}
  26. if err := json.Unmarshal(data, &dat); err != nil {
  27. return err
  28. }
  29. if dat["id"] != nil {
  30. id, _ := primitive.ObjectIDFromHex(dat["id"].(string))
  31. s.ID = id
  32. }
  33. if dat["foreignId"] != nil {
  34. s.ForeignID = dat["foreignId"].(string)
  35. }
  36. if dat["createdAt"] != nil {
  37. switch v := dat["createdAt"].(type) {
  38. case string:
  39. layout := "2006-01-02T15:04:05.000Z"
  40. s.CreatedAt, _ = time.Parse(layout, v)
  41. case float64:
  42. s.CreatedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
  43. }
  44. }
  45. if dat["lastModifiedAt"] != nil {
  46. switch v := dat["lastModifiedAt"].(type) {
  47. case string:
  48. layout := "2006-01-02T15:04:05.000Z"
  49. s.LastModifiedAt, _ = time.Parse(layout, v)
  50. case float64:
  51. s.LastModifiedAt = time.Unix(0, int64(v)*int64(time.Millisecond))
  52. }
  53. }
  54. if dat["manufacturer"] != nil {
  55. s.Manufacturer = dat["manufacturer"].(string)
  56. }
  57. if dat["model"] != nil {
  58. s.Model = dat["model"].(string)
  59. }
  60. if dat["subtitle"] != nil {
  61. s.SubTitle = dat["subtitle"].(string)
  62. }
  63. if dat["tags"] != nil {
  64. values := dat["tags"].([]interface{})
  65. s.Tags = make([]string, len(values))
  66. for i, d := range values {
  67. s.Tags[i] = d.(string)
  68. }
  69. }
  70. if dat["description"] != nil {
  71. s.Description = dat["description"].(string)
  72. }
  73. if dat["privateFile"] != nil {
  74. switch v := dat["privateFile"].(type) {
  75. case float64:
  76. s.PrivateFile = v > 0
  77. case int:
  78. s.PrivateFile = v > 0
  79. case bool:
  80. s.PrivateFile = v
  81. }
  82. }
  83. if dat["owner"] != nil {
  84. s.Owner = dat["owner"].(string)
  85. }
  86. if dat["files"] != nil {
  87. switch v := dat["files"].(type) {
  88. case []interface{}:
  89. values := v
  90. s.Files = make(map[string]string)
  91. for _, d := range values {
  92. s.Files[d.(string)] = ""
  93. }
  94. case map[string]interface{}:
  95. values := v
  96. s.Files = make(map[string]string)
  97. for k, d := range values {
  98. s.Files[k] = d.(string)
  99. }
  100. }
  101. }
  102. // if dat["buildIn"] != nil {
  103. // s.BuildIn = dat["buildIn"].(string)
  104. // }
  105. // if dat["buildTO"] != nil {
  106. // s.BuildTO = dat["buildTO"].(string)
  107. // }
  108. return nil
  109. }