Skip to content
Merged
4 changes: 4 additions & 0 deletions dataType/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,7 @@ func (b *Bool) GobDecode(data []byte) error {
b.bool = data[0] != 0
return nil
}

func (b Bool) ToPtr() *Bool {
return &b
}
52 changes: 42 additions & 10 deletions dataType/date.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dataType

import (
"database/sql"
"database/sql/driver"
"strings"
"time"
Expand All @@ -13,6 +12,16 @@ import (
"helay.net/go/utils/v3/tools"
)

var timeFormat = formatter.FormatRule[time.Time]{
FormatType: "output_date",
InputRules: []string{
"timestamp",
"2006-01-02",
"2006-01-02 15",
"2006-01-02 15:04",
},
}

type CustomDate struct {
time.Time
}
Expand All @@ -23,10 +32,15 @@ func (c CustomDate) String() string {
}

// noinspection all
func (c *CustomDate) Scan(value interface{}) (err error) {
nullTime := &sql.NullTime{}
err = nullTime.Scan(value)
c.Time = nullTime.Time
func (c *CustomDate) Scan(value any) (err error) {
if value == nil {
return nil
}
t, err := timeFormat.Format(value)
if err != nil {
return err
}
c.Time = t
return
}

Expand Down Expand Up @@ -70,8 +84,7 @@ func (c *CustomDate) UnmarshalJSON(b []byte) (err error) {
//*this = CustomTime{}
return nil
}
tf := formatter.FormatRule[time.Time]{FormatType: "output_date"}
_t, err := tf.Format(s)
_t, err := timeFormat.Format(s)
if err != nil {
return err
}
Expand All @@ -98,9 +111,14 @@ func (c CustomTime) String() string {

// noinspection all
func (c *CustomTime) Scan(value interface{}) (err error) {
nullTime := &sql.NullTime{}
err = nullTime.Scan(value)
c.Time = nullTime.Time
if value == nil {
return nil
}
t, err := timeFormat.Format(value)
if err != nil {
return err
}
c.Time = t
return
}

Expand Down Expand Up @@ -155,6 +173,20 @@ func (c *CustomTime) UnmarshalJSON(b []byte) (err error) {
return err
}

// UnmarshalParam 从 query string 参数解析
func (c *CustomTime) UnmarshalParam(param string) error {
if param == "" {
return nil
}
tf := formatter.FormatRule[time.Time]{FormatType: "output_date"}
_t, err := tf.Format(param)
if err != nil {
return err
}
c.Time = _t
return nil
}

// noinspection all
func (c CustomTime) ToPtr() *CustomTime {
return &c
Expand Down
36 changes: 36 additions & 0 deletions dataType/int.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ func (Byte) GormDBDataType(db *gorm.DB, field *schema.Field) string {
return "int"
}

func (b Byte) ToInt() int {
return int(b)
}

func (b Byte) ToInt64() int64 {
return int64(b)
}

func (b Byte) ToUint64() uint64 {
return uint64(b)
}

func (b Byte) ToFloat64() float64 {
return float64(b)
}

func (b Byte) ToString() string {
return fmt.Sprintf("%d", b)
}

func (b Byte) ToBool() bool {
return b != 0
}

func (b Byte) ToInt32() int32 {
return int32(b)
}

func (b Byte) ToInt16() int16 {
return int16(b)
}

func (b Byte) ToByte() byte {
return byte(b)
}

type Uint64 struct {
uint64
}
Expand Down
80 changes: 48 additions & 32 deletions dataType/json_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"database/sql/driver"
"encoding/json"
"errors"

"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand All @@ -13,48 +14,36 @@ import (

// JSONMap defined JSON data type, need to implements driver.Valuer, sql.Scanner interface
// noinspection all
type JSONMap map[string]any
type JSONMap struct {
data map[string]any
}

func NewJSONMap(data map[string]any) JSONMap {
return JSONMap{data: data}
}

// Value return json value, implement driver.Valuer interface
// noinspection all
func (m JSONMap) Value() (driver.Value, error) {
return DriverValueWithJson(m)
return DriverValueWithJson(m.data)
}

// Scan scan value into Jsonb, implements sql.Scanner interface
// noinspection all
func (m *JSONMap) Scan(val any) error {
return DriverScanWithJson(val, m) // 这里暂时先用这个版本
//if val == nil {
// *m = make(JSONMap)
// return nil
//}
//var ba []byte
//switch v := val.(type) {
//case []byte:
// ba = v
//case string:
// ba = []byte(v)
//default:
// return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", val))
//}
//t := map[string]any{}
//rd := bytes.NewReader(ba)
//decoder := json.NewDecoder(rd)
//decoder.UseNumber()
//err := decoder.Decode(&t)
//*m = t
//return err
if m == nil {
return errors.New("JSONMap is nil")
}
return DriverScanWithJson(val, &m.data) // 这里暂时先用这个版本
}

// MarshalJSON to output non base64 encoded []byte
// noinspection all
func (m JSONMap) MarshalJSON() ([]byte, error) {
if m == nil {
if m.data == nil {
return []byte("null"), nil
}
t := (map[string]any)(m)
return json.Marshal(t)
return json.Marshal(m.data)
}

// UnmarshalJSON to deserialize []byte
Expand All @@ -64,10 +53,7 @@ func (m *JSONMap) UnmarshalJSON(b []byte) error {
rd := bytes.NewReader(b)
decoder := json.NewDecoder(rd)
decoder.UseNumber()
t := map[string]any{}
err := decoder.Decode(&t)
*m = t
return err
return decoder.Decode(&m.data)
}

// GormDataType gorm common data type
Expand All @@ -83,7 +69,37 @@ func (JSONMap) GormDBDataType(db *gorm.DB, field *schema.Field) string {
}

// noinspection all
func (jm JSONMap) GormValue(_ context.Context, db *gorm.DB) clause.Expr {
data, _ := jm.MarshalJSON()
func (m JSONMap) GormValue(_ context.Context, db *gorm.DB) clause.Expr {
data, _ := m.MarshalJSON()
return MapGormValue(string(data), db)
}

func (m *JSONMap) Add(k string, v any) {
if m == nil {
return
}
if m.data == nil {
m.data = make(map[string]any)
}
m.data[k] = v
}

func (m *JSONMap) Get(k string) (any, bool) {
if m == nil || m.data == nil {
return nil, false
}
v, ok := m.data[k]
return v, ok
}

func (m *JSONMap) Remove(k string) {
if m == nil || m.data == nil {
return
}

delete(m.data, k)
}

func (m *JSONMap) ToMap() map[string]any {
return m.data
}
Loading
Loading