Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

runtime error: hash of unhashable type map[string]interface {} #110

Closed
mateors opened this issue Apr 14, 2022 · 9 comments
Closed

runtime error: hash of unhashable type map[string]interface {} #110

mateors opened this issue Apr 14, 2022 · 9 comments

Comments

@mateors
Copy link

mateors commented Apr 14, 2022

Unable to perform group by query on the following dataset

rows := []map[string]interface{}{
	{"name": "Mostain", "salary": 30000, "ShipmentTag": "b001"},
	{"name": "Sanzida", "salary": 20000, "ShipmentTag": "b001"},
	{"name": "Riaz", "salary": 21000, "ShipmentTag": "b002"},
	{"name": "Pallobi", "salary": 30000, "ShipmentTag": "b001"},
	{"name": "Moaz", "salary": 20000, "ShipmentTag": "b001"},
	{"name": "Tareq", "salary": 21000, "ShipmentTag": "b002"},
}
	data := From(rows).Where(func(c interface{}) bool {
		cmap := c.(map[string]interface{})
		return len(cmap["name"].(string)) > 0

	}).GroupBy(func(ShipmentTag interface{}) interface{} {
		return ShipmentTag

	}, func(ShipmentTag interface{}) interface{} {
		return ShipmentTag

	}).First()

	fmt.Println(data)

Error details:

goroutine 1 [running]:
github.com/ahmetb/go-linq/v3.Query.GroupBy.func1()
E:/GOLANG/pkg/mod/github.com/ahmetb/go-linq/v3@v3.2.0/groupby.go:21 +0x15f
github.com/ahmetb/go-linq/v3.Query.First({0x5f0aa0?})
E:/GOLANG/pkg/mod/github.com/ahmetb/go-linq/v3@v3.2.0/result.go:188 +0x1a
main.main()

@kalaninja
Copy link
Collaborator

Hi @mateors. You are probably doing something wrong. You do a GroupBy over a list of maps. So in your keySelector function ShipmentTag is a map - not a "ShipmentTag" key.
The thing that you probably want to do is:

	data := From(rows).Where(func(c interface{}) bool {
		cmap := c.(map[string]interface{})
		return len(cmap["name"].(string)) > 0

	}).GroupBy(func(c interface{}) interface{} {
		cmap := c.(map[string]interface{})
		return cmap["ShipmentTag"]

	}, func(ShipmentTag interface{}) interface{} {
		return ShipmentTag

	}).First()

@mateors
Copy link
Author

mateors commented Apr 18, 2022

@kalaninja Thank you very much, It works, But I am wondering How can I use ToMap() instead of First()
Could you please show me an example?

@kalaninja
Copy link
Collaborator

@mateors

result := make(map[string]interface{})

From(rows).WhereT(func(x map[string]interface{}) bool {
	return len(x["name"].(string)) > 0
}).GroupByT(
	func(x map[string]interface{}) interface{} {
		return x["ShipmentTag"]
	},
	func(x interface{}) interface{} {
		return x
	},
).ToMapBy(&result,
	func(i interface{}) interface{} {
		return i.(Group).Key
	}, func(i interface{}) interface{} {
		return i.(Group).Group
	})

@mateors
Copy link
Author

mateors commented Apr 18, 2022

@kalaninja Thank you very much, you are superb! I noticed you used GroupByT but I want to use GroupBy only, no generics. I am concern about performance.

	result := make(map[string]interface{})

	data := From(rows).Where(func(c interface{}) bool {
		cmap := c.(map[string]interface{})
		return len(cmap["name"].(string)) > 0

	}).GroupBy(func(c interface{}) interface{} {
		cmap := c.(map[string]interface{})
		return cmap["ShipmentTag"]

	}, func(ShipmentTag interface{}) interface{} {
		return ShipmentTag

	}).ToMapBy(&result,
		func(i interface{}) interface{} {
			return i.(Group).Key
		}, func(i interface{}) interface{} {
			return i.(Group).Group
		})

above code not working.
It says (no value) used as value

@kalaninja
Copy link
Collaborator

that is probably because you are trying to read the resulting data from data instead of result?
ToMapBy doesn't return a value, it populates a map you provide instead.

@mateors
Copy link
Author

mateors commented Apr 18, 2022

@kalaninja No its compiling error, code seems okay but I don't know why not showing error. Could you please compile at your end? Advance thanks for your kind quick response.

@kalaninja
Copy link
Collaborator

@mateors I think the reason might be because ToMapBy doesn't return any value. Can you try removing data := from your code please?

@mateors
Copy link
Author

mateors commented Apr 18, 2022

@kalaninja Thank you, following code working now. Jajakallah. You make my day. I am fasting as we are celebrating Ramadan, I pray for your success.

	result := make(map[string]interface{})

	From(rows).Where(func(c interface{}) bool {
		cmap := c.(map[string]interface{})
		return len(cmap["name"].(string)) > 0

	}).GroupBy(func(c interface{}) interface{} {
		cmap := c.(map[string]interface{})
		return cmap["ShipmentTag"]

	}, func(ShipmentTag interface{}) interface{} {
		return ShipmentTag

	}).ToMapBy(&result,
		func(i interface{}) interface{} {
			return i.(Group).Key
		}, func(i interface{}) interface{} {
			return i.(Group).Group
		})

	fmt.Println(result)

@kalaninja
Copy link
Collaborator

I was glad to help @mateors. Welcome. And happy celebration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants