Skip to content

Commit

Permalink
feat: add generic gorm scan/value funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Feb 22, 2023
1 parent 359fe2e commit 22702ab
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,34 @@ func (jm JSONMap) GormValue(ctx context.Context, db *gorm.DB) clause.Expr {
data, _ := jm.MarshalJSON()
return gorm.Expr("?", string(data))
}

// GenericStructValue can be set as the Value() func for any json struct
func GenericStructValue[T any](t T, defaultNull bool) (driver.Value, error) {
b, err := json.Marshal(t)
if err != nil {
return b, err
}
if defaultNull && string(b) == "{}" {
return nil, nil
}
return string(b), nil
}

// GenericStructScan can be set as the Scan(val) func for any json struct
func GenericStructScan[T any](t *T, val any) error {
if val == nil {
*t = *new(T)
return nil
}
var ba []byte
switch v := val.(type) {
case []byte:
ba = v
case string:
ba = []byte(v)
default:
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
}
err := json.Unmarshal(ba, &t)
return err
}

0 comments on commit 22702ab

Please sign in to comment.