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

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

AWS EventBridge PutEvent Detail Malformed

问题

我已经尝试了AWS Go SDK的两个版本,每次都会出现错误,指出Details字段格式不正确。Details字段接受一个JSON字符串。

在SDK V2中,事件的结构体如下:

  1. type Event struct {
  2. Details []struct {
  3. Key string `json:"Key"`
  4. Value string `json:"Value"`
  5. } `json:"Details"`
  6. DetailType string `json:"DetailType"`
  7. Source string `json:"Source"`
  8. }

然后,示例代码使用以下代码构建JSON字符串:

  1. myDetails := "{ "
  2. for _, d := range event.Details {
  3. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
  4. }
  5. myDetails = myDetails + " }"

然后进行API调用:

  1. input := &cloudwatchevents.PutEventsInput{
  2. Entries: []types.PutEventsRequestEntry{
  3. {
  4. Detail: &myDetails,
  5. DetailType: &event.DetailType,
  6. Resources: []string{
  7. *lambdaARN,
  8. },
  9. Source: &event.Source,
  10. },
  11. },
  12. }

基本上,我会收到一个错误,指出分配给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

  1. type Event struct {
  2. Details []struct {
  3. Key string `json:"Key"`
  4. Value string `json:"Value"`
  5. } `json:"Details"`
  6. DetailType string `json:"DetailType"`
  7. Source string `json:"Source"`

}

The example then builds up the JSON string with this code

  1. myDetails := "{ "
  2. for _, d := range event.Details {
  3. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
  4. }
  5. myDetails = myDetails + " }"

And then make the api call

  1. input := &cloudwatchevents.PutEventsInput{
  2. Entries: []types.PutEventsRequestEntry{
  3. {
  4. Detail: &myDetails,
  5. DetailType: &event.DetailType,
  6. Resources: []string{
  7. *lambdaARN,
  8. },
  9. Source: &event.Source,
  10. },
  11. },
  12. }

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

  1. myDetails := "{ "
  2. for i, d := range event.Details {
  3. if i == (len(event.Details) - 1) {
  4. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\""
  5. } else {
  6. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
  7. }
  8. }
  9. myDetails = myDetails + " }"

但是,像这样构建 JSON 并不理想,容易出错,正如你已经发现的那样。

我建议使用以下代码:

  1. details := make(map[string]string, len(event.Details))
  2. for _, d := range event.Details {
  3. details[d.Key] = d.Value
  4. }
  5. b, err := json.Marshal(details)
  6. if err != nil {
  7. return
  8. }
  9. 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

  1. myDetails := "{ "
  2. for i, d := range event.Details {
  3. if i == (len(event.Details) - 1) {
  4. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\""
  5. } else {
  6. myDetails = myDetails + "\"" + d.Key + "\": \"" + d.Value + "\","
  7. }
  8. }
  9. myDetails = myDetails + " }"

But building the JSON like this is not ideal and error-prone, as you already found out.

I'd propose the following:

  1. details := make(map[string]string, len(event.Details))
  2. for _, d := range event.Details {
  3. details[d.Key] = d.Value
  4. }
  5. b, err := json.Marshal(details)
  6. if err != nil {
  7. return
  8. }
  9. 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:

确定