Skip to content
Merged
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
22 changes: 19 additions & 3 deletions pkg/cmd/kafka/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/AlecAivazis/survey/v2"
kafkamgmtclient "github.com/redhat-developer/app-services-sdk-go/kafkamgmt/apiv1/client"
Expand Down Expand Up @@ -281,6 +282,7 @@ func promptConfirmUpdate(opts *options) (bool, error) {
// creates a summary of what values will be changed in this update
// returns a formatted string. Example:
// owner: foo_user ➡️ bar_user
// to do: Needs update once reauthentication_enabled is incorporated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How reauthentication_enabled affects this preview?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods generating preview iterate through the entire update struct, the broken preview did display reauthentication_enabled too.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That iteration thru all elements of struct is core issue IMHO

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to filter out the nil properties of the update object.

func generateUpdateSummary(new reflect.Value, current reflect.Value) string {
var summary string

Expand All @@ -291,10 +293,14 @@ func generateUpdateSummary(new reflect.Value, current reflect.Value) string {
fieldTag := field.Tag.Get("json")
fieldName := field.Name

oldVal := getElementValue(current.FieldByName(fieldName)).String()
newVal := getElementValue(new.FieldByName(fieldName)).String()
oldVal := getElementValue(current.FieldByName(fieldName))
newVal := getUpdateObjValue(new.FieldByName(fieldName))

summary += fmt.Sprintf("%v: %v %v %v\n", color.Bold(fieldTag), oldVal, icon.Emoji("\u27A1", "=>"), newVal)
fieldTagDisp := strings.Split(fieldTag, ",")[0]

if newVal != reflect.ValueOf(nil) {
summary += fmt.Sprintf("%v: %v %v %v\n", color.Bold(fieldTagDisp), oldVal, icon.Emoji("\u27A1", "=>"), newVal)
}
}

return summary
Expand All @@ -308,3 +314,13 @@ func getElementValue(v reflect.Value) reflect.Value {
}
return v
}

// get the true value from a reflect.Value for KafkaUpdateRequest struct
// to do: Needs update once reauthentication_enabled is incorporated
func getUpdateObjValue(v reflect.Value) reflect.Value {
if v.Kind() == reflect.Struct {
vstruct := v.Field(0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better waywould be to get the value field instead of index. Do we need to iterate through this struct as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should work:

Suggested change
vstruct := v.Field(0)
vstruct := v.FieldByName("value")

This should suffice, no need to iterate.

Though I'm starting to think going forward with reflection on this is a bad idea, as we are making assumptions that are tightly coupled with the SDK, which could introduce breaking changes in the future by changing the name from "value" and since this is not strongly typed, we would not catch it..

For now, once you update with the suggestion above, we can merge as it is blocking any releases. Could you create a follow up issue to address this? We can simplify it by operating directly on the SDK model properties instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!
Ready for review.

return vstruct.Elem()
}
return v
}