Parse string arrays from env in Go first-class provider (#5744)

This commit is contained in:
Mikhail Shilkov 2020-11-13 10:59:41 +01:00 committed by GitHub
parent 9d6e380680
commit 66c5cdb7a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -680,6 +680,10 @@ func (pkg *pkgContext) getDefaultValue(dv *schema.DefaultValue, t schema.Type) (
pkg.needsUtils = true
parser, typDefault, typ := "nil", "\"\"", "string"
switch t.(type) {
case *schema.ArrayType:
parser, typDefault, typ = "parseEnvStringArray", "pulumi.StringArray{}", "pulumi.StringArray"
}
switch t {
case schema.BoolType:
parser, typDefault, typ = "parseEnvBool", "false", "bool"
@ -1434,7 +1438,8 @@ func GeneratePackage(tool string, pkg *schema.Package) (map[string][]byte, error
// Utilities
if pkg.needsUtils {
buffer := &bytes.Buffer{}
pkg.genHeader(buffer, []string{"os", "strconv"}, nil)
imports := newStringSet("github.com/pulumi/pulumi/sdk/v2/go/pulumi")
pkg.genHeader(buffer, []string{"os", "strconv", "strings"}, imports)
fmt.Fprintf(buffer, "%s", utilitiesFile)
@ -1477,6 +1482,14 @@ func parseEnvFloat(v string) interface{} {
return f
}
func parseEnvStringArray(v string) interface{} {
var result pulumi.StringArray
for _, item := range strings.Split(v, ";") {
result = append(result, pulumi.String(item))
}
return result
}
func getEnvOrDefault(def interface{}, parser envParser, vars ...string) interface{} {
for _, v := range vars {
if value := os.Getenv(v); value != "" {