Skip to content

Commit

Permalink
Replace NormalizeJsonString with local helper func
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Nam authored and Sharon Nam committed Jun 21, 2023
1 parent 5108afe commit 83aa83e
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion internal/service/events/rule.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package events

import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"time"
Expand Down Expand Up @@ -65,7 +67,7 @@ func ResourceRule() *schema.Resource {
ValidateFunc: validateEventPatternValue(),
AtLeastOneOf: []string{"schedule_expression", "event_pattern"},
StateFunc: func(v interface{}) string {
json, _ := structure.NormalizeJsonString(v.(string))
json, _ := jsonMarshal(v.(string))
return json
},
},
Expand Down Expand Up @@ -109,6 +111,30 @@ func ResourceRule() *schema.Resource {
}
}

func jsonMarshal(jsonString interface{}) (string, error) {
var j interface{}

if jsonString == nil || jsonString.(string) == "" {
return "", nil
}

s := jsonString.(string)

err := json.Unmarshal([]byte(s), &j)
if err != nil {
return s, err
}

b, _ := json.Marshal(j)

if bytes.Contains(b, []byte("\\u003c")) || bytes.Contains(b, []byte("\\u003e")) || bytes.Contains(b, []byte("\\u0026")) {
b = bytes.Replace(b, []byte("\\u003c"), []byte("<"), -1)
b = bytes.Replace(b, []byte("\\u003e"), []byte(">"), -1)
b = bytes.Replace(b, []byte("\\u0026"), []byte("&"), -1)
}
return string(b[:]), nil
}

func resourceRuleCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).EventsConn(ctx)
Expand Down

0 comments on commit 83aa83e

Please sign in to comment.