[codegen] Add support for default values in enum properties (#6017)
This commit is contained in:
parent
b8e31446d1
commit
0f9273fc7b
|
@ -597,11 +597,35 @@ func primitiveValue(value interface{}) (string, error) {
|
|||
func (mod *modContext) getDefaultValue(dv *schema.DefaultValue, t schema.Type) (string, error) {
|
||||
var val string
|
||||
if dv.Value != nil {
|
||||
v, err := primitiveValue(dv.Value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
switch enum := t.(type) {
|
||||
case *schema.EnumType:
|
||||
enumName := tokenToName(enum.Token)
|
||||
for _, e := range enum.Elements {
|
||||
if e.Value != dv.Value {
|
||||
continue
|
||||
}
|
||||
|
||||
elName := e.Name
|
||||
if elName == "" {
|
||||
elName = fmt.Sprintf("%v", e.Value)
|
||||
}
|
||||
safeName, err := makeSafeEnumName(elName, enumName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
val = fmt.Sprintf("%s.%s.%s", mod.namespaceName, enumName, safeName)
|
||||
break
|
||||
}
|
||||
if val == "" {
|
||||
return "", errors.Errorf("default value '%v' not found in enum '%s'", dv.Value, enumName)
|
||||
}
|
||||
default:
|
||||
v, err := primitiveValue(dv.Value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
val = v
|
||||
}
|
||||
val = v
|
||||
}
|
||||
|
||||
if len(dv.Environment) != 0 {
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace Pulumi.PlantProvider.Inputs
|
|||
|
||||
public ContainerArgs()
|
||||
{
|
||||
Brightness = Pulumi.PlantProvider.ContainerBrightness.One;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,6 +77,7 @@ namespace Pulumi.PlantProvider.Tree.V1
|
|||
|
||||
public RubberTreeArgs()
|
||||
{
|
||||
Farm = "(unknown)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,9 @@ func NewRubberTree(ctx *pulumi.Context,
|
|||
return nil, errors.New("missing one or more required arguments")
|
||||
}
|
||||
|
||||
if args.Farm == nil {
|
||||
args.Farm = pulumi.StringPtr("(unknown)")
|
||||
}
|
||||
var resource RubberTree
|
||||
err := ctx.RegisterResource("plant-provider:tree/v1:RubberTree", name, args, &resource, opts...)
|
||||
if err != nil {
|
||||
|
|
|
@ -50,7 +50,7 @@ export class RubberTree extends pulumi.CustomResource {
|
|||
throw new Error("Missing required property 'type'");
|
||||
}
|
||||
inputs["container"] = args ? args.container : undefined;
|
||||
inputs["farm"] = args ? args.farm : undefined;
|
||||
inputs["farm"] = (args ? args.farm : undefined) || "(unknown)";
|
||||
inputs["type"] = args ? args.type : undefined;
|
||||
} else {
|
||||
inputs["container"] = undefined /*out*/;
|
||||
|
|
|
@ -21,6 +21,8 @@ class ContainerArgs:
|
|||
color: Optional[pulumi.Input[Union['ContainerColor', str]]] = None,
|
||||
material: Optional[pulumi.Input[str]] = None):
|
||||
pulumi.set(__self__, "size", size)
|
||||
if brightness is None:
|
||||
brightness = 1
|
||||
if brightness is not None:
|
||||
pulumi.set(__self__, "brightness", brightness)
|
||||
if color is not None:
|
||||
|
|
|
@ -21,6 +21,8 @@ class Container(dict):
|
|||
color: Optional[str] = None,
|
||||
material: Optional[str] = None):
|
||||
pulumi.set(__self__, "size", size)
|
||||
if brightness is None:
|
||||
brightness = 1
|
||||
if brightness is not None:
|
||||
pulumi.set(__self__, "brightness", brightness)
|
||||
if color is not None:
|
||||
|
|
|
@ -48,6 +48,8 @@ class RubberTree(pulumi.CustomResource):
|
|||
__props__ = dict()
|
||||
|
||||
__props__['container'] = container
|
||||
if farm is None:
|
||||
farm = '(unknown)'
|
||||
__props__['farm'] = farm
|
||||
if type is None and not opts.urn:
|
||||
raise TypeError("Missing required property 'type'")
|
||||
|
|
|
@ -14,7 +14,8 @@
|
|||
"oneOf": [
|
||||
{"$ref": "#/types/plant-provider:tree/v1:Farm"},
|
||||
{"type": "string"}
|
||||
]
|
||||
],
|
||||
"default": "(unknown)"
|
||||
}
|
||||
},
|
||||
"properties": {
|
||||
|
@ -52,7 +53,8 @@
|
|||
]
|
||||
},
|
||||
"brightness": {
|
||||
"$ref": "#/types/plant-provider::ContainerBrightness"
|
||||
"$ref": "#/types/plant-provider::ContainerBrightness",
|
||||
"default": 1.0
|
||||
}
|
||||
},
|
||||
"required": ["size"]
|
||||
|
|
|
@ -1281,6 +1281,21 @@ func bindDefaultValue(value interface{}, spec *DefaultSpec, typ Type) (*DefaultV
|
|||
}
|
||||
|
||||
if value != nil {
|
||||
switch typ := typ.(type) {
|
||||
case *UnionType:
|
||||
if typ.DefaultType != nil {
|
||||
return bindDefaultValue(value, spec, typ.DefaultType)
|
||||
}
|
||||
for _, elementType := range typ.ElementTypes {
|
||||
v, err := bindDefaultValue(value, spec, elementType)
|
||||
if err == nil {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
case *EnumType:
|
||||
return bindDefaultValue(value, spec, typ.ElementType)
|
||||
}
|
||||
|
||||
switch typ {
|
||||
case BoolType:
|
||||
if _, ok := value.(bool); !ok {
|
||||
|
@ -1519,6 +1534,11 @@ func bindTypes(pkg *Package, complexTypes map[string]ComplexTypeSpec, loader Loa
|
|||
typ := &EnumType{Token: token}
|
||||
typs.enums[token] = typ
|
||||
typs.named[token] = typ
|
||||
|
||||
// Bind enums before object types because object type generation depends on enum values to be present.
|
||||
if err := typs.bindEnumTypeDetails(typs.enums[token], token, spec); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to bind type %s", token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1527,16 +1547,12 @@ func bindTypes(pkg *Package, complexTypes map[string]ComplexTypeSpec, loader Loa
|
|||
typs.resources[r.Token] = &ResourceType{Token: r.Token}
|
||||
}
|
||||
|
||||
// Process properties.
|
||||
// Process object types.
|
||||
for token, spec := range complexTypes {
|
||||
if spec.Type == "object" {
|
||||
if err := typs.bindObjectTypeDetails(typs.objects[token], token, spec.ObjectTypeSpec); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to bind type %s", token)
|
||||
}
|
||||
} else if len(spec.Enum) > 0 {
|
||||
if err := typs.bindEnumTypeDetails(typs.enums[token], token, spec); err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to bind type %s", token)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue