From 8eec541d564075a42588eb1c15fd5f8f9a7e95fb Mon Sep 17 00:00:00 2001 From: Seth Shelnutt Date: Tue, 20 Nov 2018 15:45:56 -0500 Subject: [PATCH] Switch to ansi sql JSON types, and remove uuid sql type (#12) --- callbacks.go | 8 +++---- jsonb.go | 60 ---------------------------------------------------- loggable.go | 17 +++++++++------ util.go | 2 -- 4 files changed, 15 insertions(+), 72 deletions(-) delete mode 100644 jsonb.go diff --git a/callbacks.go b/callbacks.go index 7345272..88974c1 100644 --- a/callbacks.go +++ b/callbacks.go @@ -3,8 +3,8 @@ package loggable import ( "encoding/json" - "github.com/jinzhu/gorm" "github.com/gofrs/uuid" + "github.com/jinzhu/gorm" ) func (p *Plugin) addCreated(scope *gorm.Scope) { @@ -43,12 +43,12 @@ func addRecord(scope *gorm.Scope, action string) error { return err } cl := ChangeLog{ - ID: id.String(), + ID: id, Action: action, ObjectID: interfaceToString(scope.PrimaryKeyValue()), ObjectType: scope.GetModelStruct().ModelType.Name(), - RawObject: rawObject, - RawMeta: fetchChangeLogMeta(scope), + RawObject: string(rawObject), + RawMeta: string(fetchChangeLogMeta(scope)), } return scope.DB().Create(&cl).Error } diff --git a/jsonb.go b/jsonb.go deleted file mode 100644 index eafa619..0000000 --- a/jsonb.go +++ /dev/null @@ -1,60 +0,0 @@ -package loggable - -import ( - "bytes" - "database/sql/driver" - "encoding/json" - "errors" - "reflect" -) - -type JSONB []byte - -func (j JSONB) Value() (driver.Value, error) { - if j.IsNull() { - return nil, nil - } - return string(j), nil -} - -func (j *JSONB) Scan(value interface{}) error { - if value == nil { - *j = nil - return nil - } - s, ok := value.([]byte) - if !ok { - return errors.New("scan source is not bytes") - } - *j = append((*j)[0:0], s...) - return nil -} - -func (j JSONB) MarshalJSON() ([]byte, error) { - if j == nil { - return []byte("null"), nil - } - return j, nil -} - -func (j *JSONB) UnmarshalJSON(data []byte) error { - if j == nil { - return errors.New("json.RawMessage: UnmarshalJSON on nil pointer") - } - *j = append((*j)[0:0], data...) - return nil -} - -func (j JSONB) IsNull() bool { - return len(j) == 0 || string(j) == "null" -} - -func (j JSONB) Equals(j1 JSONB) bool { - return bytes.Equal([]byte(j), []byte(j1)) -} - -func (j JSONB) unmarshal(p reflect.Type) (interface{}, error) { - obj := reflect.New(p).Interface() - err := json.Unmarshal(j, obj) - return obj, err -} diff --git a/loggable.go b/loggable.go index a0442e6..fc012ed 100644 --- a/loggable.go +++ b/loggable.go @@ -6,6 +6,7 @@ import ( "reflect" "time" + "github.com/gofrs/uuid" "github.com/jinzhu/gorm" ) @@ -34,24 +35,28 @@ func (l LoggableModel) Enable(v bool) { l.Disabled = !v } // ChangeLog is a main entity, which used to log changes. type ChangeLog struct { - ID string `gorm:"type:uuid;primary_key;"` + ID uuid.UUID `gorm:"primary_key;"` CreatedAt time.Time `sql:"DEFAULT:current_timestamp"` Action string ObjectID string `gorm:"index"` ObjectType string `gorm:"index"` - RawObject JSONB `sql:"type:JSONB"` - RawMeta JSONB `sql:"type:JSONB"` + RawObject string `sql:"type:JSON"` + RawMeta string `sql:"type:JSON"` Object interface{} `sql:"-"` Meta interface{} `sql:"-"` } func (l *ChangeLog) prepareObject(objType reflect.Type) (err error) { - l.Object, err = l.RawObject.unmarshal(objType) + obj := reflect.New(objType).Interface() + err = json.Unmarshal([]byte(l.RawObject), obj) + l.Object = obj return } func (l *ChangeLog) prepareMeta(objType reflect.Type) (err error) { - l.Meta, err = l.RawMeta.unmarshal(objType) + obj := reflect.New(objType).Interface() + err = json.Unmarshal([]byte(l.RawMeta), obj) + l.Meta = obj return } @@ -64,7 +69,7 @@ func interfaceToString(v interface{}) string { } } -func fetchChangeLogMeta(scope *gorm.Scope) JSONB { +func fetchChangeLogMeta(scope *gorm.Scope) []byte { val, ok := scope.Value.(Interface) if !ok { return nil diff --git a/util.go b/util.go index cc44587..3e5ad5c 100644 --- a/util.go +++ b/util.go @@ -30,8 +30,6 @@ func somethingToMapStringInterface(item interface{}) map[string]interface{} { return nil } switch raw := item.(type) { - case JSONB: - return somethingToMapStringInterface([]byte(raw)) case string: return somethingToMapStringInterface([]byte(raw)) case []byte: