Skip to content

Commit

Permalink
Add JSONPtr and UntypedNil to dbutil
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 27, 2024
1 parent 953608f commit e5cb5e9
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions dbutil/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ func (j JSON) Value() (driver.Value, error) {
v, err := json.Marshal(j.Data)
return string(v), err
}

// JSONPtr is a convenience function for wrapping a pointer to a value in the JSON utility, but removing typed nils
// (i.e. preventing nils from turning into the string "null" in the database).
func JSONPtr[T any](val *T) JSON {
return JSON{Data: UntypedNil(val)}
}

func UntypedNil[T any](val *T) any {
if val == nil {
return nil
}
return val
}

0 comments on commit e5cb5e9

Please sign in to comment.