This repository has been archived by the owner on Jun 14, 2021. It is now read-only.
forked from shopspring/decimal
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Adding xorm interface Conversion ✨
- Loading branch information
1 parent
99f4d74
commit 2b2f99a
Showing
2 changed files
with
41 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |