Skip to content

Commit

Permalink
Merge #11231
Browse files Browse the repository at this point in the history
11231: Support a logical name for config vars r=aq17 a=iwahbe

Fixes #11230

Co-authored-by: Ian Wahbe <ian@wahbe.com>
  • Loading branch information
bors[bot] and iwahbe committed Nov 2, 2022
2 parents 67f37bd + a601fdb commit 35d2a6e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 15 deletions.
@@ -0,0 +1,4 @@
changes:
- type: feat
scope: programgen/dotnet,go,java,nodejs,python
description: Support a logical name for config vars
10 changes: 7 additions & 3 deletions pkg/codegen/dotnet/gen_program.go
Expand Up @@ -744,17 +744,21 @@ func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
getOrRequire = "Require"
}

name := makeValidIdentifier(v.Name())
if v.DefaultValue != nil {
typ := v.DefaultValue.Type()
if _, ok := typ.(*model.PromiseType); ok {
g.Fgenf(w, "%[1]svar %[2]s = Output.Create(config.%[3]s%[4]s(\"%[2]s\"))", g.Indent, v.Name(), getOrRequire, getType)
g.Fgenf(w, "%svar %s = Output.Create(config.%s%s(\"%s\"))",
g.Indent, name, getOrRequire, getType, v.LogicalName())
} else {
g.Fgenf(w, "%[1]svar %[2]s = config.%[3]s%[4]s(\"%[2]s\")", g.Indent, v.Name(), getOrRequire, getType)
g.Fgenf(w, "%svar %s = config.%s%s(\"%s\")",
g.Indent, name, getOrRequire, getType, v.LogicalName())
}
expr := g.lowerExpression(v.DefaultValue, v.DefaultValue.Type())
g.Fgenf(w, " ?? %.v", expr)
} else {
g.Fgenf(w, "%[1]svar %[2]s = config.%[3]s%[4]s(\"%[2]s\")", g.Indent, v.Name(), getOrRequire, getType)
g.Fgenf(w, "%svar %s = config.%s%s(\"%s\")",
g.Indent, name, getOrRequire, getType, v.LogicalName())
}
g.Fgenf(w, ";\n")
}
Expand Down
21 changes: 11 additions & 10 deletions pkg/codegen/go/gen_program.go
Expand Up @@ -903,16 +903,17 @@ func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
getOrRequire = "Require"
}

name := makeValidIdentifier(v.Name())
if v.DefaultValue == nil {
g.Fgenf(w, "%[1]s := cfg.%[2]s%[3]s(\"%[1]s\")\n", v.Name(), getOrRequire, getType)
g.Fgenf(w, "%s := cfg.%s%s(\"%s\")\n", name, getOrRequire, getType, v.LogicalName())
} else {
expr, temps := g.lowerExpression(v.DefaultValue, v.DefaultValue.Type())
g.genTemps(w, temps)
switch expr := expr.(type) {
case *model.FunctionCallExpression:
switch expr.Name {
case pcl.Invoke:
g.Fgenf(w, "%s, err := %.3v;\n", v.Name(), expr)
g.Fgenf(w, "%s, err := %.3v;\n", name, expr)
g.isErrAssigned = true
g.Fgenf(w, "if err != nil {\n")
g.Fgenf(w, "return err\n")
Expand All @@ -922,24 +923,24 @@ func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
switch v.Type() {
// Go will default to interpreting integers (i.e. 3) as ints, even if the config is Number
case model.NumberType:
g.Fgenf(w, "%s := float64(%.3v);\n", v.Name(), expr)
g.Fgenf(w, "%s := float64(%.3v);\n", name, expr)
default:
g.Fgenf(w, "%s := %.3v;\n", v.Name(), expr)
g.Fgenf(w, "%s := %.3v;\n", name, expr)
}
}
switch v.Type() {
case model.StringType:
g.Fgenf(w, "if param := cfg.Get(\"%s\"); param != \"\"{\n", v.Name())
g.Fgenf(w, "if param := cfg.Get(\"%s\"); param != \"\"{\n", v.LogicalName())
case model.NumberType:
g.Fgenf(w, "if param := cfg.GetFloat64(\"%s\"); param != 0 {\n", v.Name())
g.Fgenf(w, "if param := cfg.GetFloat64(\"%s\"); param != 0 {\n", v.LogicalName())
case model.IntType:
g.Fgenf(w, "if param := cfg.GetInt(\"%s\"); param != 0 {\n", v.Name())
g.Fgenf(w, "if param := cfg.GetInt(\"%s\"); param != 0 {\n", v.LogicalName())
case model.BoolType:
g.Fgenf(w, "if param := cfg.GetBool(\"%s\"); param {\n", v.Name())
g.Fgenf(w, "if param := cfg.GetBool(\"%s\"); param {\n", v.LogicalName())
default:
g.Fgenf(w, "if param := cfg.GetBool(\"%s\"); param != nil {\n", v.Name())
g.Fgenf(w, "if param := cfg.GetBool(\"%s\"); param != nil {\n", v.LogicalName())
}
g.Fgenf(w, "%s = param\n", v.Name())
g.Fgenf(w, "%s = param\n", name)
g.Fgen(w, "}\n")
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/codegen/nodejs/gen_program.go
Expand Up @@ -579,7 +579,9 @@ func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
getOrRequire = "require"
}

g.Fgenf(w, "%[1]sconst %[2]s = config.%[3]s%[4]s(\"%[2]s\")", g.Indent, v.Name(), getOrRequire, getType)
name := makeValidIdentifier(v.Name())
g.Fgenf(w, "%[1]sconst %[2]s = config.%[3]s%[4]s(\"%[5]s\")",
g.Indent, name, getOrRequire, getType, v.LogicalName())
if v.DefaultValue != nil {
g.Fgenf(w, " || %.v", g.lowerExpression(v.DefaultValue, v.DefaultValue.Type()))
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/codegen/pcl/binder_nodes.go
Expand Up @@ -108,6 +108,14 @@ func (b *binder) bindConfigVariable(node *ConfigVariable) hcl.Diagnostics {
diagnostics = append(diagnostics, model.ExprNotConvertible(model.InputType(node.typ), node.DefaultValue))
}
}
if attr, ok := block.Body.Attribute(LogicalNamePropertyKey); ok {
logicalName, lDiags := getStringAttrValue(attr)
if lDiags != nil {
diagnostics = diagnostics.Append(lDiags)
} else {
node.logicalName = logicalName
}
}
node.Definition = block
return diagnostics
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/codegen/pcl/config.go
Expand Up @@ -32,6 +32,11 @@ type ConfigVariable struct {
Definition *model.Block
// The default value for the config variable, if any.
DefaultValue model.Expression

// The name visible to API calls related to the config. Used as the argument when
// fetching config variables. Must not be modified during code generation to ensure
// that valid config calls don't become invalid.
logicalName string
}

// SyntaxNode returns the syntax node associated with the config variable.
Expand All @@ -51,6 +56,13 @@ func (cv *ConfigVariable) Name() string {
return cv.Definition.Labels[0]
}

func (cv *ConfigVariable) LogicalName() string {
if cv.logicalName != "" {
return cv.logicalName
}
return cv.Name()
}

// Type returns the type of the config variable.
func (cv *ConfigVariable) Type() model.Type {
return cv.typ
Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/python/gen_program.go
Expand Up @@ -553,7 +553,7 @@ func (g *generator) genConfigVariable(w io.Writer, v *pcl.ConfigVariable) {
g.genTemps(w, temps)

name := PyName(v.Name())
g.Fgenf(w, "%s%s = config.%s%s(\"%s\")\n", g.Indent, name, getOrRequire, getType, v.Name())
g.Fgenf(w, "%s%s = config.%s%s(\"%s\")\n", g.Indent, name, getOrRequire, getType, v.LogicalName())
if defaultValue != nil {
g.Fgenf(w, "%sif %s is None:\n", g.Indent, name)
g.Indented(func() {
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/python/python_test.go
Expand Up @@ -21,6 +21,7 @@ var pyNameTests = []struct {
{"podCIDRSet", "pod_cidr_set", "pod_cidr_set"},
{"Sha256Hash", "sha256_hash", "sha256_hash"},
{"SHA256Hash", "sha256_hash", "sha256_hash"},
{"proj:config", "proj_config", "proj_config"},

// PyName should return the legacy name for these:
{"openXJsonSerDe", "open_x_json_ser_de", "open_x_json_ser_de"},
Expand Down

0 comments on commit 35d2a6e

Please sign in to comment.