Default value for slice

Is there a way to declare a default value for slice like this?

type SoftwareComponent struct {
    Id             int                      `json:"id"`
    Name           string                   `json:"name"`
    Version        string                   `json:"version"`
    Licenses       dbtypes.StringSlice      `gorm:"default:['UNKNOWN']"`    // <-----  ???
}

Remark: dbtypes.StringSlice is []string with implemented Value() (driver.Value, error) and Scan(val interface{}) error

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

( ... ) Value() (driver.Value, error) {
    // set value if fieldValue is null or empty
}

Comment From: nikhrom

but then the default value will be the same for all structures that use fields with the dbtypes.StringSlice type. And it is necessary that it be possible to assign a default value for a specific field of a specific struct

Comment From: a631807682

parse field.DefaultValue from string to string slice yourself

Comment From: nikhrom

Wouldn't it be difficult to show how to do it? I can't figure out exactly how to get the DefaultValue and where to parse it.

Thank you for your time

Comment From: a631807682

type struct .. {
    Licenses  dbtypes.StringSlice      `gorm:"default:['UNKNOWN']"`
}
func (StringSlice) Value(ctx context.Context, field *Field, dst reflect.Value, fieldValue interface{}) (interface{}, error) {
    // now field.DefaultValue is "['UNKNOWN']"

    // pseudo code, please use reflect
    if fieldValue == nil {
        return []string{"UNKNOWN"}, nil
    }
}

Comment From: nikhrom

Thanks!!!