jalanga
jalanga

Reputation: 1576

How to save array of structs

I am trying to save an Array of Structs.
I tried:

type ShiftValue struct {
    Hour   uint8 `json:"hour"`
    Minute uint8 `json:"minute"`
}

type Shift struct {
    Start ShiftValue `json:"start"`
    End   ShiftValue `json:"end"`
}

type Config struct {
    ID                        uuid.UUID       `gorm:"type:uuid;primary_key;index;" json:"id"`
    CreatedAt                 time.Time       `json:"created_at"`
    UpdatedAt                 time.Time       `json:"updated_at"`
    DeletedAt                 *time.Time      `json:"deleted_at,omitempty"`
    Shifts                    []Shift         `gorm:"type:varchar(100)[];" json:"shifts,"`
}

But is not working. I also tried to save Shifts as pq.StringArray:

type Config struct {
    ID                        uuid.UUID       `gorm:"type:uuid;primary_key;index;" json:"id"`
    CreatedAt                 time.Time       `json:"created_at"`
    UpdatedAt                 time.Time       `json:"updated_at"`
    DeletedAt                 *time.Time      `json:"deleted_at,omitempty"`
    Shifts                    pq.StringArray  `gorm:"type:varchar(100)[];" json:"shifts,"`
}

Which is kind of working, but I don't know how can I convert a slice of Shift to StringArray.
Should I use GenericArrray?
How can I make the conversion from Slice to GenericArray or StringArray?

When I Unmarshall the data, I do it in the following struct, I validate the data, and after that I want to save it to Database:

type ConfigUpdate struct {
    Shifts                    []Shift         `json:"shifts,"`
}

Upvotes: 1

Views: 4444

Answers (1)

Umar Hayat
Umar Hayat

Reputation: 4991

The nearest scenario that can be seen in gorm embedded struct test is

package gorm_test

import "testing"

type BasePost struct {
    Id    int64
    Title string
    URL   string
}

type Author struct {
    ID    string
    Name  string
    Email string
}

type HNPost struct {
    BasePost
    Author  `gorm:"embedded_prefix:user_"` // Embedded struct
    Upvotes int32
}

type EngadgetPost struct {
    BasePost BasePost `gorm:"embedded"`
    Author   Author   `gorm:"embedded;embedded_prefix:author_"` // Embedded struct
    ImageUrl string
}

As can be seen, all of these Base Struct have Id to be referred to as foreign key in parent struct.

One more scenario that can be found handled in one of StackOverflow another answer.

type Children struct {
    Lat float64
    Lng float64
}

type ChildArray []Children

func (sla *ChildArray) Scan(src interface{}) error {
    return json.Unmarshal(src.([]byte), &sla)
}

func (sla ChildArray) Value() (driver.Value, error) {
    val, err := json.Marshal(sla)
    return string(val), err
}

type Parent struct {
    *gorm.Model    
    Childrens ChildArray `gorm:"column:childrens;type:longtext"`
}

Please verify it on your own as I don't have a gorm set up. I just did the R & D part. I hope, it will help many of us. thanks

Upvotes: 4

Related Questions