AWS EventBridge PutEvent Detail Malformed(AWS EventBridge PutEvent 详细信息格式错误)

huangapple go评论87阅读模式
英文:

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.

AWS SDK Example

The example for SDK version 1 also generates an error.
Any help would be great

答案1

得分: 2

你提供的代码链接中的代码并不完全正确。实际上,他们在页面底部链接了一个"complete example",其中包含稍微不同的代码:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/cloudwatch/PutEvent/PutEventv2.go#L108-L117

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:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/cloudwatch/PutEvent/PutEventv2.go#L108-L117

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:

https://play.golang.com/p/E4ueZLGIKp4

huangapple
  • 本文由 发表于 2022年3月5日 02:58:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/71356086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定