func main() { c, err := redis.Dial("tcp", ":6379") if err != nil { log.Fatal(err) } v0 := &MyStruct{1, "hello"} _, err = c.Do("HMSET", redisx.AppendStruct([]interface{}{"key"}, v0)...) if err != nil { log.Fatal(err) } reply, err := c.Do("HGETALL", "key") if err != nil { log.Fatal(err) } v1 := &MyStruct{} err = redisx.ScanStruct(reply, v1) if err != nil { log.Fatal(err) } log.Printf("v1=%v", v1) }
func ReadIndexTypeForSQL(message string) (sql string, t string, appdbserver string, dbtype int, isfiel int, filepath string, filekey string, primarykey string, optype int, appindex string) { err := json.Unmarshal([]byte(message), &mqd) if err != nil { fmt.Println(err) } pid := mqd.Primarykey optype = mqd.OperationType applogo := mqd.Apploggo table := mqd.TableName appkey := "app:" + applogo key := "app:" + applogo + ":Index:" + table c := InitRedis() //连接App表,获取数据库服务器地址以及数据库类型 r, e := c.Do("HGETALL", appkey) if e != nil { log.Fatal(e) } err = redisx.ScanStruct(r, &app) //获取IndexMap表字段 reply, err := c.Do("HGETALL", key) if err != nil { log.Fatal(err) } err = redisx.ScanStruct(reply, &src) var ( strsql string ) //判读表的全部索引 strsql = "select " + src.IndexAttribute + " from " + src.IndexTable if pid == 0 { if src.IndexWhere != "" { strsql = strsql + " where " + src.IndexWhere } } else { strsql = strsql + " where " + src.IndexPrimarykey + "=" + strconv.Itoa(pid) if src.IndexWhere != "" { strsql = strsql + " & " + src.IndexWhere } } return strsql, table, app.Appdbserver, app.Appdbdevice, src.IndexIsFile, src.IndexFilePath, src.IndexFileKey, src.IndexPrimarykey, optype, applogo }
//根据key获取相对应的value func HGETALL(key string, src interface{}) { c := InitRedis() reply, err := c.Do("HGETALL", key) if err != nil { log.Fatal(err) } err = redisx.ScanStruct(reply, src) if err != nil { log.Fatal(err) } }
func TestScanStruct(t *testing.T) { for _, tt := range scanStructTests { var reply []interface{} for _, v := range tt.reply { reply = append(reply, []byte(v)) } value := reflect.New(reflect.ValueOf(tt.value).Type().Elem()) if err := redisx.ScanStruct(reply, value.Interface()); err != nil { t.Fatalf("ScanStruct(%s) returned error %v", tt.title, err) } if !reflect.DeepEqual(value.Interface(), tt.value) { t.Fatalf("ScanStruct(%s) returned %v, want %v", tt.title, value.Interface(), tt.value) } } }