schematic.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Manufacturer string `json:"manufacturer" bson:"manufacturer,omitempty"`
  10. Model string `json:"model" bson:"model,omitempty"`
  11. SubTitle string `json:"subtitle" bson:"subtitle,omitempty"`
  12. Tags []string `json:"tags" bson:"tags,omitempty"`
  13. Description string `json:"description" bson:"description,omitempty"`
  14. PrivateFile bool `json:"privateFile" bson:"privateFile,omitempty"`
  15. Owner string `json:"owner" bson:"Owner,omitempty"`
  16. Files []string `json:"files" bson:"files,omitempty"`
  17. ForeignId string `json:"foreignId" bson:"foreignId,omitempty"`
  18. CreatedAt time.Time `json:"createdAt" bson:"createdAt,omitempty"`
  19. LastModifiedAt time.Time `json:"lastModifiedAt" bson:"lastModifiedAt,omitempty"`
  20. BuildIn time.Time `json:"buildIn" bson:"buildIn,omitempty"`
  21. BuildTO time.Time `json:"buildTO" bson:"buildTO,omitempty"`
  22. }
  23. func (s *Schematic) UnmarshalJSON(data []byte) error {
  24. var dat map[string]interface{}
  25. if err := json.Unmarshal(data, &dat); err != nil {
  26. return err
  27. }
  28. if dat["id"] != nil {
  29. id, _ := primitive.ObjectIDFromHex(dat["id"].(string))
  30. s.ID = id
  31. }
  32. if dat["manufacturer"] != nil {
  33. s.Manufacturer = dat["manufacturer"].(string)
  34. }
  35. if dat["model"] != nil {
  36. s.Model = dat["model"].(string)
  37. }
  38. if dat["subtitle"] != nil {
  39. s.SubTitle = dat["subtitle"].(string)
  40. }
  41. if dat["tags"] != nil {
  42. values := dat["tags"].([]interface{})
  43. s.Tags = make([]string, len(values))
  44. for i, d := range values {
  45. s.Tags[i] = d.(string)
  46. }
  47. }
  48. if dat["description"] != nil {
  49. s.Description = dat["description"].(string)
  50. }
  51. if dat["privateFile"] != nil {
  52. switch v := dat["privateFile"].(type) {
  53. case float64:
  54. s.PrivateFile = v > 0
  55. case int:
  56. s.PrivateFile = v > 0
  57. case bool:
  58. s.PrivateFile = v
  59. }
  60. }
  61. if dat["owner"] != nil {
  62. s.Owner = dat["owner"].(string)
  63. }
  64. if dat["files"] != nil {
  65. values := dat["files"].([]interface{})
  66. s.Files = make([]string, len(values))
  67. for i, d := range values {
  68. s.Files[i] = d.(string)
  69. }
  70. }
  71. if dat["foreignId"] != nil {
  72. s.ForeignId = dat["foreignId"].(string)
  73. }
  74. if dat["createdAt"] != nil {
  75. s.CreatedAt = time.Unix(0, int64(dat["createdAt"].(float64))*int64(time.Millisecond))
  76. }
  77. if dat["lastModifiedAt"] != nil {
  78. s.LastModifiedAt = time.Unix(0, int64(dat["lastModifiedAt"].(float64))*int64(time.Millisecond))
  79. }
  80. // if dat["buildIn"] != nil {
  81. // s.BuildIn = dat["buildIn"].(string)
  82. // }
  83. // if dat["buildTO"] != nil {
  84. // s.BuildTO = dat["buildTO"].(string)
  85. // }
  86. return nil
  87. }