Visitar URL original
Switch the PCL interpreter to use `property.{Map,Value}` (#24942) · pulumi/pulumi@b87d121 · GitHub
Skip to content

Commit b87d121

Browse files
authored
Switch the PCL interpreter to use property.{Map,Value} (#24942)
Switch over to `property.{Map,Value}` for the PCL interpreter. There should be no behavioral differences.
1 parent 8d2265b commit b87d121

10 files changed

Lines changed: 559 additions & 832 deletions

File tree

‎pkg/cmd/pulumi/do/do_shared.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import (
5555
"github.com/pulumi/pulumi/sdk/v3/go/common/util/cmdutil"
5656
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
5757
"github.com/pulumi/pulumi/sdk/v3/go/common/util/result"
58+
"github.com/pulumi/pulumi/sdk/v3/go/property"
5859
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
5960
codegenrpc "github.com/pulumi/pulumi/sdk/v3/proto/go/codegen"
6061
)
@@ -272,8 +273,8 @@ func evaluatePCL(
272273
func(context.Context, string) (*schema.Function, error) {
273274
return nil, notSupported("reference functions")
274275
},
275-
func(context.Context, resource.ResourceReference) (resource.PropertyMap, error) {
276-
return nil, notSupported("reference resources")
276+
func(context.Context, property.ResourceReference) (property.Map, error) {
277+
return property.Map{}, notSupported("reference resources")
277278
},
278279
func(context.Context, *pulumirpc.ResourceInvokeRequest) (*pulumirpc.ResourceInvokeResponse, error) {
279280
return nil, notSupported("invoke functions")
@@ -291,7 +292,7 @@ func evaluatePCL(
291292
if diags.HasErrors() {
292293
return nil, diags
293294
}
294-
return result, nil
295+
return resource.ToResourcePropertyMap(result), nil
295296
}
296297

297298
// parseFile reads an input file in the given format and returns it ready for evaluation. For non-PCL formats the source

‎pkg/pcl/runtime/builtinFunctions.go‎

Lines changed: 40 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@ import (
3030
"github.com/hashicorp/hcl/v2"
3131
"github.com/hashicorp/hcl/v2/ext/customdecode"
3232
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
33-
"github.com/pulumi/pulumi/pkg/v3/resource/plugin"
3433
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
3534
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/archive"
3635
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/asset"
36+
"github.com/pulumi/pulumi/sdk/v3/go/property"
37+
"github.com/pulumi/pulumi/sdk/v3/go/propertyrpc"
3738
pulumirpc "github.com/pulumi/pulumi/sdk/v3/proto/go"
3839
"github.com/zclconf/go-cty/cty"
3940
"github.com/zclconf/go-cty/cty/function"
@@ -53,7 +54,7 @@ var invokeOptionsType = cty.ObjectWithOptionalAttrs(map[string]cty.Type{
5354

5455
func tryExpressions(
5556
args []cty.Value,
56-
getResource func(context.Context, resource.ResourceReference) (resource.PropertyMap, error),
57+
getResource func(context.Context, property.ResourceReference) (property.Map, error),
5758
) (cty.Value, error) {
5859
if len(args) == 0 {
5960
return cty.NilVal, errors.New("at least one argument is required")
@@ -100,7 +101,7 @@ func tryExpressions(
100101

101102
func recoverExpression(
102103
args []cty.Value,
103-
getResource func(context.Context, resource.ResourceReference) (resource.PropertyMap, error),
104+
getResource func(context.Context, property.ResourceReference) (property.Map, error),
104105
) (cty.Value, error) {
105106
if len(args) != 2 {
106107
return cty.NilVal, errors.New("recover requires a value and recovery expression")
@@ -266,8 +267,8 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
266267
if err != nil {
267268
return cty.False, nil
268269
}
269-
if pv.IsSecret() {
270-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.MakeSecret(resource.NewProperty(true)))
270+
if pv.Secret() {
271+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(true).WithSecret(true))
271272
}
272273
return cty.True, nil
273274
},
@@ -295,7 +296,6 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
295296
if err != nil {
296297
return cty.DynamicVal, nil
297298
}
298-
stackRefPV, _ = unwrapOutputs(stackRefPV)
299299
if stackRefPV.IsNull() {
300300
return cty.DynamicVal, nil
301301
}
@@ -354,29 +354,18 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
354354
if err != nil {
355355
return cty.NilVal, fmt.Errorf("invalid invoke arguments: %w", err)
356356
}
357-
argsPV, dependsOn := unwrapOutputs(argsPV)
357+
dependsOn := allDependencies(argsPV)
358358
if fun.Inputs != nil {
359-
args, err := applySchemaInputs(argsPV.ObjectValue(), fun.Inputs.Properties)
359+
args, err := applySchemaInputs(argsPV.AsMap(), fun.Inputs.Properties)
360360
if err != nil {
361361
return cty.NilVal, fmt.Errorf("convert invoke arguments: %w", err)
362362
}
363-
argsPV = resource.NewProperty(args)
364-
}
365-
366-
marshalOpts := plugin.MarshalOptions{
367-
KeepUnknowns: true,
368-
KeepSecrets: true,
369-
KeepResources: true,
370-
KeepByteString: true,
371-
}
372-
obj, err := plugin.MarshalProperties(argsPV.ObjectValue(), marshalOpts)
373-
if err != nil {
374-
return cty.NilVal, fmt.Errorf("marshal invoke arguments: %w", err)
363+
argsPV = property.New(args)
375364
}
376365

377366
request := &pulumirpc.ResourceInvokeRequest{
378367
Tok: token,
379-
Args: obj,
368+
Args: propertyrpc.Marshal(argsPV.AsMap()),
380369
AcceptsByteString: true,
381370
}
382371

@@ -442,35 +431,27 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
442431
}
443432

444433
if resp.Unknown {
445-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(resource.Output{
446-
Known: false,
447-
Dependencies: dependsOn,
448-
}))
434+
return propertyValueToCty(context.TODO(), ectx.getResource,
435+
property.New(property.Computed).WithDependencies(dependsOn))
449436
}
450437

451-
resultPM, err := plugin.UnmarshalProperties(resp.GetReturn(), marshalOpts)
438+
resultPM, err := propertyrpc.Unmarshal(resp.GetReturn())
452439
if err != nil {
453440
return cty.NilVal, fmt.Errorf("unmarshal invoke result: %w", err)
454441
}
455442
// If this is a scalar invoke pull off the one property in the map
456-
var resultPV resource.PropertyValue
443+
var resultPV property.Value
457444
if _, ok := fun.ReturnType.(*schema.ObjectType); ok {
458-
resultPV = resource.NewProperty(resultPM)
445+
resultPV = property.New(resultPM)
459446
} else {
460-
if len(resultPM) != 1 {
461-
return cty.NilVal, fmt.Errorf("expected scalar invoke result to have exactly one property, got %d", len(resultPM))
447+
if resultPM.Len() != 1 {
448+
return cty.NilVal, fmt.Errorf("expected scalar invoke result to have exactly one property, got %d", resultPM.Len())
462449
}
463-
for _, v := range resultPM {
450+
for _, v := range resultPM.All {
464451
resultPV = v
465452
}
466453
}
467-
if len(dependsOn) > 0 {
468-
resultPV = resource.NewProperty(resource.Output{
469-
Element: resultPV,
470-
Known: true,
471-
Dependencies: dependsOn,
472-
})
473-
}
454+
resultPV = resultPV.WithDependencies(append(resultPV.Dependencies(), dependsOn...))
474455
return propertyValueToCty(context.TODO(), ectx.getResource, resultPV)
475456
},
476457
})
@@ -539,8 +520,8 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
539520
if err != nil {
540521
return cty.NilVal, fmt.Errorf("invalid invoke arguments: %w", err)
541522
}
542-
argsPV, _ = unwrapOutputs(argsPV)
543-
argsPM := argsPV.ObjectValue()
523+
// A resource that is passed directly must not be a dependency of its own argument.
524+
argsPM := collapseResourceReferences(argsPV).AsMap()
544525
if fun.Inputs != nil {
545526
argsPM, err = applySchemaInputs(argsPM, fun.Inputs.Properties)
546527
if err != nil {
@@ -558,30 +539,18 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
558539
}
559540

560541
// The ID is unknown when self is a resource still being created during a preview.
561-
idPV := resource.MakeComputed(resource.NewProperty(""))
542+
idPV := property.New(property.Computed)
562543
if id.IsKnown() {
563-
idPV = resource.NewProperty(id.AsString())
544+
idPV = property.New(id.AsString())
564545
}
565-
argsPM["__self__"] = resource.NewProperty(resource.ResourceReference{
546+
argsPM = argsPM.Set("__self__", property.New(property.ResourceReference{
566547
URN: resource.URN(urnVal.AsString()),
567548
ID: idPV,
568-
})
569-
570-
marshalOpts := plugin.MarshalOptions{
571-
KeepUnknowns: true,
572-
KeepSecrets: true,
573-
KeepResources: true,
574-
KeepOutputValues: true,
575-
KeepByteString: true,
576-
}
577-
obj, err := plugin.MarshalProperties(argsPM, marshalOpts)
578-
if err != nil {
579-
return cty.NilVal, fmt.Errorf("marshal invoke arguments: %w", err)
580-
}
549+
}))
581550

582551
request := &pulumirpc.ResourceCallRequest{
583552
Tok: fun.Token,
584-
Args: obj,
553+
Args: propertyrpc.Marshal(argsPM),
585554
AcceptsByteString: true,
586555
}
587556

@@ -626,36 +595,30 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
626595
return cty.NilVal, errors.New(buf.String())
627596
}
628597

629-
resultPM, err := plugin.UnmarshalProperties(resp.GetReturn(), marshalOpts)
598+
resultPM, err := propertyrpc.Unmarshal(resp.GetReturn())
630599
if err != nil {
631600
return cty.NilVal, fmt.Errorf("unmarshal invoke result: %w", err)
632601
}
633602
// Methods declared with ReturnTypePlain but no object return type carry the single value in a
634603
// property map with exactly one entry, whose key may be any name. Unwrap it so callers get the
635604
// value directly.
636-
var resultPV resource.PropertyValue
605+
var resultPV property.Value
637606
if fun.ReturnTypePlain {
638607
if _, isObject := fun.ReturnType.(*schema.ObjectType); !isObject {
639-
if len(resultPM) != 1 {
608+
if resultPM.Len() != 1 {
640609
return cty.NilVal, fmt.Errorf(
641-
"invoke %q: expected a single return value, got %d", fun.Token, len(resultPM))
610+
"invoke %q: expected a single return value, got %d", fun.Token, resultPM.Len())
642611
}
643-
for _, v := range resultPM {
612+
for _, v := range resultPM.All {
644613
resultPV = v
645614
}
646615
} else {
647-
resultPV = resource.NewProperty(resultPM)
616+
resultPV = property.New(resultPM)
648617
}
649618
} else {
650-
resultPV = resource.NewProperty(resultPM)
651-
}
652-
if len(dependsOn) > 0 {
653-
resultPV = resource.NewProperty(resource.Output{
654-
Element: resultPV,
655-
Known: true,
656-
Dependencies: dependsOn,
657-
})
619+
resultPV = property.New(resultPM)
658620
}
621+
resultPV = resultPV.WithDependencies(append(resultPV.Dependencies(), dependsOn...))
659622
return propertyValueToCty(context.TODO(), ectx.getResource, resultPV)
660623
},
661624
})
@@ -680,7 +643,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
680643
if err != nil {
681644
return cty.NilVal, fmt.Errorf("creating file asset: %w", err)
682645
}
683-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
646+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
684647
},
685648
})
686649

@@ -704,7 +667,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
704667
if err != nil {
705668
return cty.NilVal, fmt.Errorf("creating file archive: %w", err)
706669
}
707-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
670+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
708671
},
709672
})
710673

@@ -733,7 +696,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
733696
if err != nil {
734697
return cty.NilVal, fmt.Errorf("creating archive from assets: %w", err)
735698
}
736-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
699+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
737700
},
738701
})
739702

@@ -757,7 +720,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
757720
if err != nil {
758721
return cty.NilVal, fmt.Errorf("creating string asset: %w", err)
759722
}
760-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
723+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
761724
},
762725
})
763726

@@ -781,7 +744,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
781744
if err != nil {
782745
return cty.NilVal, fmt.Errorf("creating remote asset: %w", err)
783746
}
784-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
747+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
785748
},
786749
})
787750

@@ -805,7 +768,7 @@ func (ectx *EvalContext) builtinFunctions() map[string]function.Function {
805768
if err != nil {
806769
return cty.NilVal, fmt.Errorf("creating remote archive: %w", err)
807770
}
808-
return propertyValueToCty(context.TODO(), ectx.getResource, resource.NewProperty(a))
771+
return propertyValueToCty(context.TODO(), ectx.getResource, property.New(a))
809772
},
810773
})
811774

‎pkg/pcl/runtime/builtinFunctions_test.go‎

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ import (
2424
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/syntax"
2525
"github.com/pulumi/pulumi/pkg/v3/codegen/pcl"
2626
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
27-
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
27+
"github.com/pulumi/pulumi/sdk/v3/go/property"
2828
)
2929

3030
// evaluateLocals binds a program of local variables and evaluates each one with no
3131
// resources, invokes, or environment.
32-
func evaluateLocals(t *testing.T, source string) map[string]resource.PropertyValue {
32+
func evaluateLocals(t *testing.T, source string) map[string]property.Value {
3333
parser := syntax.NewParser()
3434
require.NoError(t, parser.ParseFile(strings.NewReader(source), "main.pp"))
3535
require.False(t, parser.Diagnostics.HasErrors(), parser.Diagnostics.Error())
@@ -38,7 +38,7 @@ func evaluateLocals(t *testing.T, source string) map[string]resource.PropertyVal
3838
require.False(t, diags.HasErrors(), diags.Error())
3939

4040
ectx := NewEvalContext("", "", "", "", "", nil, nil, nil, nil, nil)
41-
values := map[string]resource.PropertyValue{}
41+
values := map[string]property.Value{}
4242
for _, node := range program.Nodes {
4343
local := node.(*pcl.LocalVariable)
4444
value, poison, diags := ectx.Evaluate(local.Definition.Value)
@@ -59,28 +59,24 @@ object = length({"a" = 1, "b" = 2})
5959
emptyObject = length({})
6060
map = length({for k, v in {"a" = 1, "b" = 2, "c" = 3} : k => v})
6161
`)
62-
assert.Equal(t, map[string]resource.PropertyValue{
63-
"string": resource.NewProperty(3.0),
64-
"tuple": resource.NewProperty(3.0),
65-
"object": resource.NewProperty(2.0),
66-
"emptyObject": resource.NewProperty(0.0),
67-
"map": resource.NewProperty(3.0),
62+
assert.Equal(t, map[string]property.Value{
63+
"string": property.New(3.0),
64+
"tuple": property.New(3.0),
65+
"object": property.New(2.0),
66+
"emptyObject": property.New(0.0),
67+
"map": property.New(3.0),
6868
}, values)
6969
}
7070

7171
func TestRange(t *testing.T) {
7272
t.Parallel()
7373

74-
// An empty cty list converts to a nil slice, so the empty case is built without make.
75-
list := func(ns ...float64) resource.PropertyValue {
76-
if len(ns) == 0 {
77-
return resource.NewProperty([]resource.PropertyValue(nil))
78-
}
79-
values := make([]resource.PropertyValue, len(ns))
74+
list := func(ns ...float64) property.Value {
75+
values := make([]property.Value, len(ns))
8076
for i, n := range ns {
81-
values[i] = resource.NewProperty(n)
77+
values[i] = property.New(n)
8278
}
83-
return resource.NewProperty(values)
79+
return property.New(values)
8480
}
8581
values := evaluateLocals(t, `
8682
to = range(3)
@@ -89,7 +85,7 @@ empty = range(0)
8985
empty2 = range(3, 3)
9086
reversed = range(5, 2)
9187
`)
92-
assert.Equal(t, map[string]resource.PropertyValue{
88+
assert.Equal(t, map[string]property.Value{
9389
"to": list(0, 1, 2),
9490
"fromTo": list(2, 3, 4),
9591
"empty": list(),

0 commit comments

Comments
 (0)