Skip to content

Commit

Permalink
Normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 15, 2024
1 parent d963913 commit c01cde3
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 137 deletions.
125 changes: 2 additions & 123 deletions pf/tests/util/property/pf/value/provider/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@
package provider

import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/zclconf/go-cty/cty"
"pgregory.net/rapid"

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

func boolVal(t *rapid.T) value {
Expand Down Expand Up @@ -51,122 +49,3 @@ func numberVal(t *rapid.T) value {
Pu: resource.NewProperty(f),
}
}

func makeConvertMap(elem cty.Type) func(map[string]value) value {
return func(m map[string]value) value { return convertMap(m, elem) }
}

func convertMap(m map[string]value, elem cty.Type) value {
tfMap, puMap := make(map[string]cty.Value, len(m)), make(resource.PropertyMap, len(m))
for k, v := range m {
tfMap[k] = v.Tf
if v.hasValue {
puMap[resource.PropertyKey(k)] = v.Pu
}
}

ctyMap := cty.MapValEmpty(elem)
if len(tfMap) > 0 {
ctyMap = cty.MapVal(tfMap)
}

return value{
hasValue: true,
Tf: ctyMap,
Pu: resource.NewProperty(puMap),
}
}

func convertObject(m map[string]value, names map[string]resource.PropertyKey) value {
tfMap, puMap := make(map[string]cty.Value, len(m)), make(resource.PropertyMap, len(m))
for k, v := range m {
tfMap[k] = v.Tf
if v.hasValue {
puMap[names[k]] = v.Pu
}
}
return value{
hasValue: true,
Tf: cty.ObjectVal(tfMap),
Pu: resource.NewProperty(puMap),
}
}

func makeConvertSet(elem cty.Type) func([]value) value {
return func(a []value) value { return convertSet(a, elem) }
}

func convertSet(a []value, elem cty.Type) value {
tfArr, puArr := make([]cty.Value, len(a)), make([]resource.PropertyValue, len(a))
for i, v := range a {
tfArr[i] = v.Tf
puArr[i] = v.Pu
}

ctyList := cty.SetValEmpty(elem)
if len(tfArr) > 0 {
ctyList = cty.SetVal(tfArr)
}

return value{
hasValue: true,
Tf: ctyList,
Pu: resource.NewProperty(puArr),
}
}

func makeConvertList(elem cty.Type) func([]value) value {
return func(a []value) value { return convertList(a, elem) }
}

func convertList(a []value, elem cty.Type) value {
tfArr, puArr := make([]cty.Value, len(a)), make([]resource.PropertyValue, len(a))
for i, v := range a {
tfArr[i] = v.Tf
puArr[i] = v.Pu
}

ctyList := cty.ListValEmpty(elem)
if len(tfArr) > 0 {
ctyList = cty.ListVal(tfArr)
}

return value{
hasValue: true,
Tf: ctyList,
Pu: resource.NewProperty(puArr),
}
}

func ctyType(typ tftypes.Type) cty.Type {
switch {
case typ.Is(tftypes.Bool):
return cty.Bool
case typ.Is(tftypes.Number):
return cty.Number
case typ.Is(tftypes.String):
return cty.String
case typ.Is(tftypes.Map{}):
return cty.Map(ctyType(typ.(tftypes.Map).ElementType))
case typ.Is(tftypes.List{}):
return cty.List(ctyType(typ.(tftypes.List).ElementType))
case typ.Is(tftypes.Set{}):
return cty.Set(ctyType(typ.(tftypes.Set).ElementType))
case typ.Is(tftypes.Object{}):
o := typ.(tftypes.Object)
attrs := make(map[string]cty.Type, len(o.AttributeTypes))
optionals := make([]string, 0, len(o.OptionalAttributes))

for k, v := range o.AttributeTypes {
attrs[k] = ctyType(v)
}
for k := range o.OptionalAttributes {
optionals = append(optionals, k)
}

return cty.ObjectWithOptionalAttrs(attrs, optionals)
default:
panic(fmt.Sprintf("Unknown tftypes.Type: %v", typ))
}

}
160 changes: 160 additions & 0 deletions pf/tests/util/property/pf/value/provider/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright 2016-2024, 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 provider

import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/zclconf/go-cty/cty"

shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
)

func shimType(t tftypes.Type) shim.ValueType {
switch {
case t.Is(tftypes.Set{}):
return shim.TypeSet
case t.Is(tftypes.Map{}):
return shim.TypeMap
case t.Is(tftypes.List{}):
return shim.TypeList
default:
// This is only used when interacting with
// [tfbridge.TerraformToPulumiNameV2], so other scalar types don't matter
// here.
return shim.TypeBool
}
}

func makeConvertMap(elem cty.Type) func(map[string]value) value {
return func(m map[string]value) value { return convertMap(m, elem) }
}

func convertMap(m map[string]value, elem cty.Type) value {
tfMap, puMap := make(map[string]cty.Value, len(m)), make(resource.PropertyMap, len(m))
for k, v := range m {
tfMap[k] = v.Tf
if v.hasValue {
puMap[resource.PropertyKey(k)] = v.Pu
}
}

ctyMap := cty.MapValEmpty(elem)
if len(tfMap) > 0 {
ctyMap = cty.MapVal(tfMap)
}

return value{
hasValue: true,
Tf: ctyMap,
Pu: resource.NewProperty(puMap),
}
}

func convertObject(m map[string]value, names map[string]resource.PropertyKey) value {
tfMap, puMap := make(map[string]cty.Value, len(m)), make(resource.PropertyMap, len(m))
for k, v := range m {
tfMap[k] = v.Tf
if v.hasValue {
puMap[names[k]] = v.Pu
}
}
return value{
hasValue: true,
Tf: cty.ObjectVal(tfMap),
Pu: resource.NewProperty(puMap),
}
}

func makeConvertSet(elem cty.Type) func([]value) value {
return func(a []value) value { return convertSet(a, elem) }
}

func convertSet(a []value, elem cty.Type) value {
tfArr, puArr := make([]cty.Value, len(a)), make([]resource.PropertyValue, len(a))
for i, v := range a {
tfArr[i] = v.Tf
puArr[i] = v.Pu
}

ctyList := cty.SetValEmpty(elem)
if len(tfArr) > 0 {
ctyList = cty.SetVal(tfArr)
}

return value{
hasValue: true,
Tf: ctyList,
Pu: resource.NewProperty(puArr),
}
}

func makeConvertList(elem cty.Type) func([]value) value {
return func(a []value) value { return convertList(a, elem) }
}

func convertList(a []value, elem cty.Type) value {
tfArr, puArr := make([]cty.Value, len(a)), make([]resource.PropertyValue, len(a))
for i, v := range a {
tfArr[i] = v.Tf
puArr[i] = v.Pu
}

ctyList := cty.ListValEmpty(elem)
if len(tfArr) > 0 {
ctyList = cty.ListVal(tfArr)
}

return value{
hasValue: true,
Tf: ctyList,
Pu: resource.NewProperty(puArr),
}
}

func ctyType(typ tftypes.Type) cty.Type {
switch {
case typ.Is(tftypes.Bool):
return cty.Bool
case typ.Is(tftypes.Number):
return cty.Number
case typ.Is(tftypes.String):
return cty.String
case typ.Is(tftypes.Map{}):
return cty.Map(ctyType(typ.(tftypes.Map).ElementType))
case typ.Is(tftypes.List{}):
return cty.List(ctyType(typ.(tftypes.List).ElementType))
case typ.Is(tftypes.Set{}):
return cty.Set(ctyType(typ.(tftypes.Set).ElementType))
case typ.Is(tftypes.Object{}):
o := typ.(tftypes.Object)
attrs := make(map[string]cty.Type, len(o.AttributeTypes))
optionals := make([]string, 0, len(o.OptionalAttributes))

for k, v := range o.AttributeTypes {
attrs[k] = ctyType(v)
}
for k := range o.OptionalAttributes {
optionals = append(optionals, k)
}

return cty.ObjectWithOptionalAttrs(attrs, optionals)
default:
panic(fmt.Sprintf("Unknown tftypes.Type: %v", typ))
}

}
14 changes: 0 additions & 14 deletions pf/tests/util/property/pf/value/provider/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"pgregory.net/rapid"

"github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfbridge"
shim "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim"
shimschema "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/tfshim/schema"

"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
Expand Down Expand Up @@ -71,19 +70,6 @@ func WithValue(s schema.Schema) *rapid.Generator[Value] {
)
}

func shimType(t tftypes.Type) shim.ValueType {
switch {
case t.Is(tftypes.Set{}):
return shim.TypeSet
case t.Is(tftypes.Map{}):
return shim.TypeMap
case t.Is(tftypes.List{}):
return shim.TypeList
default:
return shim.TypeBool
}
}

func (g generator) withAttr(attr schema.Attribute) *rapid.Generator[value] {
contract.Assertf(g.isInput, "only input values are implemented")

Expand Down

0 comments on commit c01cde3

Please sign in to comment.