-
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 #38622 from hashicorp/b-aws_ecs_task_definition.co…
…ntainer_definitions-json-uppercase r/aws_ecs_task_definition: Ensure JSON keys in `container_definitions` have leading lowercase
- Loading branch information
Showing
8 changed files
with
291 additions
and
80 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,3 @@ | ||
```release-note:bug | ||
resource/aws_ecs_task_definition: Ensure that JSON keys in `container_definitions` start with a lowercase letter | ||
``` |
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,51 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json | ||
|
||
import ( | ||
"unicode" | ||
"unicode/utf8" | ||
|
||
"github.com/hashicorp/terraform-provider-aws/internal/json/ujson" | ||
) | ||
|
||
// KeyFirstLower converts the first letter of each key to lowercase. | ||
func KeyFirstLower(in []byte) []byte { | ||
out := make([]byte, 0, len(in)) | ||
|
||
err := ujson.Walk(in, func(_ int, key, value []byte) bool { | ||
// Write to output. | ||
if len(out) != 0 && ujson.ShouldAddComma(value, out[len(out)-1]) { | ||
out = append(out, ',') | ||
} | ||
// key is the raw key of the current object or empty otherwise. | ||
// It can be a double-quoted string or empty. | ||
switch len(key) { | ||
case 0: | ||
case 1, 2: | ||
// Empty key. | ||
out = append(out, key...) | ||
default: | ||
} | ||
if len(key) > 0 { | ||
out = append(out, '"') | ||
r, n := utf8.DecodeRune(key[1:]) | ||
r = unicode.ToLower(r) | ||
low := make([]byte, utf8.RuneLen(r)) | ||
utf8.EncodeRune(low, r) | ||
out = append(out, low...) | ||
out = append(out, key[n+1:]...) | ||
out = append(out, ':') | ||
} | ||
out = append(out, value...) | ||
|
||
return true | ||
}) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
return out | ||
} |
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,68 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package json_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-provider-aws/internal/json" | ||
) | ||
|
||
func TestKeyFirstLower(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
testName string | ||
input string | ||
want string | ||
}{ | ||
{ | ||
testName: "empty JSON", | ||
input: "{}", | ||
want: "{}", | ||
}, | ||
{ | ||
testName: "single field, lowercase", | ||
input: `{ "key": 42 }`, | ||
want: `{"key":42}`, | ||
}, | ||
{ | ||
testName: "single field, uppercase", | ||
input: `{ "Key": 42 }`, | ||
want: `{"key":42}`, | ||
}, | ||
{ | ||
testName: "multiple fields", | ||
input: ` | ||
[ | ||
{ | ||
"Name": "FIRST", | ||
"Image": "alpine", | ||
"Cpu": 10, | ||
"Memory": 512, | ||
"Essential": true, | ||
"PortMappings": [ | ||
{ | ||
"ContainerPort": 80, | ||
"HostPort": 80 | ||
} | ||
] | ||
} | ||
] | ||
`, | ||
want: `[{"name":"FIRST","image":"alpine","cpu":10,"memory":512,"essential":true,"portMappings":[{"containerPort":80,"hostPort":80}]}]`, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(testCase.testName, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
if got, want := string(json.KeyFirstLower([]byte(testCase.input))), testCase.want; got != want { | ||
t.Errorf("KeyFirstLower(%q) = %q, want %q", testCase.input, got, want) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.