[codegen/python] - Fix required vs default values in provider config (#7536)

This commit is contained in:
Komal 2021-07-16 09:48:03 -07:00 committed by GitHub
parent 55ed50f87a
commit 01321187a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 27 deletions

View file

@ -17,13 +17,13 @@ favoriteSandwich: Optional[str]
omg my favorite sandwich
"""
isMember: Optional[str]
isMember: bool
kids: Optional[str]
name: str
name: Optional[str]
numberOfSheep: Optional[str]
numberOfSheep: Optional[int]
secretCode: Optional[str]
"""

View file

@ -28,20 +28,20 @@ class _ExportableConfig(types.ModuleType):
return __config__.get('favoriteSandwich')
@property
def is_member(self) -> Optional[str]:
return __config__.get('isMember') or True
def is_member(self) -> bool:
return __config__.get_bool('isMember') or True
@property
def kids(self) -> Optional[str]:
return __config__.get('kids')
@property
def name(self) -> str:
def name(self) -> Optional[str]:
return __config__.get('name')
@property
def number_of_sheep(self) -> Optional[str]:
return __config__.get('numberOfSheep')
def number_of_sheep(self) -> Optional[int]:
return __config__.get_int('numberOfSheep')
@property
def secret_code(self) -> Optional[str]:

View file

@ -765,23 +765,12 @@ func (mod *modContext) genConfig(variables []*schema.Property) (string, error) {
// Emit an entry for all config variables.
for _, p := range variables {
configFetch := fmt.Sprintf("__config__.get('%s')", p.Name)
if p.DefaultValue != nil {
v, err := getDefaultValue(p.DefaultValue, codegen.UnwrapType(p.Type))
if err != nil {
return "", err
}
configFetch += " or " + v
}
// For historical reasons and to maintain backwards compatibility, the config variables for python
// are always typed as `Optional[str`] or `str` since the getters only use config.get().
// To return the rich objects would be a breaking change, tracked in https://github.com/pulumi/pulumi/issues/7493
typeString := "Optional[str]"
if p.IsRequired() {
typeString = "str"
configFetch, err := genConfigFetch(p)
if err != nil {
return "", err
}
typeString := genConfigVarType(p)
fmt.Fprintf(w, "%s@property\n", indent)
fmt.Fprintf(w, "%sdef %s(self) -> %s:\n", indent, PyName(p.Name), typeString)
dblIndent := strings.Repeat(indent, 2)
@ -794,6 +783,49 @@ func (mod *modContext) genConfig(variables []*schema.Property) (string, error) {
return w.String(), nil
}
func genConfigFetch(configVar *schema.Property) (string, error) {
getFunc := "get"
unwrappedType := codegen.UnwrapType(configVar.Type)
switch unwrappedType {
case schema.BoolType:
getFunc = "get_bool"
case schema.IntType:
getFunc = "get_int"
case schema.NumberType:
getFunc = "get_float"
}
configFetch := fmt.Sprintf("__config__.%s('%s')", getFunc, configVar.Name)
if configVar.DefaultValue != nil {
v, err := getDefaultValue(configVar.DefaultValue, unwrappedType)
if err != nil {
return "", err
}
configFetch += " or " + v
}
return configFetch, nil
}
func genConfigVarType(configVar *schema.Property) string {
// For historical reasons and to maintain backwards compatibility, the config variables for python
// are typed as `Optional[str`] or `str` for complex objects since the getters only use config.get().
// To return the rich objects would be a breaking change, tracked in https://github.com/pulumi/pulumi/issues/7493
typeString := "str"
switch codegen.UnwrapType(configVar.Type) {
case schema.BoolType:
typeString = "bool"
case schema.IntType:
typeString = "int"
case schema.NumberType:
typeString = "float"
}
if configVar.DefaultValue == nil || configVar.DefaultValue.Value == nil {
typeString = "Optional[" + typeString + "]"
}
return typeString
}
// genConfigStubs emits all type information for the config variables in the given module, returning the resulting file.
// We do this because we lose IDE autocomplete by implementing the dynamic config getters described in genConfig.
// Emitting these stubs allows us to maintain type hints and autocomplete for users.
@ -807,10 +839,7 @@ func (mod *modContext) genConfigStubs(variables []*schema.Property) (string, err
// Emit an entry for all config variables.
for _, p := range variables {
typeString := "Optional[str]"
if p.IsRequired() {
typeString = "str"
}
typeString := genConfigVarType(p)
fmt.Fprintf(w, "%s: %s\n", p.Name, typeString)
printComment(w, p.Comment, "")
fmt.Fprintf(w, "\n")