Advertisement
Guest User

KarmaDecimal

a guest
Oct 18th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.54 KB | None | 0 0
  1. type KarmaDecimal struct {
  2.     decimal.Decimal
  3. }
  4.  
  5. // Scan implements the sql.Scanner interface for database deserialization.
  6. func (d *KarmaDecimal) Scan(value interface{}) error {
  7.     // first try to see if the data is stored in database as a Numeric datatype
  8.     switch v := value.(type) {
  9.  
  10.     case float32:
  11.         old_d := decimal.NewFromFloat32(v)
  12.         *d = KarmaDecimal{old_d}
  13.         return nil
  14.     case sql.NullFloat64:
  15.         old_d := decimal.NewFromFloat(v.Float64)
  16.         *d = KarmaDecimal{old_d}
  17.         return nil
  18.     default:
  19.         return d.Decimal.Scan(value)
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement