我是 Go 的新手,正在构建一个请求解码器。请求采用 JSON 格式,我们将其解码为 map[string]interface{}。然后我们将该对象数据传递给我们自己的 ProcessRequest 结构进行解码。正如我所说,我是新手,所以我在以前开发人员编写的代码的类似部分中重复使用了一些逻辑。本质上,我们正在检查地图中是否有必要的部分,然后设置并返回它们。有人可以向我解释为什么我会收到标题错误吗?我是否必须将项目一直设置为不再有任何嵌套的基本结构?有没有更好的方法来完成我想要的?这是代码和相关结构。它突出显示了 model.ProcessRequest 返回时的错误。蒂亚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'") }
1 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
- 1 回答
- 0 关注
- 59 浏览
添加回答
举报
0/150
提交
取消