Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow decoding *asset and *archive values #11053

Merged
merged 1 commit into from Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/go
description: Allow decoding *asset and *archive values
85 changes: 85 additions & 0 deletions sdk/go/common/resource/mapper_test.go
@@ -0,0 +1,85 @@
// Copyright 2016-2022, 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 resource_test

import (
"reflect"
"testing"

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

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/mapper"
)

type complexBag struct {
asset resource.Asset
archive resource.Archive

optionalAsset *resource.Asset
optionalArchive *resource.Archive
}

func TestAssetsAndArchives(t *testing.T) {
t.Parallel()

newAsset := func(s string) *resource.Asset {
a, err := resource.NewTextAsset(s)
require.NoError(t, err, "creating asset %s", s)
return a
}
newArchive := func(m map[string]interface{}) *resource.Archive {
a, err := resource.NewAssetArchive(m)
require.NoError(t, err, "creating asset %#v", m)
return a
}

bigArchive := func() *resource.Archive {
return newArchive(map[string]interface{}{
"asset1": newAsset("asset1"),
"archive1": newArchive(map[string]interface{}{
"asset2": newAsset("asset2"),
"asset3": newAsset("asset3"),
}),
})
}
tree := map[string]interface{}{
"asset": newAsset("simple asset"),
"optionalAsset": newAsset("simple optional asset"),
"archive": bigArchive(),
"optionalArchive": bigArchive(),
}

bag := complexBag{}
md := mapper.New(nil)

t.Run("asset", func(t *testing.T) { //nolint:parallelTest
err := md.DecodeValue(tree, reflect.TypeOf(complexBag{}), "asset", &bag.asset, false)
assert.Nil(t, err)
})
t.Run("optionalAsset", func(t *testing.T) { //nolint:parallelTest
err := md.DecodeValue(tree, reflect.TypeOf(complexBag{}), "optionalAsset", &bag.optionalAsset, false)
assert.Nil(t, err)
})
t.Run("archive", func(t *testing.T) { //nolint:parallelTest
err := md.DecodeValue(tree, reflect.TypeOf(complexBag{}), "archive", &bag.archive, false)
assert.Nil(t, err)
})
t.Run("optionalArchive", func(t *testing.T) { //nolint:parallelTest
err := md.DecodeValue(tree, reflect.TypeOf(complexBag{}), "optionalArchive", &bag.optionalArchive, false)
assert.Nil(t, err)
})
}
9 changes: 8 additions & 1 deletion sdk/go/common/util/mapper/mapper_decode.go
Expand Up @@ -88,13 +88,20 @@ func (md *mapper) DecodeValue(obj map[string]interface{}, ty reflect.Type, key s
if v, has := obj[key]; has {
// The field exists; okay, try to map it to the right type.
vsrc := reflect.ValueOf(v)

// If the source is a ptr, dereference it as necessary to get the underlying
// value.
for vsrc.IsValid() && vsrc.Type().Kind() == reflect.Ptr && !vsrc.IsNil() {
vsrc = vsrc.Elem()
}

// Ensure the source is valid; this is false if the value reflects the zero value.
if vsrc.IsValid() {
vdstType := vdst.Type().Elem()

// So long as the target element is a pointer, we have a pointer to pointer; dig through until we bottom out
// on the non-pointer type that matches the source. This assumes the source isn't itself a pointer!
contract.Assertf(vsrc.Type().Kind() != reflect.Ptr, "source is a pointer")
contract.Assertf(vsrc.Type().Kind() != reflect.Ptr, "source is a null pointer")
for vdstType.Kind() == reflect.Ptr {
vdst = vdst.Elem()
vdstType = vdstType.Elem()
Expand Down