Skip to content

Commit

Permalink
Introduce AssetOrArchive to support the SDK's Asset type which can be…
Browse files Browse the repository at this point in the history
… both
  • Loading branch information
thomas11 committed Jun 4, 2024
1 parent 2bf8636 commit 952f307
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
14 changes: 12 additions & 2 deletions infer/internal/ende/ende.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"reflect"

"github.com/pulumi/pulumi-go-provider/internal/introspect"
"github.com/pulumi/pulumi-go-provider/types"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/mapper"
Expand Down Expand Up @@ -60,7 +62,6 @@ func decode(
IgnoreUnrecognized: ignoreUnrecognized,
IgnoreMissing: allowMissing,
}).Decode(m.Mappable(), target.Addr().Interface())

}

func DecodeAny(m resource.PropertyMap, dst any) (Encoder, mapper.MappingError) {
Expand Down Expand Up @@ -188,8 +189,17 @@ func (e *ende) walk(
if typ == nil || typ.Kind() != reflect.Struct {
return e.walkMap(v, path, elemType, alignTypes)
}
// This is a scalar value, so we can return it as is.
// This is a scalar value, so we can return it as is. The exception is types.AssetOrArchive, which we translate
// to a types.Asset or types.Archive, depending on what it contains, to match the SDK's AssetOrArchive type.
default:
if typ == reflect.TypeOf(types.AssetOrArchive{}) {
if v.IsAsset() {
v = resource.NewPropertyValue(types.AssetOrArchive{Asset: v.AssetValue()})
} else if v.IsArchive() {
v = resource.NewPropertyValue(types.AssetOrArchive{Archive: v.ArchiveValue()})
}
}

return v
}
}
Expand Down
77 changes: 77 additions & 0 deletions infer/internal/ende/ende_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ import (
"reflect"
"testing"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
r "github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/archive"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"pgregory.net/rapid"

rType "github.com/pulumi/pulumi-go-provider/internal/rapid/reflect"
rResource "github.com/pulumi/pulumi-go-provider/internal/rapid/resource"
"github.com/pulumi/pulumi-go-provider/types"
)

// testRoundTrip asserts that the result of pMap can be decoded onto T, and then
Expand Down Expand Up @@ -144,3 +148,76 @@ func TestRoundtripIn(t *testing.T) {
}
})
}

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

type foo struct {
AA types.AssetOrArchive `pulumi:"aa"`
}

simplify := func(v any) r.PropertyMap {
m := resource.NewPropertyMap(v)
e := ende{}
return e.simplify(m, reflect.TypeOf(v))
}

assertDecodedFoo := func(kind string, m r.PropertyMap) {
key := r.PropertyKey(kind)

require.True(t, m["aa"].IsObject())
obj := m["aa"].ObjectValue()
require.True(t, obj.HasValue(key))
require.Len(t, obj, 1)

require.True(t, obj[key].IsObject())
arch := obj[key].ObjectValue()
require.True(t, arch.HasValue("path"))
}

t.Run("asset", func(t *testing.T) {
asset := asset.Asset{
Path: "asset://foo",
}
f := foo{
AA: types.AssetOrArchive{Asset: &asset},
}

mNew := simplify(f)

assertDecodedFoo("asset", mNew)
})

t.Run("archive", func(t *testing.T) {
archive := archive.Archive{
Path: "/data",
}
f := foo{
AA: types.AssetOrArchive{Archive: &archive},
}

mNew := simplify(f)

assertDecodedFoo("archive", mNew)
})

type bar struct {
Foo foo `pulumi:"foo"`
}

t.Run("nested", func(t *testing.T) {
asset := asset.Asset{
Path: "asset://foo",
}
f := foo{
AA: types.AssetOrArchive{Asset: &asset},
}
b := bar{Foo: f}

mNew := simplify(b)

require.True(t, mNew["foo"].IsObject())
inner := mNew["foo"].ObjectValue()
assertDecodedFoo("asset", inner)
})
}
6 changes: 6 additions & 0 deletions infer/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

"github.com/pulumi/pulumi-go-provider/internal/introspect"
sch "github.com/pulumi/pulumi-go-provider/middleware/schema"
"github.com/pulumi/pulumi-go-provider/types"
)

func getAnnotated(t reflect.Type) introspect.Annotator {
Expand Down Expand Up @@ -127,6 +128,11 @@ func serializeTypeAsPropertyType(
Ref: "pulumi.json#/Archive",
}, nil
}
if t == reflect.TypeOf(types.AssetOrArchive{}) {
return schema.TypeSpec{
Ref: "pulumi.json#/Asset",
}, nil
}
if enum, ok := isEnum(t); ok {
return schema.TypeSpec{
Ref: "#/types/" + enum.token,
Expand Down
22 changes: 22 additions & 0 deletions types/asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 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 types

import "github.com/pulumi/pulumi/sdk/v3/go/common/resource"

type AssetOrArchive struct {
Asset *resource.Asset `pulumi:"asset,optional"`
Archive *resource.Archive `pulumi:"archive,optional"`
}

0 comments on commit 952f307

Please sign in to comment.