go-xorm / xorm

Simple and Powerful ORM for Go, support mysql,postgres,tidb,sqlite3,mssql,oracle, Moved to https://gitea.com/xorm/xorm
BSD 3-Clause "New" or "Revised" License
6.66k stars 754 forks source link

自定义类型报错:cannot return value obtained from unexported #1302

Open leeson1995 opened 5 years ago

leeson1995 commented 5 years ago

这是 sql:: image 这是 struct:: image

这里面的 decimal是自定义的 struct: image

然后爆这个错

    panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

goroutine 50 [running]:
testing.tRunner.func1(0xc00018e400)
    /usr/local/go/src/testing/testing.go:830 +0x388
panic(0x158ac80, 0x1722ac0)
    /usr/local/go/src/runtime/panic.go:522 +0x1b5
reflect.valueInterface(0x1572ec0, 0xc00038e128, 0x36, 0x1572e01, 0xc00038e128, 0x36)
    /usr/local/go/src/reflect/value.go:990 +0x1bf
reflect.Value.Interface(...)
    /usr/local/go/src/reflect/value.go:979
github.com/go-xorm/xorm.(*Engine).mapType(0xc000164750, 0x1615600, 0xc00038e128, 0x199, 0x1615600, 0xc00038e128, 0xc0000e3400)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/engine.go:1003 +0x9ee
github.com/go-xorm/xorm.(*Engine).autoMapType(0xc000164750, 0x1615600, 0xc00038e128, 0x199, 0x0, 0x0, 0x0)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/engine.go:817 +0x154
github.com/go-xorm/xorm.(*Session).slice2Bean(0xc000111340, 0xc0004a2300, 0x4, 0x4, 0xc0004a2280, 0x4, 0x4, 0x157c000, 0xc00038e120, 0xc0000e3b68, ...)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/session.go:668 +0x5b44
github.com/go-xorm/xorm.(*Session).nocacheGet(0xc000111340, 0x19, 0xc0001d2600, 0x157c000, 0xc00038e120, 0xc00014c5a0, 0x58, 0xc0004a0270, 0x1, 0x1, ...)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/session_get.go:131 +0x4a8
github.com/go-xorm/xorm.(*Session).cacheGet(0xc000111340, 0x157c000, 0xc00038e120, 0xc00014c5a0, 0x58, 0xc0004a0270, 0x1, 0x1, 0x0, 0x172d280, ...)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/session_get.go:221 +0x10fa
github.com/go-xorm/xorm.(*Session).get(0xc000111340, 0x157c000, 0xc00038e120, 0xc0004a0120, 0x1, 0x1)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/session_get.go:63 +0xad4
github.com/go-xorm/xorm.(*Session).Get(0xc000111340, 0x157c000, 0xc00038e120, 0xc0004a0100, 0x0, 0x0)
    /Users/leeson/gopath/pkg/mod/github.com/go-xorm/xorm@v0.7.1/session_get.go:23 +0x59
trading-system/account/server/repository/mysql.(*accountRepository).GetAccountBalance(0xc0004a0110, 0xcf, 0x218f1850)
    /Users/leeson/gopath/src/trading-system/account/server/repository/mysql/repository.go:62 +0x17a
trading-system/account/server/repository.TestAccountRepository_GetAccountBalance(0xc00018e400)
    /Users/leeson/gopath/src/trading-system/account/server/repository/account_repository_test.go:17 +0x34
testing.tRunner(0xc00018e400, 0x1685a70)
    /usr/local/go/src/testing/testing.go:865 +0xc0
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:916 +0x357
leeson1995 commented 5 years ago

图2里的decimal类型 是图3里的struct tag 里用 text 和什么都不加 都报这个错

leeson1995 commented 5 years ago

@BetaCat0 @lunny pls help~

BetaCat0 commented 5 years ago

@leeson1995 Maybe you should use the exported field as payload, for example:

type Decimal struct {
   DigitsInt int8
   ...
}
lunny commented 5 years ago

XORM is absent of detect MashalText I think we need to do that.