Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

Commit

Permalink
make json as interface (#1248)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Mar 29, 2019
1 parent 1c2d47e commit 5ef12ea
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 35 deletions.
9 changes: 4 additions & 5 deletions engine_cond.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package xorm

import (
"database/sql/driver"
"encoding/json"
"fmt"
"reflect"
"strings"
Expand Down Expand Up @@ -147,7 +146,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
} else {
if col.SQLType.IsJson() {
if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand All @@ -156,7 +155,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
} else if col.SQLType.IsBlob() {
var bytes []byte
var err error
bytes, err = json.Marshal(fieldValue.Interface())
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand Down Expand Up @@ -195,7 +194,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
}

if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand All @@ -212,7 +211,7 @@ func (engine *Engine) buildConds(table *core.Table, bean interface{},
continue
}
} else {
bytes, err = json.Marshal(fieldValue.Interface())
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand Down
31 changes: 31 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2019 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package xorm

import "encoding/json"

// JSONInterface represents an interface to handle json data
type JSONInterface interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(data []byte, v interface{}) error
}

var (
// DefaultJSONHandler default json handler
DefaultJSONHandler = StdJSON{}
)

// StdJSON implements JSONInterface via encoding/json
type StdJSON struct{}

// Marshal implements JSONInterface
func (StdJSON) Marshal(v interface{}) ([]byte, error) {
return json.Marshal(v)
}

// Unmarshal implements JSONInterface
func (StdJSON) Unmarshal(data []byte, v interface{}) error {
return json.Unmarshal(data, v)
}
19 changes: 9 additions & 10 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package xorm
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
Expand Down Expand Up @@ -491,13 +490,13 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
continue
}
if fieldValue.CanAddr() {
err := json.Unmarshal(bs, fieldValue.Addr().Interface())
err := DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
if err != nil {
return nil, err
}
} else {
x := reflect.New(fieldType)
err := json.Unmarshal(bs, x.Interface())
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return nil, err
}
Expand All @@ -521,13 +520,13 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
hasAssigned = true
if len(bs) > 0 {
if fieldValue.CanAddr() {
err := json.Unmarshal(bs, fieldValue.Addr().Interface())
err := DefaultJSONHandler.Unmarshal(bs, fieldValue.Addr().Interface())
if err != nil {
return nil, err
}
} else {
x := reflect.New(fieldType)
err := json.Unmarshal(bs, x.Interface())
err := DefaultJSONHandler.Unmarshal(bs, x.Interface())
if err != nil {
return nil, err
}
Expand All @@ -543,7 +542,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
hasAssigned = true
if col.SQLType.IsText() {
x := reflect.New(fieldType)
err := json.Unmarshal(vv.Bytes(), x.Interface())
err := DefaultJSONHandler.Unmarshal(vv.Bytes(), x.Interface())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -658,7 +657,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
hasAssigned = true
x := reflect.New(fieldType)
if len([]byte(vv.String())) > 0 {
err := json.Unmarshal([]byte(vv.String()), x.Interface())
err := DefaultJSONHandler.Unmarshal([]byte(vv.String()), x.Interface())
if err != nil {
return nil, err
}
Expand All @@ -668,7 +667,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
hasAssigned = true
x := reflect.New(fieldType)
if len(vv.Bytes()) > 0 {
err := json.Unmarshal(vv.Bytes(), x.Interface())
err := DefaultJSONHandler.Unmarshal(vv.Bytes(), x.Interface())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -804,7 +803,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
case core.Complex64Type:
var x complex64
if len([]byte(vv.String())) > 0 {
err := json.Unmarshal([]byte(vv.String()), &x)
err := DefaultJSONHandler.Unmarshal([]byte(vv.String()), &x)
if err != nil {
return nil, err
}
Expand All @@ -814,7 +813,7 @@ func (session *Session) slice2Bean(scanResults []interface{}, fields []string, b
case core.Complex128Type:
var x complex128
if len([]byte(vv.String())) > 0 {
err := json.Unmarshal([]byte(vv.String()), &x)
err := DefaultJSONHandler.Unmarshal([]byte(vv.String()), &x)
if err != nil {
return nil, err
}
Expand Down
21 changes: 10 additions & 11 deletions session_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package xorm
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -103,7 +102,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
case reflect.Complex64, reflect.Complex128:
x := reflect.New(fieldType)
if len(data) > 0 {
err := json.Unmarshal(data, x.Interface())
err := DefaultJSONHandler.Unmarshal(data, x.Interface())
if err != nil {
session.engine.logger.Error(err)
return err
Expand All @@ -117,7 +116,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
if col.SQLType.IsText() {
x := reflect.New(fieldType)
if len(data) > 0 {
err := json.Unmarshal(data, x.Interface())
err := DefaultJSONHandler.Unmarshal(data, x.Interface())
if err != nil {
session.engine.logger.Error(err)
return err
Expand All @@ -130,7 +129,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
} else {
x := reflect.New(fieldType)
if len(data) > 0 {
err := json.Unmarshal(data, x.Interface())
err := DefaultJSONHandler.Unmarshal(data, x.Interface())
if err != nil {
session.engine.logger.Error(err)
return err
Expand Down Expand Up @@ -259,7 +258,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
case core.Complex64Type.Kind():
var x complex64
if len(data) > 0 {
err := json.Unmarshal(data, &x)
err := DefaultJSONHandler.Unmarshal(data, &x)
if err != nil {
session.engine.logger.Error(err)
return err
Expand All @@ -270,7 +269,7 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
case core.Complex128Type.Kind():
var x complex128
if len(data) > 0 {
err := json.Unmarshal(data, &x)
err := DefaultJSONHandler.Unmarshal(data, &x)
if err != nil {
session.engine.logger.Error(err)
return err
Expand Down Expand Up @@ -604,14 +603,14 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
}

if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
session.engine.logger.Error(err)
return 0, err
}
return string(bytes), nil
} else if col.SQLType.IsBlob() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
session.engine.logger.Error(err)
return 0, err
Expand All @@ -620,7 +619,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
}
return nil, fmt.Errorf("Unsupported type %v", fieldValue.Type())
case reflect.Complex64, reflect.Complex128:
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
session.engine.logger.Error(err)
return 0, err
Expand All @@ -632,7 +631,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
}

if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
session.engine.logger.Error(err)
return 0, err
Expand All @@ -645,7 +644,7 @@ func (session *Session) value2Interface(col *core.Column, fieldValue reflect.Val
(fieldValue.Type().Elem().Kind() == reflect.Uint8) {
bytes = fieldValue.Bytes()
} else {
bytes, err = json.Marshal(fieldValue.Interface())
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
session.engine.logger.Error(err)
return 0, err
Expand Down
7 changes: 3 additions & 4 deletions statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package xorm

import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -408,7 +407,7 @@ func (statement *Statement) buildUpdates(bean interface{},
} else {
// Blank struct could not be as update data
if requiredField || !isStructZero(fieldValue) {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
panic(fmt.Sprintf("mashal %v failed", fieldValue.Interface()))
}
Expand Down Expand Up @@ -437,7 +436,7 @@ func (statement *Statement) buildUpdates(bean interface{},
}

if col.SQLType.IsText() {
bytes, err := json.Marshal(fieldValue.Interface())
bytes, err := DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand All @@ -457,7 +456,7 @@ func (statement *Statement) buildUpdates(bean interface{},
fieldType.Elem().Kind() == reflect.Uint8 {
val = fieldValue.Slice(0, 0).Interface()
} else {
bytes, err = json.Marshal(fieldValue.Interface())
bytes, err = DefaultJSONHandler.Marshal(fieldValue.Interface())
if err != nil {
engine.logger.Error(err)
continue
Expand Down
9 changes: 4 additions & 5 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package xorm

import (
"encoding/json"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -117,21 +116,21 @@ type ConvConfig struct {
}

func (s *ConvConfig) FromDB(data []byte) error {
return json.Unmarshal(data, s)
return DefaultJSONHandler.Unmarshal(data, s)
}

func (s *ConvConfig) ToDB() ([]byte, error) {
return json.Marshal(s)
return DefaultJSONHandler.Marshal(s)
}

type SliceType []*ConvConfig

func (s *SliceType) FromDB(data []byte) error {
return json.Unmarshal(data, s)
return DefaultJSONHandler.Unmarshal(data, s)
}

func (s *SliceType) ToDB() ([]byte, error) {
return json.Marshal(s)
return DefaultJSONHandler.Marshal(s)
}

type ConvStruct struct {
Expand Down

0 comments on commit 5ef12ea

Please sign in to comment.