Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat object parameters as objects in templating #3225

Merged
merged 5 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,6 @@ and we will add you. **All** contributors belong here. 💯
- [guangwu guo](https://github.com/testwill)
- [Eric Herrmann](https://github.com/egherrmann)
- [Alex Dejanu](https://github.com/dejanu)
- [Leo Bergnéhr](https://github.com/lbergnehr)


41 changes: 35 additions & 6 deletions pkg/runtime/runtime_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"compress/gzip"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -131,15 +132,40 @@ func (m *RuntimeManifest) loadDependencyDefinitions() error {
return nil
}

func (m *RuntimeManifest) resolveParameter(pd manifest.ParameterDefinition) string {
// This wrapper type serve as a way of formatting a `map[string]interface{}` as
// JSON when the templating by mustache is done. It makes it possible to
// maintain the JSON string representation of the map while still allowing the
// map to be used as a context in the templating, allowing for accessing the
// map's keys in the template.
type FormattedObject map[string]interface{}

// Format the `FormattedObject` as a JSON string.
func (fo FormattedObject) Format(f fmt.State, c rune) {
jsonStr, _ := json.Marshal(fo)
fmt.Fprintf(f, string(jsonStr))
}

func (m *RuntimeManifest) resolveParameter(pd manifest.ParameterDefinition) (interface{}, error) {
getValue := func(envVar string) (interface{}, error) {
value := m.config.Getenv(envVar)
if pd.Type == "object" {
var obj map[string]interface{}
if err := json.Unmarshal([]byte(value), &obj); err != nil {
return nil, err
}
return FormattedObject(obj), nil
}
return value, nil
}

if pd.Destination.EnvironmentVariable != "" {
return m.config.Getenv(pd.Destination.EnvironmentVariable)
return getValue(pd.Destination.EnvironmentVariable)
}
if pd.Destination.Path != "" {
return pd.Destination.Path
return pd.Destination.Path, nil
}
envVar := manifest.ParamToEnvVar(pd.Name)
return m.config.Getenv(envVar)
return getValue(envVar)
}

func (m *RuntimeManifest) resolveCredential(cd manifest.CredentialDefinition) (string, error) {
Expand Down Expand Up @@ -271,9 +297,12 @@ func (m *RuntimeManifest) buildSourceData() (map[string]interface{}, error) {
}

pe := param.Name
val := m.resolveParameter(param)
val, err := m.resolveParameter(param)
if err != nil {
return nil, err
}
if param.Sensitive {
m.setSensitiveValue(val)
m.setSensitiveValue(val.(string))
kichristensen marked this conversation as resolved.
Show resolved Hide resolved
}
params[pe] = val
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/runtime/runtime_manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,22 @@ func TestResolveMapParam(t *testing.T) {
ctx := context.Background()
testConfig := config.NewTestConfig(t)
testConfig.Setenv("PERSON", "Ralpha")
testConfig.Setenv("CONTACT", "{ \"name\": \"Breta\" }")

mContent := `schemaVersion: 1.0.0-alpha.2
parameters:
- name: person
- name: place
applyTo: [install]
- name: contact
type: object

install:
- mymixin:
Parameters:
Thing: ${ bundle.parameters.person }
ObjectName: ${ bundle.parameters.contact.name }
Object: '${ bundle.parameters.contact }'
`
rm := runtimeManifestFromStepYaml(t, testConfig, mContent)
s := rm.Install[0]
Expand All @@ -62,6 +67,19 @@ install:
assert.Equal(t, "Ralpha", val)
assert.NotContains(t, "place", pms, "parameters that don't apply to the current action should not be resolved")

// Asserting `bundle.parameters.contact.name` works.
require.IsType(t, "string", pms["ObjectName"], "Data.mymixin.Parameters.ObjectName has incorrect type")
contactName := pms["ObjectName"].(string)
require.IsType(t, "string", contactName, "Data.mymixin.Parameters.ObjectName.name has incorrect type")
assert.Equal(t, "Breta", contactName)

// Asserting `bundle.parameters.contact` evaluates to the JSON string
// representation of the object.
require.IsType(t, "string", pms["Object"], "Data.mymixin.Parameters.Object has incorrect type")
contact := pms["Object"].(string)
require.IsType(t, "string", contact, "Data.mymixin.Parameters.Object has incorrect type")
assert.Equal(t, "{\"name\":\"Breta\"}", contact)

err = rm.Initialize(ctx)
require.NoError(t, err)
}
Expand Down