Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

Commit

Permalink
- Adding xorm interface Conversion ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
fairyhunter13 committed Jan 28, 2021
1 parent 99f4d74 commit 2b2f99a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 39 additions & 0 deletions decimal_xorm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package decimal

import (
"database/sql/driver"
"errors"
)

// FromDB implements Conversion interface for xorm.
// This is needed because Decimal is a struct not a newtype pattern.
func (d *Decimal) FromDB(payload []byte) (err error) {
err = d.Scan(payload)
return
}

var (
// ErrEmptyDecimalString specifies the empty string of decimal value.
ErrEmptyDecimalString = errors.New("Invalid empty decimal string")
)

// ToDB implements Conversion interface for xorm.
// This is needed because Decimal is a struct not a newtype pattern.
func (d Decimal) ToDB() (payload []byte, err error) {
var result driver.Value
result, err = d.Value()
if err != nil {
return
}
var (
resultString string
ok bool
)
resultString, ok = result.(string)
if !ok {
err = ErrEmptyDecimalString
return
}
payload = []byte(resultString)
return
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/shopspring/decimal
module github.com/fairyhunter13/decimal

go 1.13
go 1.14

0 comments on commit 2b2f99a

Please sign in to comment.