How can I save/include an array of struct as a field of another struct but I don’t want to give it a table?

I currently have a bit of unique situation where I think I don’t want to make a table for a struct because it’s not gonna be used by other parts of the system other than the struct/table that needs it. Also the child struct only have a few fields, so I think I’d just save it as a string. Here’s the structs involved:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type LivestockSpecies int
const (
Chicken LivestockSpecies = iota + 1
Goat
Cattle
Carabao
Horse
Swine
)
type Sex int
const (
Male Sex = iota + 1
Female
)
type ShippingDocLivestockRecord struct {
LivestockSpecies LivestockSpecies `json:"livestock_species"`
HeadCount int32 `json:"head_count"`
Sex Sex `json:"sex"`
Remarks string `json:"remarks"`
}
type ShippingDoc struct {
gorm.Model
ID string `gorm:"primarykey; size:40;"`
ValidUntil string `json:"valid_until"`
QRCodeVal string `json:"qr_code_val"`
Destination User `gorm:"foreignKey:DestinationID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"destination"`
DestinationID string `gorm:"size:40; index; not null" json:"destination_id"`
IncludeVHCs []VHCApplication `gorm:"many2many:shipping_doc_included_vhcs;" json:"included_vhcs"`
IssuanceDate string `gorm:"column:issuance_date"`
ControlNumber string `json:"control_number"`
SharedToHauler User `gorm:"foreignKey:SharedToHaulerID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"shared_to_hauler"`
SharedToHaulerID string `gorm:"size:40; index; not null" json:"shared_to_hauler_id"`
Remarks string `json:"remarks"`
IssuedByProvetRep User `gorm:"foreignKey:IssuedByProvetRepID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"issued_by_provet_rep"`
IssuedByProvetRepID string `gorm:"size:40; index; not null" json:"issued_by_provet_rep_id"`
IsRequestedByGrower bool `json:"is_requested_by_grower"`
IsRequestedByHauler bool `json:"is_requested_by_hauler"`
Vehicle User `gorm:"foreignKey:VehicleID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"vehicle"`
VehicleID string `gorm:"size:40; index; not null" json:"vehicle_id"`
ShipmentDate string `json:"shipment_date"`
LivestockDetails []ShippingDocLivestockRecord `json:"livestock_details"`
}
</code>
<code>type LivestockSpecies int const ( Chicken LivestockSpecies = iota + 1 Goat Cattle Carabao Horse Swine ) type Sex int const ( Male Sex = iota + 1 Female ) type ShippingDocLivestockRecord struct { LivestockSpecies LivestockSpecies `json:"livestock_species"` HeadCount int32 `json:"head_count"` Sex Sex `json:"sex"` Remarks string `json:"remarks"` } type ShippingDoc struct { gorm.Model ID string `gorm:"primarykey; size:40;"` ValidUntil string `json:"valid_until"` QRCodeVal string `json:"qr_code_val"` Destination User `gorm:"foreignKey:DestinationID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"destination"` DestinationID string `gorm:"size:40; index; not null" json:"destination_id"` IncludeVHCs []VHCApplication `gorm:"many2many:shipping_doc_included_vhcs;" json:"included_vhcs"` IssuanceDate string `gorm:"column:issuance_date"` ControlNumber string `json:"control_number"` SharedToHauler User `gorm:"foreignKey:SharedToHaulerID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"shared_to_hauler"` SharedToHaulerID string `gorm:"size:40; index; not null" json:"shared_to_hauler_id"` Remarks string `json:"remarks"` IssuedByProvetRep User `gorm:"foreignKey:IssuedByProvetRepID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"issued_by_provet_rep"` IssuedByProvetRepID string `gorm:"size:40; index; not null" json:"issued_by_provet_rep_id"` IsRequestedByGrower bool `json:"is_requested_by_grower"` IsRequestedByHauler bool `json:"is_requested_by_hauler"` Vehicle User `gorm:"foreignKey:VehicleID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"vehicle"` VehicleID string `gorm:"size:40; index; not null" json:"vehicle_id"` ShipmentDate string `json:"shipment_date"` LivestockDetails []ShippingDocLivestockRecord `json:"livestock_details"` } </code>
type LivestockSpecies int

const (
    Chicken LivestockSpecies = iota + 1
    Goat
    Cattle
    Carabao
    Horse
    Swine
)

type Sex int

const (
    Male Sex = iota + 1
    Female
)

type ShippingDocLivestockRecord struct {
    LivestockSpecies LivestockSpecies `json:"livestock_species"`
    HeadCount        int32            `json:"head_count"`
    Sex              Sex              `json:"sex"`
    Remarks          string           `json:"remarks"`
}

type ShippingDoc struct {
    gorm.Model
    ID                  string                       `gorm:"primarykey; size:40;"`
    ValidUntil          string                       `json:"valid_until"`
    QRCodeVal           string                       `json:"qr_code_val"`
    Destination         User                         `gorm:"foreignKey:DestinationID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"destination"`
    DestinationID       string                       `gorm:"size:40; index; not null" json:"destination_id"`
    IncludeVHCs         []VHCApplication             `gorm:"many2many:shipping_doc_included_vhcs;" json:"included_vhcs"`
    IssuanceDate        string                       `gorm:"column:issuance_date"`
    ControlNumber       string                       `json:"control_number"`
    SharedToHauler      User                         `gorm:"foreignKey:SharedToHaulerID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"shared_to_hauler"`
    SharedToHaulerID    string                       `gorm:"size:40; index; not null" json:"shared_to_hauler_id"`
    Remarks             string                       `json:"remarks"`
    IssuedByProvetRep   User                         `gorm:"foreignKey:IssuedByProvetRepID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"issued_by_provet_rep"`
    IssuedByProvetRepID string                       `gorm:"size:40; index; not null" json:"issued_by_provet_rep_id"`
    IsRequestedByGrower bool                         `json:"is_requested_by_grower"`
    IsRequestedByHauler bool                         `json:"is_requested_by_hauler"`
    Vehicle             User                         `gorm:"foreignKey:VehicleID; constraint:OnUpdate:CASCADE,OnDelete:CASCADE" json:"vehicle"`
    VehicleID           string                       `gorm:"size:40; index; not null" json:"vehicle_id"`
    ShipmentDate        string                       `json:"shipment_date"`
    LivestockDetails    []ShippingDocLivestockRecord `json:"livestock_details"`
}

The compiler told me that I might need a valuer/scanner interface for my usecase, but I can’t make it work so far. Here’s what I currently have.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>type LivestockRecords []ShippingDocLivestockRecord
func (lr *LivestockRecords) Scan(value interface{}) (err error) {
var data []byte
switch v := value.(type) {
case []byte:
data = v
case string:
data = []byte(v)
case nil:
return nil
default:
return fmt.Errorf("unsupported type: %T", value)
}
if len(data) == 0 {
return nil
}
var sdlrArr []ShippingDocLivestockRecord
mErr := json.Unmarshal(data, &sdlrArr)
if mErr != nil {
panic(mErr)
}
for _, v := range sdlrArr {
*lr = append(*lr, v)
}
return mErr
}
func (lr LivestockRecords) Value() (driver.Value, error) {
if lr == nil {
return nil, nil
}
blr, err := json.Marshal(lr)
if err != nil {
panic(err)
}
return blr, nil
}
</code>
<code>type LivestockRecords []ShippingDocLivestockRecord func (lr *LivestockRecords) Scan(value interface{}) (err error) { var data []byte switch v := value.(type) { case []byte: data = v case string: data = []byte(v) case nil: return nil default: return fmt.Errorf("unsupported type: %T", value) } if len(data) == 0 { return nil } var sdlrArr []ShippingDocLivestockRecord mErr := json.Unmarshal(data, &sdlrArr) if mErr != nil { panic(mErr) } for _, v := range sdlrArr { *lr = append(*lr, v) } return mErr } func (lr LivestockRecords) Value() (driver.Value, error) { if lr == nil { return nil, nil } blr, err := json.Marshal(lr) if err != nil { panic(err) } return blr, nil } </code>
type LivestockRecords []ShippingDocLivestockRecord

func (lr *LivestockRecords) Scan(value interface{}) (err error) {
    var data []byte

    switch v := value.(type) {
    case []byte:
        data = v
    case string:
        data = []byte(v)
    case nil:
        return nil
    default:
        return fmt.Errorf("unsupported type: %T", value)
    }

    if len(data) == 0 {
        return nil
    }

    var sdlrArr []ShippingDocLivestockRecord

    mErr := json.Unmarshal(data, &sdlrArr)

    if mErr != nil {
        panic(mErr)
    }

    for _, v := range sdlrArr {
        *lr = append(*lr, v)
    }

    return mErr
}

func (lr LivestockRecords) Value() (driver.Value, error) {

    if lr == nil {
        return nil, nil
    }

    blr, err := json.Marshal(lr)

    if err != nil {
        panic(err)
    }

    return blr, nil
}

I’m not strongly against giving the child struct a table, but I just want to explore some new ways of doing things

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật