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.

panic when update using map: call of reflect.Value.Interface on zero Value #1407

Open
finelog opened this issue Aug 27, 2019 · 0 comments · May be fixed by #1448
Open

panic when update using map: call of reflect.Value.Interface on zero Value #1407

finelog opened this issue Aug 27, 2019 · 0 comments · May be fixed by #1448
Labels

Comments

@finelog
Copy link

finelog commented Aug 27, 2019

panic when update using map
reproduce example:

/*
CREATE TABLE `user` (
	`Fid` INT(10) UNSIGNED NOT NULL DEFAULT '0',
	`Fname` VARCHAR(64) NOT NULL DEFAULT '',
	`Fver` INT(10) UNSIGNED NOT NULL DEFAULT '0',
	PRIMARY KEY (`Fid`)
)
*/
type User struct {
	Id   uint64 `xorm:"PK"`
	Name string `xorm:""`
	Ver  uint64 `xorm:"version"`
}

mapper := core.NewPrefixMapper(core.SnakeMapper{}, "F")
engine.SetColumnMapper(mapper)

update := map[string]interface{}{
	"Fname": "user name",
	"Fver":  1,
}
rows, err := engine.Table(new(User)).ID(1).Update(update)
// panic: reflect: call of reflect.Value.Interface on zero Value

update := map[string]interface{}{
	"Name": "user name",
	"Ver":  1,
}
rows, err := engine.Table(new(User)).ID(1).Update(update)
// err, column `Ver` not exists

possible fix, xorm.io/core/column.go:

 // ValueOfV returns column's filed of struct's value accept reflevt value
 func (col *Column) ValueOfV(dataStruct *reflect.Value) (*reflect.Value, error) {
 	var fieldValue reflect.Value
 	fieldPath := strings.Split(col.FieldName, ".")
 
 	if dataStruct.Type().Kind() == reflect.Map {
+ 		fieldPath := strings.Split(col.Name, ".")
 		keyValue := reflect.ValueOf(fieldPath[len(fieldPath)-1])
@lunny lunny added the kind/bug label Aug 27, 2019
@lunny lunny linked a pull request Oct 2, 2019 that will close this issue
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants