Skip to content

Commit

Permalink
Ensure we normalize JSON result
Browse files Browse the repository at this point in the history
  • Loading branch information
bdwyertech committed Feb 23, 2022
1 parent f2c5d8f commit 986d256
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 49 deletions.
72 changes: 35 additions & 37 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,34 @@ before:
# this is just an example and not a requirement for provider building/publishing
- go mod tidy
builds:
- env:
# goreleaser does not work with CGO, it could also complicate
# usage by users in CI/CD systems like Terraform Cloud where
# they are unable to install libraries.
- CGO_ENABLED=0
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -mod=vendor
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
goos:
- freebsd
- windows
- linux
- darwin
goarch:
- amd64
- '386'
- arm
- arm64
ignore:
- goos: darwin
goarch: '386'
binary: '{{ .ProjectName }}_v{{ .Version }}'
- env:
# goreleaser does not work with CGO, it could also complicate
# usage by users in CI/CD systems like Terraform Cloud where
# they are unable to install libraries.
- CGO_ENABLED=0
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -mod=vendor
- -trimpath
ldflags:
- '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}'
goos:
- freebsd
- windows
- linux
- darwin
goarch:
- amd64
- '386'
- arm
- arm64
ignore:
- goos: darwin
goarch: '386'
binary: '{{ .ProjectName }}_v{{ .Version }}'
archives:
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
- format: zip
name_template: '{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}'
checksum:
extra_files:
- glob: 'terraform-registry-manifest.json'
Expand All @@ -42,20 +42,18 @@ checksum:
signs:
- artifacts: checksum
args:
# if you are using this in a GitHub action or some other automated pipeline, you
# if you are using this in a GitHub action or some other automated pipeline, you
# need to pass the batch flag to indicate its not interactive.
- "--batch"
- "--local-user"
- "{{ .Env.GPG_FINGERPRINT }}" # set this environment variable for your signing key
- "--output"
- "${signature}"
- "--detach-sign"
- "${artifact}"
- '--batch'
- '--local-user'
- '{{ .Env.GPG_FINGERPRINT }}' # set this environment variable for your signing key
- '--output'
- '${signature}'
- '--detach-sign'
- '${artifact}'
release:
extra_files:
- glob: 'terraform-registry-manifest.json'
name_template: '{{ .ProjectName }}_{{ .Version }}_manifest.json'
# If you want to manually examine the release before its live, uncomment this line:
# draft: true
changelog:
skip: true
3 changes: 2 additions & 1 deletion internal/provider/data_json2dynamodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func dataRead(ctx context.Context, d *schema.ResourceData, meta interface{}) dia

if specJson := d.Get("spec").(string); specJson != "" {
schema := new(spec.Schema)
if err := json.Unmarshal([]byte(specJson), schema); err != nil {
if err := schema.UnmarshalJSON([]byte(specJson)); err != nil {
return diag.Diagnostics{
{
Severity: diag.Error,
Expand All @@ -87,6 +87,7 @@ func dataRead(ctx context.Context, d *schema.ResourceData, meta interface{}) dia
},
}
}

if err := validate.AgainstSchema(schema, jInt, strfmt.Default); err != nil {
return diag.Diagnostics{
{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/data_json2dynamodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ output "ddbjson" {
}
`

var basicExpectedOutput = `{"cookbook_versions":{"M":{"wildfly":{"S":"> 0.0.0"}}},"default_attributes":{"M":{"wildfly":{"M":{"config":{"M":{"abc":{"N":"123"}}}}}}},"description":{"S":"Brian's Test Environment"},"name":{"S":"briansenvtest"}}`
var basicExpectedOutput = `{"chef_type":{"S":"environment"},"cookbook_versions":{"M":{"wildfly":{"S":"\u003e 0.0.0"}}},"default_attributes":{"M":{"wildfly":{"M":{"config":{"M":{"abc":{"N":"123"}}}}}}},"description":{"S":"Brian's Test Environment"},"json_class":{"S":"Chef::Environment"},"name":{"S":"briansenvtest"}}`

func TestDataSource_basic(t *testing.T) {
resource.UnitTest(t, resource.TestCase{
Expand Down
20 changes: 10 additions & 10 deletions internal/provider/dynamodb_encoding.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package provider

import (
"encoding/json"
"fmt"
"sort"

"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
smithyjson "github.com/aws/smithy-go/encoding/json"
Expand All @@ -12,13 +12,7 @@ func SerializeAttributeMap(v map[string]types.AttributeValue) (jsonBytes []byte,
value := smithyjson.NewEncoder()
object := value.Object()

keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)

for _, key := range keys {
for key := range v {
om := object.Key(key)
if vv := v[key]; vv == nil {
continue
Expand All @@ -28,8 +22,14 @@ func SerializeAttributeMap(v map[string]types.AttributeValue) (jsonBytes []byte,
}
}
object.Close()
jsonBytes = value.Bytes()
return

var j json.RawMessage
err = json.Unmarshal(value.Bytes(), &j)
if err != nil {
return
}

return json.Marshal(j)
}

func serializeDocumentAttributeValue(v types.AttributeValue, value smithyjson.Value) error {
Expand Down

0 comments on commit 986d256

Please sign in to comment.