Skip to content

Commit

Permalink
fix empty one-to-one strcut default value to null
Browse files Browse the repository at this point in the history
  • Loading branch information
fifsky committed Dec 7, 2018
1 parent e5067cf commit d0dc70b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ sudo: false
go:
- 1.9.x
- 1.10.x
- 1.11.x

matrix:
fast_finish: true
include:
- go: 1.11.x
env: GO111MODULE=on

services:
- mysql
Expand Down
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (b *Builder) reflectModel(autoTime []string) map[string]reflect.Value {
}

// Relation association table builder handle
func (b *Builder) Relation(fieldName string,fn func(b *Builder) *Builder) *Builder {
func (b *Builder) Relation(fieldName string,fn BuilderChainFunc) *Builder {
if b.wrapper.RelationMap == nil {
b.wrapper.RelationMap = make(map[string] BuilderChainFunc)
}
Expand Down
8 changes: 4 additions & 4 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,8 @@ func TestBuilder_NullString(t *testing.T) {

func TestBuilder_Relation1(t *testing.T) {
moment := &UserMoment{}
err := Model(moment).Relation("User" , func(b *Builder) *Builder {
return b.Where("gender = 1")
err := Model(moment).Relation("User" , func(b *Builder) {
b.Where("gender = 1")
}).Where("status = 1 and id = ?",14).Get()

b , _ :=json.MarshalIndent(moment,""," ")
Expand All @@ -564,8 +564,8 @@ func TestBuilder_Relation1(t *testing.T) {

func TestBuilder_Relation2(t *testing.T) {
var moments = make([]*UserMoment, 0)
err := Model(&moments).Relation("User" , func(b *Builder) *Builder {
return b.Where("gender = 0")
err := Model(&moments).Relation("User" , func(b *Builder) {
b.Where("gender = 0")
}).Where("status = 1").Limit(10).All()

b , _ :=json.MarshalIndent(moments,""," ")
Expand Down
17 changes: 4 additions & 13 deletions relation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ func RelationOne(data interface{} , chains map[string] BuilderChainFunc) error {

m := Model(foreignModel.Interface())
if chainFn , ok := chains[name] ; ok {
if m = chainFn(m) ; m == nil {
refVal.FieldByName(name).Set(reflect.MakeSlice(field.Type, 0, 0))
return nil
}
chainFn(m)
}

// batch get field values
Expand All @@ -69,9 +66,7 @@ func RelationOne(data interface{} , chains map[string] BuilderChainFunc) error {

m := Model(foreignModel.Interface())
if chainFn , ok := chains[name] ; ok {
if m = chainFn(m) ; m == nil {
return nil
}
chainFn(m)
}

err := m.Where(fmt.Sprintf("%s=?", relations[1]), mapper.FieldByName(refVal, relations[0]).Interface()).Get()
Expand Down Expand Up @@ -123,9 +118,7 @@ func RelationAll(data interface{} , chains map[string] BuilderChainFunc) error {

m := Model(foreignModel.Interface())
if chainFn , ok := chains[name] ; ok {
if m = chainFn(m) ; m == nil {
return nil
}
chainFn(m)
}

// batch get field values
Expand Down Expand Up @@ -169,9 +162,7 @@ func RelationAll(data interface{} , chains map[string] BuilderChainFunc) error {
m := Model(fi.Interface())

if chainFn , ok := chains[name] ; ok {
if m = chainFn(m) ; m == nil {
return nil
}
chainFn(m)
}

err := m.Where(fmt.Sprintf("%s in(?)", relations[1]), relVals).All()
Expand Down
1 change: 0 additions & 1 deletion relation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestRelationAll(t *testing.T) {
t.Fatal(err)
}


b , _ :=json.MarshalIndent(moments,""," ")
fmt.Println(string(b),err)

Expand Down
2 changes: 1 addition & 1 deletion wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var (
defaultWrapper = Use(Default)
)

type BuilderChainFunc func(b *Builder) *Builder
type BuilderChainFunc func(b *Builder)

type Wrapper struct {
database string
Expand Down
8 changes: 4 additions & 4 deletions wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ func TestTxx(t *testing.T) {

func TestWrapper_Relation(t *testing.T) {
moment := &UserMoment{}
err := Relation("User" , func(b *Builder) *Builder {
return b.Where("gender = 0")
err := Relation("User" , func(b *Builder) {
b.Where("gender = 0")
}).Get(moment , "select * from moments")

b , _ :=json.MarshalIndent(moment,""," ")
Expand All @@ -368,8 +368,8 @@ func TestWrapper_Relation(t *testing.T) {

func TestWrapper_Relation2(t *testing.T) {
var moments = make([]*UserMoment, 0)
err := Relation("User" , func(b *Builder) *Builder {
return b.Where("gender = 1")
err := Relation("User" , func(b *Builder) {
b.Where("gender = 1")
}).Select(&moments , "select * from moments")

if err != nil {
Expand Down

0 comments on commit d0dc70b

Please sign in to comment.