登录
首页 >  Golang >  Go问答

"为何收到“未加密的复合文字使用”错误信息?"

来源:stackoverflow

时间:2024-02-11 10:45:24 156浏览 收藏

一分耕耘,一分收获!既然打开了这篇文章《"为何收到“未加密的复合文字使用”错误信息?"》,就坚持看下去吧!文中内容包含等等知识点...希望你能在阅读本文后,能真真实实学到知识或者帮你解决心中的疑惑,也欢迎大佬或者新人朋友们多留言评论,多给建议!谢谢!

问题内容

我对 go 比较陌生,正在致力于构建一个请求解码器。该请求采用 json 格式,我们将其解码为 map[string]interface{}。然后,我们将该对象数据传递到我们自己的 processrequest 结构中进行解码。正如我所说,我是新来的,所以我在以前的开发人员编写的代码的类似部分中重用了一些逻辑。本质上,我们正在检查地图中是否有必要的部分,然后设置并返回它们。有人可以向我解释为什么我会收到标题错误吗?我是否必须将项目一直设置为不再有任何嵌套的基本结构?有更好的方法来实现我想要的吗?这是代码和相关结构。它突出显示 model.processrequest 返回时的错误。 tyia

type ProcessRequest struct {
    RequestID string
    Message   *message.Message
    Rule      *Rule
    Options   *ProcessOptions
    //TODO: Context EvaluatorContext
    //TODO: Links
}

type Message struct {
    ID         int
    Key        string
    Source     string
    Headers    *HeaderSet
    Categories *CategorySet
    Properties *PropertySet
    Streams    *StreamSet
}

type RuleAction struct {
    Name       string
    Expression map[string]interface{}
}

type RuleLink struct {
    LinkID       int
    Condition    map[string]interface{}
    TargetRuleID int
}

type Rule struct {
    Name    string
    Code    string
    Actions []RuleAction
    Links   []RuleLink
}
type object = map[string]interface{}

func DecodeProcessRequest(dataObject map[string]interface{}) (*model.ProcessRequest, error) {
    var (
        requestID string
        message   *message.Message
        rule      *model.Rule
        options   *model.ProcessOptions
        err       error
    )

    if reqIDSrc, ok := dataObject["requestId"]; ok {
        if requestID, err = converter.ToString(reqIDSrc); err != nil {
            return nil, errors.Wrapf(err, "Error reading property 'requestID'")
        }
        if requestID == "" {
            return nil, errors.Errorf("Property 'requestID' is an empty string")
        }
    } else {
        return nil, errors.Errorf("Missing required property 'requestID'")
    }

    if messageSrc, ok := dataObject["message"]; ok {
        messageData, ok := messageSrc.(object)
        if !ok {
            return nil, errors.Errorf("Error reading property 'message': Value is not an object")
        }
        if message, err = DecodeMessage(messageData); err != nil {
            return nil, errors.Wrapf(err, "Error reading property 'message'")
        }
    } else {
        return nil, errors.Errorf("Missing required property 'message'")
    }

    if ruleSrc, ok := dataObject["rule"]; ok {
        ruleObj, ok := ruleSrc.(object)
        if !ok {
            return nil, errors.Errorf("Error reading property 'rule': Value is not an object")
        }
        if rule, err = DecodeRule(ruleObj); err != nil {
            return nil, errors.Wrapf(err, "Error reading 'rule' during decoding")
        }
    } else {
        return nil, errors.Errorf("Missing required property 'requestID'")
    }
    // Parse plain object to a Message struct
    return &model.ProcessRequest{
        requestID,
        message,
        rule,
        options,
    }, nil

}

正确答案


superthis comment中说:

一般来说,警告表明您应该更喜欢使用语法 ProcessRequest{ RequestID: requestID, ... }。命名键而不是未键控的值。

这对我有用。另外this commentkostix的解释确实很有帮助。

基本上,这个想法是,如果您使用“无键”方式定义结构文字,则定义的含义取决于基础类型的字段的布局方式。现在考虑您的类型按一定顺序具有三个 string 类型的字段。现在,经过几次迭代,一些程序员将第二个字段移动到第一个位置 - 您的文字仍然会编译,但最终会在运行时定义一个完全不同的值。

今天关于《"为何收到“未加密的复合文字使用”错误信息?"》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>