Skip to content

Commit

Permalink
Don't Base64 Encode Metadata (#913)
Browse files Browse the repository at this point in the history
* Don't Base64 Encode Metadata

* Add test
  • Loading branch information
iwahbe committed Mar 24, 2023
1 parent 8497d6c commit 4f8616f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
23 changes: 11 additions & 12 deletions unstable/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ import (
)

// The underlying value of a metadata blob.
type Data struct {
m map[string]json.RawMessage
}
type Data struct{ m map[string]*json.RawMessage }

func New(data []byte) (*Data, error) {
m := map[string]json.RawMessage{}
m := map[string]*json.RawMessage{}
if len(data) > 0 {
err := json.Unmarshal(data, &m)
if err != nil {
Expand All @@ -38,7 +36,7 @@ func New(data []byte) (*Data, error) {

func (d *Data) Marshal() []byte {
if d == nil {
d = &Data{m: make(map[string]json.RawMessage)}
d = &Data{m: make(map[string]*json.RawMessage)}
}
bytes, err := json.MarshalIndent(d.m, "", " ")
// `d.m` is a `map[string]json.RawMessage`. `json.MarshalIndent` errors only when
Expand All @@ -60,7 +58,8 @@ func Set(d *Data, key string, value any) error {
if err != nil {
return err
}
d.m[key] = data
msg := json.RawMessage(data)
d.m[key] = &msg
return nil
}

Expand All @@ -70,27 +69,27 @@ func Get[T any](d *Data, key string) (T, bool, error) {
if !ok {
return t, false, nil
}
err := json.Unmarshal(data, &t)
err := json.Unmarshal(*data, &t)
return t, true, err
}

func Clone(data *Data) *Data {
if data == nil {
return nil
}
m := make(map[string]json.RawMessage, len(data.m))
m := make(map[string]*json.RawMessage, len(data.m))
for k, v := range data.m {
dst := make(json.RawMessage, len(v))
n := copy(dst, v)
dst := make(json.RawMessage, len(*v))
n := copy(dst, *v)
// According to the documentation for `copy`:
//
// Copy returns the number of elements copied, which will be the minimum
// of len(src) and len(dst).
//
// Since `len(src)` is `len(dst)`, and `copy` cannot copy more bytes the
// its source, we know that `n == len(v)`.
contract.Assertf(n == len(v), "failed to perform full copy")
m[k] = dst
contract.Assertf(n == len(*v), "failed to perform full copy")
m[k] = &dst
}
return &Data{m}

Expand Down
37 changes: 37 additions & 0 deletions unstable/metadata/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metadata

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMarshal(t *testing.T) {
data, err := New(nil)
require.NoError(t, err)

err = Set(data, "hi", []string{"hello", "world"})
assert.NoError(t, err)

assert.Equal(t, `{
"hi": [
"hello",
"world"
]
}`, string(data.Marshal()))
}

0 comments on commit 4f8616f

Please sign in to comment.