From 004c80c25c0ced9f08fd7dacce4c069434e68be6 Mon Sep 17 00:00:00 2001 From: renshaohai83 Date: Wed, 18 Oct 2017 16:48:42 +0800 Subject: [PATCH] To support if DDL is upgraded first and application could be upgraded smoothly To support if DDL is upgraded first and application could be upgraded smoothly --- queries/reflect.go | 8 ++++++-- queries/reflect_test.go | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/queries/reflect.go b/queries/reflect.go index 9c55b336e..1e04a6d68 100644 --- a/queries/reflect.go +++ b/queries/reflect.go @@ -279,8 +279,8 @@ ColLoop: continue ColLoop } } - - return nil, errors.Errorf("could not find struct field name in mapping: %s", name) + // if c doesn't exist in the model, the pointer will be the zero value in the ptrs array and it's value will be thrown away + continue } return ptrs, nil @@ -309,6 +309,10 @@ func ValuesFromMapping(val reflect.Value, mapping []uint64) []interface{} { // ptrFromMapping expects to be passed an addressable struct that it's looking // for things on. func ptrFromMapping(val reflect.Value, mapping uint64, addressOf bool) reflect.Value { + if mapping == 0 { + var ignored interface{} + return reflect.ValueOf(&ignored) + } for i := 0; i < 8; i++ { v := (mapping >> uint(i*8)) & sentinel diff --git a/queries/reflect_test.go b/queries/reflect_test.go index 8d83ed35b..bad403d36 100644 --- a/queries/reflect_test.go +++ b/queries/reflect_test.go @@ -283,8 +283,7 @@ func TestValuesFromMapping(t *testing.T) { IntP: new(int), }, } - - mapping := []uint64{testMakeMapping(0), testMakeMapping(1), testMakeMapping(2, 0), testMakeMapping(2, 1)} + mapping := []uint64{testMakeMapping(0), testMakeMapping(1), testMakeMapping(2, 0), testMakeMapping(2, 1), 0} v := ValuesFromMapping(reflect.Indirect(reflect.ValueOf(val)), mapping) if got := v[0].(int); got != 5 { @@ -299,6 +298,9 @@ func TestValuesFromMapping(t *testing.T) { if got := v[3].(int); got != 0 { t.Error("nested pointer was wrong:", got) } + if got := *v[4].(*interface{}); got != nil { + t.Error("nil pointer was not be ignored:", got) + } } func TestPtrsFromMapping(t *testing.T) {