From d3ffcfab400c95abd057a396d2fa9c10b3438f28 Mon Sep 17 00:00:00 2001 From: deoxxa Date: Fri, 30 Jan 2015 21:24:56 +1100 Subject: [PATCH 1/2] check for IsScanner on slices --- scope.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scope.go b/scope.go index 698ed0bb..076fc336 100644 --- a/scope.go +++ b/scope.go @@ -351,7 +351,9 @@ func (scope *Scope) fieldFromStruct(fieldStruct reflect.StructField, withRelatio case reflect.Slice: typ = typ.Elem() - if (typ.Kind() == reflect.Struct) && withRelation { + if field.IsScanner() { + field.IsNormal = true + } else if (typ.Kind() == reflect.Struct) && withRelation { if foreignKey == "" { foreignKey = scopeTyp.Name() + "Id" } From e4dc4574bff9eb035bfdf8c33be6bc804a82d24b Mon Sep 17 00:00:00 2001 From: deoxxa Date: Mon, 9 Feb 2015 09:42:07 +1100 Subject: [PATCH 2/2] add tests for scannable slice support --- slice_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 slice_test.go diff --git a/slice_test.go b/slice_test.go new file mode 100644 index 00000000..21410548 --- /dev/null +++ b/slice_test.go @@ -0,0 +1,70 @@ +package gorm_test + +import ( + "database/sql/driver" + "encoding/json" + "testing" +) + +func TestScannableSlices(t *testing.T) { + if err := DB.AutoMigrate(&RecordWithSlice{}).Error; err != nil { + t.Errorf("Should create table with slice values correctly: %s", err) + } + + r1 := RecordWithSlice{ + Strings: ExampleStringSlice{"a", "b", "c"}, + Structs: ExampleStructSlice{ + {"name1", "value1"}, + {"name2", "value2"}, + }, + } + + if err := DB.Save(&r1).Error; err != nil { + t.Errorf("Should save record with slice values") + } + + var r2 RecordWithSlice + + if err := DB.Find(&r2).Error; err != nil { + t.Errorf("Should fetch record with slice values") + } + + if len(r2.Strings) != 3 || r2.Strings[0] != "a" || r2.Strings[1] != "b" || r2.Strings[2] != "c" { + t.Errorf("Should have serialised and deserialised a string array") + } + + if len(r2.Structs) != 2 || r2.Structs[0].Name != "name1" || r2.Structs[0].Value != "value1" || r2.Structs[1].Name != "name2" || r2.Structs[1].Value != "value2" { + t.Errorf("Should have serialised and deserialised a struct array") + } +} + +type RecordWithSlice struct { + ID uint64 + Strings ExampleStringSlice `sql:"type:text"` + Structs ExampleStructSlice `sql:"type:text"` +} + +type ExampleStringSlice []string + +func (l ExampleStringSlice) Value() (driver.Value, error) { + return json.Marshal(l) +} + +func (l *ExampleStringSlice) Scan(input interface{}) error { + return json.Unmarshal(input.([]byte), l) +} + +type ExampleStruct struct { + Name string + Value string +} + +type ExampleStructSlice []ExampleStruct + +func (l ExampleStructSlice) Value() (driver.Value, error) { + return json.Marshal(l) +} + +func (l *ExampleStructSlice) Scan(input interface{}) error { + return json.Unmarshal(input.([]byte), l) +}