From 49042d480c6f1854bec74e19cba7d8e370346b7b Mon Sep 17 00:00:00 2001 From: John Guo Date: Wed, 18 May 2022 11:05:05 +0800 Subject: [PATCH] improve data converting for package gdb, drivers/pgsql --- contrib/drivers/pgsql/pgsql.go | 18 ------------------ database/gdb/gdb_core_structure.go | 27 ++++++++++++++++++++------- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/contrib/drivers/pgsql/pgsql.go b/contrib/drivers/pgsql/pgsql.go index d474ecc73..d223ad2d6 100644 --- a/contrib/drivers/pgsql/pgsql.go +++ b/contrib/drivers/pgsql/pgsql.go @@ -15,7 +15,6 @@ package pgsql import ( "context" "database/sql" - "database/sql/driver" "fmt" _ "github.com/lib/pq" @@ -246,20 +245,3 @@ func (d *Driver) DoInsert(ctx context.Context, link gdb.Link, table string, list return d.Core.DoInsert(ctx, link, table, list, option) } } - -// ConvertDataForRecord converting for any data that will be inserted into table/collection as a record. -func (d *Driver) ConvertDataForRecord(ctx context.Context, value interface{}) map[string]interface{} { - data := gdb.DataToMapDeep(value) - var err error - for k, v := range data { - if valuer, ok := v.(driver.Valuer); ok { - data[k], err = valuer.Value() - if err != nil { - return nil - } - } else { - data[k] = d.Core.ConvertDataForRecordValue(ctx, v) - } - } - return data -} diff --git a/database/gdb/gdb_core_structure.go b/database/gdb/gdb_core_structure.go index aeef81aa0..2c952e591 100644 --- a/database/gdb/gdb_core_structure.go +++ b/database/gdb/gdb_core_structure.go @@ -8,6 +8,7 @@ package gdb import ( "context" + "database/sql/driver" "reflect" "strings" "time" @@ -35,6 +36,18 @@ func (c *Core) ConvertDataForRecord(ctx context.Context, value interface{}) map[ } func (c *Core) ConvertDataForRecordValue(ctx context.Context, value interface{}) interface{} { + var ( + err error + convertedValue interface{} + ) + // If `value` implements interface `driver.Valuer`, it then uses the interface for value converting. + if valuer, ok := value.(driver.Valuer); ok { + if convertedValue, err = valuer.Value(); err != nil { + panic(err) + } + return convertedValue + } + // Default value converting. var ( rvValue = reflect.ValueOf(value) rvKind = rvValue.Kind() @@ -48,7 +61,7 @@ func (c *Core) ConvertDataForRecordValue(ctx context.Context, value interface{}) // It should ignore the bytes type. if _, ok := value.([]byte); !ok { // Convert the value to JSON. - value, _ = json.Marshal(value) + convertedValue, _ = json.Marshal(value) } case reflect.Struct: @@ -57,17 +70,17 @@ func (c *Core) ConvertDataForRecordValue(ctx context.Context, value interface{}) // which will insert/update the value to database as "null". case time.Time: if r.IsZero() { - value = nil + convertedValue = nil } case gtime.Time: if r.IsZero() { - value = nil + convertedValue = nil } case *gtime.Time: if r.IsZero() { - value = nil + convertedValue = nil } case *time.Time: @@ -79,14 +92,14 @@ func (c *Core) ConvertDataForRecordValue(ctx context.Context, value interface{}) default: // Use string conversion in default. if s, ok := value.(iString); ok { - value = s.String() + convertedValue = s.String() } else { // Convert the value to JSON. - value, _ = json.Marshal(value) + convertedValue, _ = json.Marshal(value) } } } - return value + return convertedValue } // convertFieldValueToLocalValue automatically checks and converts field value from database type