From e313827f047d7649853d504f8bd25d651efb9e8d Mon Sep 17 00:00:00 2001 From: Shirou WAKAYAMA Date: Wed, 19 Nov 2014 12:44:57 +0900 Subject: [PATCH] fix panic problem when the struct has unexported field. --- scope_private.go | 4 ++++ structs_test.go | 13 +++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/scope_private.go b/scope_private.go index 0e2308e9..2d99e81d 100644 --- a/scope_private.go +++ b/scope_private.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "errors" "fmt" + "go/ast" "reflect" "regexp" "strconv" @@ -550,6 +551,9 @@ func (scope *Scope) createTable() *Scope { fields := scope.Fields() scopeType := scope.IndirectValue().Type() for i := 0; i < scopeType.NumField(); i++ { + if !ast.IsExported(scopeType.Field(i).Name) { + continue + } for _, field := range scope.fieldFromStruct(scopeType.Field(i), false) { field = fields[field.DBName] if field.IsNormal { diff --git a/structs_test.go b/structs_test.go index 9b2dd5a8..621dfda4 100644 --- a/structs_test.go +++ b/structs_test.go @@ -125,12 +125,13 @@ func (i *Num) Scan(src interface{}) error { } type Animal struct { - Counter uint64 `gorm:"primary_key:yes"` - Name string `sql:"DEFAULT:'galeone'"` - From string //test reserved sql keyword as field name - Age time.Time `sql:"DEFAULT:current_timestamp"` - CreatedAt time.Time - UpdatedAt time.Time + Counter uint64 `gorm:"primary_key:yes"` + Name string `sql:"DEFAULT:'galeone'"` + From string //test reserved sql keyword as field name + Age time.Time `sql:"DEFAULT:current_timestamp"` + unexported string // unexported value + CreatedAt time.Time + UpdatedAt time.Time } type Post struct {