我正在寻找一种存储信息的方法,这是一个函数应该使用的结构。每个结构对应于特定的数据库表。
type Record struct {
TableName string
PrimaryKey string
//XormStruct // how can I store User or Post here?
XormStruct2 interface{} // see I have tried below
XormStruct3 reflect.Type // see I have tried below
}
var posts []Post
var ListOfTables [...]Record {
{"User", "id", User},
//{"Post", "post_id", Post},
{"Post", "post_id", posts, reflect.TypeOf([]Post{})},
}
// User is xorm struct
type User struct {
Id int64
Name string
}
// Post is xorm struct
type Post struct {
Post_id int64
Name string
Other string
}我希望能够动态地为表选择struct。
for _, rec := range ListOfTables {
//var entries []rec.XormStruct // this does not work, of course
//var entries []reflect.TypeOf(rec.XormStruct) // this does not work either
// xorm is *xorm.Engine
xorm.Find(&entries)
//xorm.Find(&rec.XormStruct2) //with interface{}, does not work - produces an empty &[]
posts3 := reflect.New(rec.XormStruct3).Interface()
//xorm.Find(&posts3) // same as above, produces empty &[]
var posts []Post
xorm.Find(&posts) // produces desired data
// afterwards I will do same to any table entries, e.g.
xml.Marshal(entries)
// so there is really no need to create identical functions for each table
}目标干燥(我有大约30张桌子,功能是一样的)
我试过:
reflect.TypeOf(),但我不理解是否/如何使用它(reflect.Type)来定义一个新变量XormStruct interface{}定义记录,并为每个ListOfTables条目创建一个切片,例如var posts []Post和{"Post", "post_id", posts},在我看来,xorm.Find()对于获得一个interface{}而不是[]Posts并不感到“高兴”,即使它没有这么说。
更新:我认为最大的区别是:
spew.Dump(posts3) //posts3 := reflect.New(rec.XormStruct3).Interface()
// (*[]main.Post)<0x2312312>(<nil>)
spew.Dump(posts) //var posts []Post
// ([]main.Post)<nil>溶液
posts3 := reflect.New(rec.XormStruct3).Interface()
xorm.Find(posts3) // not &posts3发布于 2018-10-24 09:08:16
您可以使用reflect.Type来表示/描述Go类型。在运行时,您可以使用reflect.New()获得一个指针,指向包装在reflect.Value中的该类型的零值。如果您需要一个切片而不是单个值,您可以使用reflect.SliceOf(),或者首先获得切片值的类型描述符。
如果存储表的refect.Type值,则可以使用以下方法:
type Record struct {
TableName string
PrimaryKey string
XormStruct reflect.Type
}
var ListOfTables [...]Record {
{"User", "id", reflect.TypeOf((*User)(nil)).Elem()},
{"Post", "post_id", reflect.TypeOf((*Post)(nil)).Elem()},
}
// User is xorm struct
type User struct {
Id int64
Name string
}
// Post is xorm struct
type Post struct {
Post_id int64
Name string
Other string
}注意,您必须使用导出的字段!
然后处理这些桌子:
for _, rec := range ListOfTables {
entries := reflect.New(reflect.SliceOf(t.XormStruct)).Interface()
err := xorm.Find(entries)
// Handle error
err := xml.Marshal(entries)
// Handle error
}您可以看到一个使用JSON:围棋游乐场的工作示例(概念的证明)(没有使用围棋游乐场,因为Go游乐场上无法使用该示例)。
如果首先存储片的reflect.Type值:
var ListOfTables [...]Record {
{"User", "id", reflect.TypeOf([]User{})},
{"Post", "post_id", reflect.TypeOf([]Post{})},
}而且使用它也更简单:
for _, rec := range ListOfTables {
entries := reflect.New(t.XormStruct).Interface()
err := xorm.Find(entries)
// Handle error
err := xml.Marshal(entries)
// Handle error
}参见这一概念的证明:围棋游乐场。
注意,如果Record持有片类型(在XormStruct字段中),如果您需要访问结构的类型(结构的元素类型),则可以使用Type.Elem()。
https://stackoverflow.com/questions/52963237
复制相似问题