如何使用reflect.Type来执行类型断言 [英] How to use a reflect.Type to perform a type assertion

查看:63
本文介绍了如何使用reflect.Type来执行类型断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在 Go 中反射通常不受欢迎,但就我目前的目的而言,我很确定这是最好的解决方案.

I know reflection is generally frowned upon in go but for my current purposes Im pretty sure it is the best solution.

本质上我的项目是cli工具,它将根据传入的命令输出一个xml查询并返回相应的结果.

Essentially my project is cli tool which will output an xml query based on incoming commands and return the corresponding result.

每个命令请求都有一些样板代码,其中填充了默认值并验证了提供的值.

There is some boiler plate code for each command request where default values are populated and supplied values validated.

所以我有一系列基于 Command 结构的 Command 对象,如下所示:

So I have a series of Command objects based on a Command struct as follows:

type Command struct {
    Name        string
    Request     interface{}
    RequestType reflect.Type
    Response    interface{}

    RegisterFunc func(parentCmd *cobra.Command, cmd *Command) error
}

在大多数情况下,我真的不关心请求/响应的类型(只是 xml 编码/解码).但是我需要将它简要地转换为具体类型以使用结构注释进行验证例如,我可能会做这样的事情:

For the most part I really don't care about the types of the request/response (just xml encode/decode). However I need to briefly cast it to a concrete type to validate with struct annotations So for example I might do something like this:

    var ccReq *CreateCredentialRequest
    var set bool
    if ccReq, set = command.Request.(*CreateCredentialRequest); !set {
        log.Fatal(errors.New("invalid request type"))
    }

    result, err := govalidator.ValidateStruct(ccReq)
    if err != nil {
        println("error: " + err.Error())
        os.Exit(1)
    }

但是,在理想情况下,我希望对所有命令进行一般处理,如下所示:

However in an ideal would I would like to handle this generically for all commands, something like this:

    var ccReq *CreateCredentialRequest
    var set bool
    if ccReq, set = command.Request.(command.RequestType); !set {
        log.Fatal(errors.New("invalid request type"))
    }

但是这会导致错误:

command.RequestType 不是类型

command.RequestType is not a type

那么,如何为以后的类型断言存储类型的值

So, how can I store the value of a type for a later type assertion

注意:在与 JimB 的评论中讨论之后,似乎我实际上并不需要用于验证目的的具体类型(接口很好),但我仍然需要进一步断言响应类型以提供自定义响应处理程序

Note: Following discussion in comments with JimB it seems that i do not actually need the concrete type for validation purposes (interface is fine) but I still need further down to assert the response type to provide a custom response handler

推荐答案

来自 reflect.输入文档:

类型值具有可比性,例如使用 == 运算符,因此它们可以用作映射键.如果两个 Type 值表示相同的类型,则它们相等.

Type values are comparable, such as with the == operator, so they can be used as map keys. Two Type values are equal if they represent identical types.

如果你想比较底层类型,直接比较 reflect.Type 值:

If you want to compare underlying types, compare the reflect.Type values directly:

if reflect.TypeOf(command.Request) != command.RequestType {
    log.Fatal(errors.New("invalid request type"))
}

这篇关于如何使用reflect.Type来执行类型断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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