Skip to content

Commit

Permalink
Use 'itypes' and 'tfjson'.
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit committed Oct 25, 2024
1 parent 13d7d28 commit 0275a24
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/service/wafv2/flex.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
package wafv2

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
Expand All @@ -14,6 +13,8 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/wafv2/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tfjson "github.com/hashicorp/terraform-provider-aws/internal/json"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -984,23 +985,28 @@ func expandHeaderMatchPattern(l []interface{}) *awstypes.HeaderMatchPattern {
}

func expandWebACLRulesJSON(rawRules string) ([]awstypes.Rule, error) {
// Backwards compatibility.
if rawRules == "" {
return nil, errors.New("decoding JSON: unexpected end of JSON input")
}

var temp []any
err := json.Unmarshal([]byte(rawRules), &temp)
err := tfjson.DecodeFromBytes([]byte(rawRules), &temp)
if err != nil {
return nil, fmt.Errorf("decoding JSON: %s", err)
return nil, fmt.Errorf("decoding JSON: %w", err)
}

for _, v := range temp {
walkWebACLJSON(reflect.ValueOf(v))
}

out, err := json.Marshal(temp)
out, err := tfjson.EncodeToBytes(temp)
if err != nil {
return nil, err
}

var rules []awstypes.Rule
err = json.Unmarshal(out, &rules)
err = tfjson.DecodeFromBytes(out, &rules)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1041,7 +1047,7 @@ func walkWebACLJSON(v reflect.Value) {
case reflect.Slice, reflect.Array:
switch reflect.ValueOf(va.outputType).Type().Elem().Kind() {
case reflect.Uint8:
base64String := base64.StdEncoding.EncodeToString([]byte(str.(string)))
base64String := itypes.Base64Encode([]byte(str.(string)))
st[va.key] = base64String
default:
}
Expand Down

0 comments on commit 0275a24

Please sign in to comment.