英文:
AWS EventBridge PutEvent Detail Malformed
问题
我已经尝试了AWS Go SDK的两个版本,每次都会出现错误,指出Details字段格式不正确。Details字段接受一个JSON字符串。
在SDK V2中,事件的结构体如下:
type Event struct {
Details []struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"Details"`
DetailType string `json:"DetailType"`
Source string `json:"Source"`
}
然后,示例代码使用以下代码构建JSON字符串:
myDetails := "{ "
for _, d := range event.Details {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
}
myDetails = myDetails + " }"
然后进行API调用:
input := &cloudwatchevents.PutEventsInput{
Entries: []types.PutEventsRequestEntry{
{
Detail: &myDetails,
DetailType: &event.DetailType,
Resources: []string{
*lambdaARN,
},
Source: &event.Source,
},
},
}
基本上,我会收到一个错误,指出分配给Detail字段的字符串格式不正确。我认为这是因为示例代码生成了一个带有尾随逗号的字符串,这是无效的。然而,如果省略逗号,就会得到一个空指针引用。
SDK版本1的示例也会生成错误。任何帮助都将是很好的。
英文:
I have tried both AWS Go SDKs both versions and every time I get an error stating that the Details field is malformed. The Details field accepts a JSON string.
In SDK V2 you basically have a struct for the event
type Event struct {
Details []struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"Details"`
DetailType string `json:"DetailType"`
Source string `json:"Source"`
}
The example then builds up the JSON string with this code
myDetails := "{ "
for _, d := range event.Details {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
}
myDetails = myDetails + " }"
And then make the api call
input := &cloudwatchevents.PutEventsInput{
Entries: []types.PutEventsRequestEntry{
{
Detail: &myDetails,
DetailType: &event.DetailType,
Resources: []string{
*lambdaARN,
},
Source: &event.Source,
},
},
}
Basically I get an error saying that the string assigned to the Detail field is malformed. I believe this is because the example code generates a string with a trailing which is not valid . However when you omit the , you get a nil memory reference.
The example for SDK version 1 also generates an error.
Any help would be great
答案1
得分: 2
你提供的代码链接中的代码并不完全正确。实际上,他们在页面底部链接了一个"complete example",其中包含稍微不同的代码:
myDetails := "{ "
for i, d := range event.Details {
if i == (len(event.Details) - 1) {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\""
} else {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
}
}
myDetails = myDetails + " }"
但是,像这样构建 JSON 并不理想,容易出错,正如你已经发现的那样。
我建议使用以下代码:
details := make(map[string]string, len(event.Details))
for _, d := range event.Details {
details[d.Key] = d.Value
}
b, err := json.Marshal(details)
if err != nil {
return
}
fmt.Println(string(b))
可以在 playground 中查看它的运行效果:
https://play.golang.com/p/E4ueZLGIKp4
英文:
The code on the page you linked is not 100% correct. They actually link the "complete example" at the bottom of the page, which has slightly different code:
myDetails := "{ "
for i, d := range event.Details {
if i == (len(event.Details) - 1) {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\""
} else {
myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
}
}
myDetails = myDetails + " }"
But building the JSON like this is not ideal and error-prone, as you already found out.
I'd propose the following:
details := make(map[string]string, len(event.Details))
for _, d := range event.Details {
details[d.Key] = d.Value
}
b, err := json.Marshal(details)
if err != nil {
return
}
fmt.Println(string(b))
Checkout the playground to see it in action:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论