在结构的字段上实现Redigo Scanner接口 [英] Implementing Redigo Scanner interface on a struct's field

查看:840
本文介绍了在结构的字段上实现Redigo Scanner接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  type authEnum int 

const(
永不authEnum = iota
有时
总是


类型Attrs结构{
秘密字符串`redis:secret`
RequireSecret authEnum`redis:requireSecret`
UserID字符串`redis:userId`
}

func(e * authEnum)RedisScan(src interface {})错误{
//这永远不会被调用!
if e == nil {
return fmt.Errorf(nil pointer)
}
switch src:= src。(type){
case string:
如果src ==false|| src ==never{
* e = never
} else if src ==sometimes{
* e = sometimes
} else {//alwaysor true
* e = always
}
默认值:
返回fmt.Errorf(无法将authEnum从%T转换为%T,src,e)

返回nil
}

func getAttributes(ctx * AppContext,hash string)(* Attrs,error){
rc:= ctx.RedisPool.Get ()
values,err:= redis.Values(rc.Do(HGETALL,redishash))
rc.Close()
if err!= nil {
return nil,err
}
attrs:= Attrs {}
redis.ScanStruct(values,& attrs)
return& attrs,nil
}

如何实施 RequireSecret 属性来分析 authEnum 属性中的扫描器接口上的garyburd / redigo / redis#Scanner rel =nofollow noreferrer code>输出never有时always redis哈希值?



如何计算值并将其分配给authEnum?
在我的代码示例中, RedisScan 永远不会被调用。

解决方案

在指针接收器上实现该方法。 Redis批量字符串表示为[]字节,而不是字符串:

  func(e * authEnum)RedisScan(src interface {})错误{
b,ok:= src。([] byte)
if!ok {
return fmt.Errorf(无法将authEnum从%T转换为%T,src,b)
}
开关字符串(b){
casefalse,never:
* e =从不
case有时:
* e =有时
默认值:
* e =永远为
}
返回零
}

总是检查并处理错误。从 ScanStruct 返回的错误报告类型问题。



不需要检查指向结构成员的nil指针。如果ScanStruct的参数为零,则Redigo将在调用RedisScan方法之前发生恐慌。


I have a struct that looks like this:

type authEnum int

const (
    never authEnum = iota
    sometimes
    always
)

type Attrs struct {
    Secret         string   `redis:"secret"`
    RequireSecret  authEnum `redis:"requireSecret"`
    UserID         string   `redis:"userId"`
}

func (e *authEnum) RedisScan(src interface{}) error {
    // This never gets called!
    if e == nil {
        return fmt.Errorf("nil pointer")
    }
    switch src := src.(type) {
    case string:
        if src == "false" || src == "never" {
            *e = never
        } else if src == "sometimes" {
            *e = sometimes
        } else { // "always" or "true"
            *e = always
        }
    default:
        return fmt.Errorf("cannot convert authEnum from %T to %T", src, e)
    }
    return nil
}

func getAttributes(ctx *AppContext, hash string) (*Attrs, error) {
    rc := ctx.RedisPool.Get()
    values, err := redis.Values(rc.Do("HGETALL", "redishash"))
    rc.Close()
    if err != nil {
        return nil, err
    }
    attrs := Attrs{}
    redis.ScanStruct(values, &attrs)
    return &attrs, nil
}

How do I implement the Scanner interface on the RequireSecret attribute to parse an authEnum type out of "never", "sometimes" or "always" redis hash values?

How do I calculate the value and assign it to the authEnum? In my code example RedisScan never gets called.

解决方案

Implement the method on a pointer receiver. Redis bulk strings are represented as []byte, not string:

func (e *authEnum) RedisScan(src interface{}) error {
    b, ok := src.([]byte)
    if !ok {
        return fmt.Errorf("cannot convert authEnum from %T to %T", src, b)
    }
    switch string(b) {
    case "false", "never":
        *e = never
    case "sometimes":
        *e = sometimes
    default:
        *e = always
    }
    return nil
}

Always check and handle errors. The error returned from ScanStruct reports the type problem.

There's no need to check for nil pointer to the struct member. If the argument to ScanStruct is nil, then Redigo will panic well before the RedisScan method is called.

这篇关于在结构的字段上实现Redigo Scanner接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆