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

Introduce AssetOrArchive to support the SDK's Asset type which can be both #242

Merged
merged 20 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()})
thomas11 marked this conversation as resolved.
Show resolved Hide resolved
}
}

return v
}
}
Expand Down
82 changes: 82 additions & 0 deletions infer/internal/ende/ende_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import (
"testing"

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 +147,82 @@ 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 := r.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) {
t.Parallel()

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) {
t.Parallel()

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) {
t.Parallel()

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
}
thomas11 marked this conversation as resolved.
Show resolved Hide resolved
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 {
thomas11 marked this conversation as resolved.
Show resolved Hide resolved
Asset *resource.Asset `pulumi:"asset,optional"`
Archive *resource.Archive `pulumi:"archive,optional"`
}
Loading