Exemple #1
0
func ExampleScanSlice() {
	c, err := dial()
	if err != nil {
		panic(err)
	}
	defer c.Close()

	c.Send("HMSET", "album:1", "title", "Red", "rating", 5)
	c.Send("HMSET", "album:2", "title", "Earthbound", "rating", 1)
	c.Send("HMSET", "album:3", "title", "Beat", "rating", 4)
	c.Send("LPUSH", "albums", "1")
	c.Send("LPUSH", "albums", "2")
	c.Send("LPUSH", "albums", "3")
	values, err := redis.Values(c.Do("SORT", "albums",
		"BY", "album:*->rating",
		"GET", "album:*->title",
		"GET", "album:*->rating"))
	if err != nil {
		panic(err)
	}

	var albums []struct {
		Title  string
		Rating int
	}
	if err := redis.ScanSlice(values, &albums); err != nil {
		panic(err)
	}
	fmt.Printf("%v\n", albums)
	// Output:
	// [{Earthbound 1} {Beat 4} {Red 5}]
}
Exemple #2
0
func TestScanSlice(t *testing.T) {
	for _, tt := range scanSliceTests {

		typ := reflect.ValueOf(tt.dest).Type()
		dest := reflect.New(typ)

		err := redis.ScanSlice(tt.src, dest.Interface(), tt.fieldNames...)
		if tt.ok != (err == nil) {
			t.Errorf("ScanSlice(%v, []%s, %v) returned error %v", tt.src, typ, tt.fieldNames, err)
			continue
		}
		if tt.ok && !reflect.DeepEqual(dest.Elem().Interface(), tt.dest) {
			t.Errorf("ScanSlice(src, []%s) returned %#v, want %#v", typ, dest.Elem().Interface(), tt.dest)
		}
	}
}
Exemple #3
0
func (a *analyser) GetTaxonomyDatasets(request *restful.Request, response *restful.Response) {

	taxonomy := request.PathParameter("which")
	subset := request.PathParameter("subset")

	var reply []interface{}
	var err error

	rcon := a.pool.Get()
	defer rcon.Close()

	type internalDataset struct {
		ID, CKANID  string
		Publisher   string
		Contact     string
		Description string
		Version     string
		Category    string
		GeoBBox     string
		GeoToponym  string
	}
	var internalsets []internalDataset
	reply, err = redis.Values(rcon.Do("SORT", datasetskey+":"+taxonomy+":"+subset,
		"BY", "nosort",
		"GET", datasetkey+":*->ID",
		"GET", datasetkey+":*->CKANID",
		"GET", datasetkey+":*->Publisher",
		"GET", datasetkey+":*->Contact",
		"GET", datasetkey+":*->Description",
		"GET", datasetkey+":*->Version",
		"GET", datasetkey+":*->Category",
		"GET", datasetkey+":*->GeoBBox",
		"GET", datasetkey+":*->GeoToponym"))
	if err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}
	if err = redis.ScanSlice(reply, &internalsets); err != nil {
		response.WriteError(http.StatusInternalServerError, err)
		return
	}

	var responseset []Dataset
	for _, is := range internalsets {
		ds := Dataset{ID: is.ID,
			CKANID:      is.CKANID,
			Publisher:   is.Publisher,
			Contact:     is.Contact,
			Description: is.Description,
			Version:     is.Version,
			GeoBBox:     is.GeoBBox,
			GeoToponym:  is.GeoToponym}

		var strcats []string
		if len(is.Category) > 0 {
			if err := json.Unmarshal([]byte(is.Category), &strcats); err != nil {
				response.WriteError(http.StatusInternalServerError, err)
				return
			}
		}
		ds.Category = strcats
		responseset = append(responseset, ds)
	}
	response.WriteEntity(responseset)
}