Skip to content

chore: Support nested attributes in conversion logic for generating Resource Model from API response #3261

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

Merged

Conversation

lantoli
Copy link
Member

@lantoli lantoli commented Apr 8, 2025

Description

Support nested attributes in conversion logic for generating Resource Model from API response. It includes ListNest, SetNested and SingleNested. MapNested is not supported as it's not needed at the moment.

Link to any related issue(s): CLOUDP-311710

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR. A migration guide must be created or updated if the new feature will go in a major version.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR. A migration guide must be created or updated.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contributing guides
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals I have added appropriate changelog entries.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@lantoli lantoli marked this pull request as ready for review April 11, 2025 18:28
@lantoli lantoli requested a review from a team as a code owner April 11, 2025 18:28
Copy link
Member

@AgustinBettati AgustinBettati left a comment

Choose a reason for hiding this comment

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

LGTM, leaving some questions and suggestions

"JSON arrays not supported yet": {
responseJSON: `{"attr": [{"key": "value"}]}`,
errorStr: "unmarshal not supported yet for type []interface {} for field attr",
"JSON maps not supported yet": {
Copy link
Member

Choose a reason for hiding this comment

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

nice that we capture current limitation with a test

}
}

func convertUnknownToNull(valModel reflect.Value) {
Copy link
Member

Choose a reason for hiding this comment

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

should this logic be recursive?

Copy link
Member Author

@lantoli lantoli Apr 14, 2025

Choose a reason for hiding this comment

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

unknown to null logic moved to another PR

@@ -164,26 +173,211 @@ func unmarshalAttr(attrNameJSON string, attrObjJSON any, valModel reflect.Value)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it worth it to log a debug message?

Copy link
Member Author

Choose a reason for hiding this comment

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

not sure, in general I prefer not to log debug messages, I don't know if this case is so important to be logged

AttrSetString: types.SetUnknown(types.StringType),
AttrSetObj: types.SetUnknown(objTypeTest),
}
// attrUnexisting is ignored because it is in JSON but not in the model, no error is returned
Copy link
Collaborator

Choose a reason for hiding this comment

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

nice with these comments

func convertUnknownToNull(valModel reflect.Value) {
for i := 0; i < valModel.NumField(); i++ {
field := valModel.Field(i)
if obj, ok := field.Interface().(types.Object); ok {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would only work for types.Object? What about other types.XXX?

Copy link
Member Author

Choose a reason for hiding this comment

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

you're right, creating ticket created for to null logic: CLOUDP-312809

return err
}
}
convertUnknownToNull(valModel)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this would be better to do in a different step?
Then it would be easier to override and possible use values from state instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

@EspenAlbert do you mean not doing it in Unmarshal, but in another public func?

Copy link
Member Author

Choose a reason for hiding this comment

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

@EspenAlbert ticket created for to null logic: CLOUDP-312809 cc @AgustinBettati

Copy link
Collaborator

Choose a reason for hiding this comment

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

but in another public func

Yes, I believe so. Sounds good to me with a different ticket 👍

Copy link
Collaborator

@EspenAlbert EspenAlbert left a comment

Choose a reason for hiding this comment

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

Nice. Thanks for addressing the comments

@lantoli lantoli merged commit f2817f7 into CLOUDP-301808-poc-autogeneration Apr 14, 2025
40 checks passed
@lantoli lantoli deleted the CLOUDP-311710_unmarshal_nested branch April 14, 2025 14:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants