-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38016 from mattburgess/ecs-awssdkv2-migration
ecs: Migrate to AWS SDK v2
- Loading branch information
Showing
68 changed files
with
2,936 additions
and
2,908 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package jsoncmp | ||
|
||
// Inspired by https://github.com/hgsgtk/jsoncmp. | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func Diff(x, y string) string { | ||
xform := cmp.Transformer("jsoncmp", func(s string) (m map[string]any) { | ||
if err := json.Unmarshal([]byte(s), &m); err != nil { | ||
panic(fmt.Sprintf("json.Unmarshal(%s): %s", s, err)) | ||
} | ||
return m | ||
}) | ||
opt := cmp.FilterPath(func(p cmp.Path) bool { | ||
for _, ps := range p { | ||
if tr, ok := ps.(cmp.Transform); ok && tr.Option() == xform { | ||
return false | ||
} | ||
} | ||
return true | ||
}, xform) | ||
|
||
return cmp.Diff(x, y, opt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package jsoncmp_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-aws/internal/acctest/jsoncmp" | ||
) | ||
|
||
func TestDiff(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
testName string | ||
x, y string | ||
wantDiff bool | ||
}{ | ||
{ | ||
testName: "no diff", | ||
x: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`, | ||
y: `{"A":"test1", "B":42, "C":{"A":true}, "D": ["test2", "test3"]}`, | ||
}, | ||
{ | ||
testName: "has diff", | ||
x: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`, | ||
y: `{"A":"test1", "B":41, "C":{"A":true}, "D": ["test3"]}`, | ||
wantDiff: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
output := jsoncmp.Diff(testCase.x, testCase.y) | ||
if got, want := output != "", testCase.wantDiff; got != want { | ||
t.Errorf("Diff(%s, %s) = %t, want %t", testCase.x, testCase.y, got, want) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io" | ||
"strings" | ||
) | ||
|
||
// DecodeFromBytes decodes (unmarshals) the given byte slice, containing valid JSON, into `to`. | ||
func DecodeFromBytes(b []byte, to any) error { | ||
return DecodeFromReader(bytes.NewReader(b), to) | ||
} | ||
|
||
// DecodeFromReader decodes (unmarshals) the given io.Reader, pointing to a JSON stream, into `to`. | ||
func DecodeFromReader(r io.Reader, to any) error { | ||
dec := json.NewDecoder(r) | ||
|
||
for { | ||
if err := dec.Decode(to); err == io.EOF { | ||
break | ||
} else if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DecodeFromString decodes (unmarshals) the given string, containing valid JSON, into `to`. | ||
func DecodeFromString(s string, to any) error { | ||
return DecodeFromReader(strings.NewReader(s), to) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-provider-aws/internal/json" | ||
) | ||
|
||
func TestDecodeFromString(t *testing.T) { | ||
t.Parallel() | ||
|
||
type to struct { | ||
A string | ||
B int | ||
C struct { | ||
A bool | ||
} | ||
D []string | ||
} | ||
var to0, to1, to2, to3 to | ||
to4 := to{ | ||
A: "test1", | ||
B: 42, | ||
C: struct { | ||
A bool | ||
}{A: true}, | ||
D: []string{"test2", "test3"}, | ||
} | ||
|
||
testCases := []struct { | ||
testName string | ||
input string | ||
output any | ||
wantOutput any | ||
wantErr bool | ||
}{ | ||
{ | ||
testName: "empty JSON", | ||
input: `{}`, | ||
output: &to1, | ||
wantOutput: &to0, | ||
}, | ||
{ | ||
testName: "bad JSON", | ||
input: `{test`, | ||
output: &to2, | ||
wantErr: true, | ||
}, | ||
{ | ||
testName: "full JSON", | ||
input: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`, | ||
output: &to3, | ||
wantOutput: &to4, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := json.DecodeFromString(testCase.input, testCase.output) | ||
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) { | ||
t.Errorf("DecodeFromString(%s) err %t, want %t", testCase.input, got, want) | ||
} | ||
if err == nil { | ||
if diff := cmp.Diff(testCase.output, testCase.wantOutput); diff != "" { | ||
t.Errorf("unexpected diff (+wanted, -got): %s", diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
) | ||
|
||
// EncodeToBytes JSON encodes (marshals) `from` into a byte slice. | ||
func EncodeToBytes(from any) ([]byte, error) { | ||
to, err := encodeToBuffer(from) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return to.Bytes(), nil | ||
} | ||
|
||
// EncodeToString JSON encodes (marshals) `from` into a string. | ||
func EncodeToString(from any) (string, error) { | ||
to, err := encodeToBuffer(from) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return to.String(), nil | ||
} | ||
|
||
func encodeToBuffer(from any) (*bytes.Buffer, error) { | ||
to := new(bytes.Buffer) | ||
enc := json.NewEncoder(to) | ||
|
||
if err := enc.Encode(from); err != nil { | ||
return nil, err | ||
} | ||
|
||
return to, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest/jsoncmp" | ||
"github.com/hashicorp/terraform-provider-aws/internal/json" | ||
) | ||
|
||
func TestEncodeToString(t *testing.T) { | ||
t.Parallel() | ||
|
||
type from struct { | ||
A string | ||
B int | ||
C struct { | ||
A bool | ||
} | ||
D []string | ||
} | ||
var from0 from | ||
from1 := from{ | ||
A: "test1", | ||
B: 42, | ||
C: struct { | ||
A bool | ||
}{A: true}, | ||
D: []string{"test2", "test3"}, | ||
} | ||
|
||
testCases := []struct { | ||
testName string | ||
input any | ||
wantOutput string | ||
wantErr bool | ||
}{ | ||
{ | ||
testName: "empty JSON", | ||
input: &from0, | ||
wantOutput: `{"A": "", "B": 0, "C": {"A": false}, "D": null}`, | ||
}, | ||
{ | ||
testName: "empty JSON", | ||
input: &from1, | ||
wantOutput: `{"A": "test1", "D": ["test2", "test3"], "C": {"A": true}, "B": 42}`, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
output, err := json.EncodeToString(testCase.input) | ||
if got, want := err != nil, testCase.wantErr; !cmp.Equal(got, want) { | ||
t.Errorf("EncodeToString(%v) err %t, want %t", testCase.input, got, want) | ||
} | ||
if err == nil { | ||
if diff := jsoncmp.Diff(output, testCase.wantOutput); diff != "" { | ||
t.Errorf("unexpected diff (+wanted, -got): %s", diff) | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
// EqualBytes returns whether the JSON documents in the given byte slices are equal. | ||
func EqualBytes(b1, b2 []byte) bool { | ||
var v1 any | ||
if err := DecodeFromBytes(b1, &v1); err != nil { | ||
return false | ||
} | ||
|
||
var v2 any | ||
if err := DecodeFromBytes(b2, &v2); err != nil { | ||
return false | ||
} | ||
|
||
return reflect.DeepEqual(v1, v2) | ||
} | ||
|
||
// EqualStrings returns whether the JSON documents in the given strings are equal. | ||
func EqualStrings(s1, s2 string) bool { | ||
return EqualBytes([]byte(s1), []byte(s2)) | ||
} |
Oops, something went wrong.