Skip to content

Commit

Permalink
feat: preserve existing alt defaults in display yaml. (#2613)
Browse files Browse the repository at this point in the history
Co-authored-by: Zheng Qin <[email protected]>
Co-authored-by: Bharath KKB <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2024
1 parent 851c946 commit b22c3bb
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cli/bpmetadata/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ func CreateBlueprintDisplayMetadata(bpPath string, bpDisp, bpCore *BlueprintMeta
bpDisp.Spec.Info.Source = bpCore.Spec.Info.Source
buildUIInputFromVariables(bpCore.Spec.Interfaces.Variables, bpDisp.Spec.Ui.Input)

existingInput := proto.Clone(bpCore.Spec.Ui.Input).(*BlueprintUIInput)
// Merge existing data (if any) into the newly generated UI Input
mergeExistingAltDefaults(bpDisp.Spec.Ui.Input, existingInput)

return bpDisp, nil
}

Expand Down
16 changes: 16 additions & 0 deletions cli/bpmetadata/display.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ func createTitleFromName(name string) string {

return strings.Join(titleSplit, " ")
}

// mergeExistingAltDefaults merges existing alt_defaults from an old BlueprintUIInput into a new one,
// preserving manually authored alt_defaults.
func mergeExistingAltDefaults(newInput, existingInput *BlueprintUIInput) {
if existingInput == nil {
return // Nothing to merge if existingInput is nil
}

for i, variable := range newInput.Variables {
for _, existingVariable := range existingInput.Variables {
if variable.Name == existingVariable.Name && existingVariable.AltDefaults != nil {
newInput.Variables[i].AltDefaults = existingVariable.AltDefaults
}
}
}
}
94 changes: 94 additions & 0 deletions cli/bpmetadata/display_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/structpb"
)

func TestUIInputFromVariables(t *testing.T) {
Expand Down Expand Up @@ -128,3 +129,96 @@ func TestCreateTitleFromName(t *testing.T) {
})
}
}

func TestMergeExistingAltDefaults(t *testing.T) {
tests := []struct {
name string
newInput *BlueprintUIInput
existingInput *BlueprintUIInput
expectInput *BlueprintUIInput
}{
{
name: "Merge alt default into UI input",
newInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
},
},
},
existingInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
AltDefaults: []*DisplayVariable_AlternateDefault{
{
Type: 0,
Value: structpb.NewStringValue("alt_default_value"),
},
},
},
},
},
expectInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
AltDefaults: []*DisplayVariable_AlternateDefault{
{
Type: 0,
Value: structpb.NewStringValue("alt_default_value"),
},
},
},
},
},
},
{
name: "No existing input",
newInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
},
},
},
existingInput: nil,
expectInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
},
},
},
},
{
name: "Empty new input",
newInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{},
},
existingInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{
"test_var_1": {
Name: "test_var_1",
AltDefaults: []*DisplayVariable_AlternateDefault{
{
Type: 0,
Value: structpb.NewStringValue("alt_default_value"),
},
},
},
},
},
expectInput: &BlueprintUIInput{
Variables: map[string]*DisplayVariable{},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergeExistingAltDefaults(tt.newInput, tt.existingInput)
assert.Equal(t, tt.newInput, tt.expectInput)
})
}
}

0 comments on commit b22c3bb

Please sign in to comment.