Is it possible to Find with reflected slice? #1362

Closed
opened 2019-07-22 18:52:27 +00:00 by TheCGDF · 4 comments
TheCGDF commented 2019-07-22 18:52:27 +00:00 (Migrated from github.com)
func FindT(typ interface{}) {
	items := reflect.Zero(reflect.SliceOf(reflect.TypeOf(typ))).Interface()
	db.Find(&items)    //panic: needs a pointer to a slice or a map
}

Is it possible to Find with reflected slice?

``` func FindT(typ interface{}) { items := reflect.Zero(reflect.SliceOf(reflect.TypeOf(typ))).Interface() db.Find(&items) //panic: needs a pointer to a slice or a map } ``` Is it possible to `Find` with reflected slice?

Why not just a slice?

Why not just a slice?
TheCGDF commented 2019-07-23 15:43:09 +00:00 (Migrated from github.com)

In fact, this is my current workaround:

func FindT(typ interface{}, typs interface{}) {
	dbTotal := *db
	total, _ := dbTotal.Count(typ)
	//...
	db.Find(typs)
}

FindT(MyType{},&[]MyType)

So, I just want to know is it possbile to omit the second param typs interface{} with reflect.

In fact, this is my current workaround: ``` func FindT(typ interface{}, typs interface{}) { dbTotal := *db total, _ := dbTotal.Count(typ) //... db.Find(typs) } FindT(MyType{},&[]MyType) ``` So, I just want to know is it possbile to omit the second param `typs interface{}` with reflect.

I think you could use FindAndCount function directly. Sorry, this function have not documented yet.

I think you could use `FindAndCount` function directly. Sorry, this function have not documented yet.
TheCGDF commented 2019-07-24 20:04:35 +00:00 (Migrated from github.com)

I'm sorry for my vague words.
The total is the count of the entire table, not the count of the results.

func FindT(typ interface{}, typs interface{}) {
	total, _ := db1.Count(typ)
        db2.Where(...).Find(typs)
}

FindT(MyType{},&[]MyType{})

So FindAndCount cannot help me.
I have worked out a solution:

func FindT(typ interface{}){
	myTypeSlice := reflect.Zero(reflect.SliceOf(reflect.TypeOf(typ)))
	reflectSlice :=reflect.New(myTypeSlice.Type())
	reflectSlice.Elem().Set(myTypeSlice)
	sliceInterface := reflectSlice.Interface()
	db.Find(sliceInterface)
}

FindT(MyType{})

Thanks for your help.

I'm sorry for my vague words. The `total` is the count of the entire table, not the count of the results. ``` func FindT(typ interface{}, typs interface{}) { total, _ := db1.Count(typ) db2.Where(...).Find(typs) } FindT(MyType{},&[]MyType{}) ``` So `FindAndCount` cannot help me. I have worked out a solution: ``` func FindT(typ interface{}){ myTypeSlice := reflect.Zero(reflect.SliceOf(reflect.TypeOf(typ))) reflectSlice :=reflect.New(myTypeSlice.Type()) reflectSlice.Elem().Set(myTypeSlice) sliceInterface := reflectSlice.Interface() db.Find(sliceInterface) } FindT(MyType{}) ``` Thanks for your help.
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1362
No description provided.