我正在使用go-pg编写自定义查询缓存系统,该系统采用传递给Query函数的查询参数并生成用于Redis的哈希键。在使用pg.Array作为传递的参数之前,我正在使用Go的反射来检查有效的参数类型。
Reflect为我提供了reflect.Ptr,但是当调用case case块时如何提取指针的struct / Array?
func GenerateQueryCacheKey(args ...interface{}) string {
var argumentString = ""
for _, arg := range args {
v := reflect.ValueOf(arg)
switch v.Kind() {
case reflect.Array, reflect.Slice:
ret := make([]interface{}, v.Len())
for i := 0; i < v.Len(); i++ {
ret[i] = v.Index(i).Interface()
}
GenerateQueryCacheKey(ret...)
case reflect.Bool:
argumentString += strconv.FormatBool(v.Bool())
case reflect.String:
argumentString += v.String()
case reflect.Int:
argumentString += string(v.Int())
case reflect.Uint:
argumentString += string(v.Uint())
case reflect.Float32:
argumentString += strconv.FormatFloat(v.Float(), 'E', -1, 32)
case reflect.Invalid:
log.Printf("Invalid type handle! " + fmt.Sprintf("%T", arg))
argumentString += "nil"
case reflect.Ptr:
p := v.Elem()
ret := make([]interface{}, p.Len())
for i := 0; i < p.Len(); i++ {
ret[i] = p.Index(i).Interface()
}
GenerateQueryCacheKey(ret...)
default:
log.Printf("Unhandled reflect type supplied! " + fmt.Sprintf("%T %T", arg, v))
argumentString += "nil"
}
}
h := md5.New()
io.WriteString(h, argumentString)
return fmt.Sprintf("%x", h.Sum(nil))
}
pg.Array定义:https://sourcegraph.com/github.com/lib/pq/-/blob/array.go#L29:6
编辑:发布的链接的pg.Array定义不正确。我不小心从sourcegraph.com上获取了错误的库。
答案 0 :(得分:0)
通过远离反射并使用更直接的方法来解决。
我只是从pg
导入了types.Array并创建了一个interface {}切片,并手动将字符串添加到interface {}切片中,并使用了散布运算符暂时创建了一个递归函数。不确定这是否是最好的方法,但目前对我有用。
func GenerateQueryCacheKey(args ...interface{}) string {
var argumentString = ""
for _, arg := range args {
switch v := arg.(type) {
case bool:
argumentString += strconv.FormatBool(v)
case string:
argumentString += v
case int:
argumentString += strconv.Itoa(v)
case uint64:
argumentString += string(v)
case float64:
argumentString += strconv.FormatFloat(v, 'E', -1, 32)
case *types.Array:
stringArrayArgs := make([]interface{}, len(v.Value().([]string)))
for i, vs := range v.Value().([]string) {
stringArrayArgs[i] = vs
}
argumentString += GenerateQueryCacheKey(stringArrayArgs...)
case nil:
default:
log.Printf("%T was requested and not handled!\n", v)
argumentString += "nil"
}
}
h := md5.New()
io.WriteString(h, argumentString)
return fmt.Sprintf("%x", h.Sum(nil))
}