123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package model
- import (
- "encoding/json"
- "time"
- "go.mongodb.org/mongo-driver/bson/primitive"
- )
- type Schematic 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"`
- Manufacturer string `json:"manufacturer" bson:"manufacturer,omitempty"`
- Model string `json:"model" bson:"model,omitempty"`
- SubTitle string `json:"subtitle" bson:"subtitle,omitempty"`
- Tags []string `json:"tags" bson:"tags,omitempty"`
- Description string `json:"description" bson:"description,omitempty"`
- PrivateFile bool `json:"privateFile" bson:"privateFile,omitempty"`
- Owner string `json:"owner" bson:"owner,omitempty"`
- Files map[string]string `json:"files" bson:"files,omitempty"`
- BuildIn time.Time `json:"buildIn" bson:"buildIn,omitempty"`
- BuildTO time.Time `json:"buildTO" bson:"buildTO,omitempty"`
- }
- //UnmarshalJSON unmarshall a json to a schematic with some user properties
- func (s *Schematic) 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["manufacturer"] != nil {
- s.Manufacturer = dat["manufacturer"].(string)
- }
- if dat["model"] != nil {
- s.Model = dat["model"].(string)
- }
- if dat["subtitle"] != nil {
- s.SubTitle = dat["subtitle"].(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["description"] != nil {
- s.Description = dat["description"].(string)
- }
- if dat["privateFile"] != nil {
- switch v := dat["privateFile"].(type) {
- case float64:
- s.PrivateFile = v > 0
- case int:
- s.PrivateFile = v > 0
- case bool:
- s.PrivateFile = v
- }
- }
- if dat["owner"] != nil {
- s.Owner = dat["owner"].(string)
- }
- if dat["files"] != nil {
- switch v := dat["files"].(type) {
- case []interface{}:
- values := v
- s.Files = make(map[string]string)
- for _, d := range values {
- s.Files[d.(string)] = ""
- }
- case map[string]interface{}:
- values := v
- s.Files = make(map[string]string)
- for k, d := range values {
- s.Files[k] = d.(string)
- }
- }
- }
- // if dat["buildIn"] != nil {
- // s.BuildIn = dat["buildIn"].(string)
- // }
- // if dat["buildTO"] != nil {
- // s.BuildTO = dat["buildTO"].(string)
- // }
- return nil
- }
|