array := AnyType[]{...}
groups = group(array, "SomeField")
// groups should be map of arrays of AnyType grouped by SomeField values
I threw a few hours at this and it seems I it seems I'll need to spend a few days to fully understand how to solve this.
So far I've come up with:
import (
"test"
"fw"
"reflect"
"fmt"
)
func group_by (array []interface{}, key string) {
grouped := map[interface{}][]interface{}{}
for _, item := range array {
v := reflect.ValueOf(&item).Elem().FieldByName(key)
grouped[v] = append(grouped[v], item)
}
fmt.Println(grouped)
}
func tests(c *fw.MyContext) {
It := test.It
//Assert := test.Assert
Describe := test.Describe
type Venue struct {
City string
}
p1 := Venue{"Ostrava"}
p2 := Venue{"Oslo"}
//venues := []Venue{p1, p2} // I wish I could just use []Venue, but i didn't know how to do it..
venues := []interface{}{p1, p2}
group_by(venues, "City")
runner := test.NewRunner()
runner.AddSuite(suite)
test.ReportHtml(runner, c)
}
This compiles and I get an error:
2012/01/21 18:47:27 http: panic serving @: reflect: call of reflect.Value.FieldByName on interface Value
/tmp/appengine/google_appengine/goroot/src/pkg/http/server.go:588 (0x8083af1)
/tmp/appengine/google_appengine/goroot/src/pkg/runtime/proc.c:1235 (0x80556da)
/tmp/appengine/google_appengine/goroot/src/pkg/reflect/value.go:354 (0x80d77af)
/tmp/appengine/google_appengine/goroot/src/pkg/reflect/value.go:746 (0x80d9ac7)
app/utils.go:14 (0x805ea24)
group_by: v := reflect.ValueOf(&item).Elem().FieldByName(key)
app/utils.go:40 (0x8061a2c)
_func_002: group_by(venues, "City")
test/spec.go:62 (0x80d2ab5)
(*Spec).Run: spec.Body()
test/testsuite.go:25 (0x80d2fd6)
(*Suite).Run: suiteResult.AddSpecResult(spec.Run())
test/runner.go:21 (0x80d2c50)
(*Runner).Run: result.AddSuiteResult(suite.Run())
test/suite.go:19 (0x80d2d39)
ReportHtml: result := runner.Run()
app/utils.go:47 (0x805ed29)
tests: test.ReportHtml(runner, c)
fw/context.go:321 (0x80b79fd)
Route: page(context)
fw/context.go:258 (0x80b8327)
_func_007: Route(context, router, e404)
app/router.go:28 (0x805f9aa)
route: }, e404)
app/router.go:16 (0x805f852)
handler: route(fw.NewMyContext(w, r))
If you could help with some of the thing I would very much appreciate it.
Also if there is any library I could use of this, it would let me go on programming until I grasp the concepts.