123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package utils
- import (
- "database/sql/driver"
- "fmt"
- "strconv"
- "strings"
- )
- type Long int64
- func (t Long) MarshalJSON() ([]byte, error) {
- b := fmt.Sprintf(`"%d"`, t)
- return []byte(b), nil
- }
- func (t *Long) UnmarshalJSON(value []byte) error {
- var v = strings.TrimSpace(strings.Trim(string(value), "\""))
- if v == "" {
- *t = Long(0)
- return nil
- }
- num, err := strconv.ParseInt(v, 10, 64)
- if err != nil {
- return err
- }
- *t = Long(num)
- return nil
- }
- func (t Long) MarshalText() ([]byte, error) {
- return []byte(t.String()), nil
- }
- func (t *Long) UnmarshalText(data []byte) error {
- *t = t.FromString(string(data))
- return nil
- }
- func (t Long) FromString(str string) Long {
- return ParseLong(str)
- }
- func ParseLong(str string) Long {
- str = strings.TrimSpace(str)
- num, err := strconv.ParseInt(str, 10, 64)
- if nil != err {
- return 0
- }
- return Long(num)
- }
- func (t Long) String() string {
- return strconv.FormatInt(int64(t), 10)
- }
- func (t Long) Value() (driver.Value, error) {
- if t.IsZero() {
- return int64(0), nil
- }
- return int64(t), nil
- }
- func (t *Long) Scan(value interface{}) error {
- if value == nil {
- return nil
- }
- if v, ok := value.(int64); ok {
- *t = Long(v)
- } else if v, ok := value.([]uint8); ok {
- num, err := strconv.ParseInt(string(v), 10, 64)
- if err == nil {
- *t = Long(num)
- }
- }
- return nil
- }
- func (t Long) IsZero() bool {
- return t == 0
- }
- // sort
- type ByLong []Long
- func (a ByLong) Len() int { return len(a) }
- func (a ByLong) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
- func (a ByLong) Less(i, j int) bool { return a[i] < a[j] }
|