Skip to content

Commit

Permalink
Merge #11053
Browse files Browse the repository at this point in the history
11053: Allow decoding *asset and *archive values r=iwahbe a=iwahbe

<!--- 
Thanks so much for your contribution! If this is your first time contributing, please ensure that you have read the [CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md) documentation.
-->

# Description

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

We handle ptr sources in deserialization. This is necessary since `pulumi.New*Asset` and `pulumi.New*Archive` return `*resource.Asset` and `*resource.Archive` respectively.

This will fix pulumi/pulumi-command#143 when it is picked up.

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [X] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [X] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
then the service should honor older versions of the CLI where this change would not exist.
You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
bors[bot] and iwahbe committed Oct 18, 2022
2 parents 3bcbe35 + 967e240 commit ed46a19
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
@@ -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

0 comments on commit ed46a19

Please sign in to comment.