-
Notifications
You must be signed in to change notification settings - Fork 190
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
Support of injecting fields in existing structures: CustomNestedFields #462
Conversation
f8c6705
to
014b256
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Vandita2020 This is great work, well done!
I left a few comments to improve the code and make it more similar to what we have in the other packages.
.vscode/settings.json
Outdated
{ | ||
"go.buildTags": "codegen", | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not commit this please. Maybe let's add .vscode/settings.json
to .gitignore
file ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd even recommend adding .vscode
to a global gitignore on your system. Otherwise every repo needs to add every IDE's config paths (.vscode
, .idea
, etc.) individually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks!
pkg/model/model.go
Outdated
func CheckIfFieldExists( | ||
field map[string]*Field, | ||
currValue string, | ||
) bool { | ||
_, ok := field[currValue] | ||
if ok { | ||
return true | ||
} | ||
return false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmmmm i think this is unecessary.. it will always fall down to:
if _, ok := fieldsInParent[parent]; ok {
// Check if the field is of type structure
previous, res = CheckType(value, fieldsInParent)
if res != true {
fmt.Printf("Cannot add new field here, %s is not of type Structure", parentName)
break
}
}
pkg/model/model.go
Outdated
return false | ||
} | ||
|
||
func CheckType( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment here, i don't think we need this function.. or the least it should be named isShapeStructure
.. and still not very convinced of moving this logic to a function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have removed the function and added the logic to the calling function
pkg/model/model.go
Outdated
type Nested struct { | ||
arr []string | ||
Config *ackgenconfig.FieldConfig | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i know this is working. Pretty sure we can find a better way to represent this data.. (i think we are copying a lot arr
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't actually get what you mean by copying arr
data a lot. It is just defined once to store the names of nested fields and is then iterated over to check the nested fields type and whether they are a part of their parent field. But yeah I'm open to looking for a better way to represent the data.
pkg/model/model.go
Outdated
|
||
for _, toInjectNestedFields := range alltoInjectNestedFields { | ||
|
||
addThis := "" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need a better name than addThis
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed the name to newCustomField
. Let me know if that sounds okay.
pkg/model/model.go
Outdated
var previous *Field | ||
var res bool | ||
|
||
len := len(toInjectNestedFields.arr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
len
is a Go keyword, i suggest just to use len(toInjectNestedFields.arr)
whenever you need to.
assert.Contains(codeField.MemberFields, "S3SHA256") | ||
assert.Contains(codeField.ShapeRef.Shape.MemberRefs, "S3SHA256") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good testing!
pkg/model/model.go
Outdated
f := NewField(crd, fPath, memberNames, shapeRef, fConfig) | ||
crd.Fields[fPath] = f | ||
|
||
previous.MemberFields[addThis] = f |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
pkg/model/model.go
Outdated
// first element is a field of CRD | ||
fieldsInCRD := crd.SpecFields |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could have been declared outside, it's a non necessary operation to do at every loop (same for few variables below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a relation between fieldsInParent
and previous
variable, which changes with every loop. I can declare the fieldsInCRD
outside the loop, but that is the only variable that can be defined outside the loop. To maintain the code readability I declared it the same way as fieldsInParent
.
pkg/model/model.go
Outdated
if CheckIfFieldExists(fieldsInCRD, value) { | ||
// Check if the field is of type structure | ||
previous, res = CheckType(value, fieldsInCRD) | ||
if res != true { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if res != true { | |
if !res { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole part is removed now
014b256
to
88be69e
Compare
/test apigatewayv2-controller-test |
88be69e
to
285eba5
Compare
@Vandita2020: The
The following commands are available to trigger optional jobs:
Use In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/test all |
/test dynamodb-controller-test |
1 similar comment
/test dynamodb-controller-test |
/test all |
/test iam-controller-test |
285eba5
to
84e7b8e
Compare
84e7b8e
to
9959155
Compare
pkg/model/crd.go
Outdated
// inject a field called "Password" into the "User" struct, the field path | ||
// would be "User.Password". The field path can be as deep as needed. | ||
func (crd *CRD) addCustomNestedFields(customNestedFields map[string]*ackgenconfig.FieldConfig) { | ||
// Now that we collected all the nested fields, we can process, validate |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does "now" mean here? The message doesn't seem relevant in the beginning of a function.
pkg/model/crd.go
Outdated
topLevelField := fieldParts[0] | ||
|
||
f, ok := crd.checkSpecOrStatus(topLevelField) | ||
// f, ok := crd.SpecFields[topLevelField] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove commented out code
pkg/model/crd.go
Outdated
// and inject them into the Spec or Status struct. | ||
for customNestedField, customNestedFieldConfig := range customNestedFields { | ||
fieldParts := strings.Split(customNestedField, ".") | ||
// we now than the length of fieldParts is at least 2 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we know that the length
pkg/model/crd.go
Outdated
// else { | ||
// f, ok = crd.SpecFields[topLevelField] | ||
// if ok && f.ShapeRef.Shape.Type != "structure" { | ||
// // We need to panic here because the user is providing wrong configuration. | ||
// msg := fmt.Sprintf("Expected parent field to be of type structure, but found %s", f.ShapeRef.Shape.Type) | ||
// panic(msg) | ||
// } | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove commented code
pkg/model/model.go
Outdated
// A list of fields that should be processed after building the gathering | ||
// the Spec and Status top level fields. The customNestedFields will be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the phrase "after building the gathering the Spec and Status" feels wrong. I think there's an extra word that shouldn't be there.
9959155
to
960ad45
Compare
/retest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! Thanks a lot for your contribution @Vandita2020 . This will be beneficial to all the ACK controllers and it's community.
/approve
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: a-hilaly, Vandita2020 The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/hold |
/unhold |
**Issue** This PR is related to fix the issue generating after executing PR #462 **Summary** To disregard fields marked with `set: ignore` for nested fields. This PR also enhances the functionality of `set:ignore` to support the exclusion of fields from `set_sdk`. **Description** The PR fixes the issue coming after a new custom field is created and has `set.ignore` set to `true` for it. Even after setting the field to ignore, the field is getting used to set the SDK fields. In this case particularly, the new field was defined as shown below: ``` Code.S3SHA256: type: string set: - method: Create ignore: true ``` And the issue it is causing is under `sdk.go/newCreateRequestPayload` function, where it is being set, as shown below: ``` if r.ko.Spec.Code.S3SHA256 != nil { f1.SetS3SHA256(*r.ko.Spec.Code.S3SHA256) } ``` The goal is to ignore this particular line of code. And also to improve the basic functionality of `set.ignore`. The `set.ignore` functionality was till now supporting exclusion of fields only when the field was used to set for the resource. This PR checks the `set.ignore` for field and ignores the following code that creates request payload if it is set to `true`. **Limitations** Currently this PR checks the `set.ignore` field only for members of the field whose type is `structure`. **Acknowledgment** By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Issue #
This issue arises from the code changes in lambda-controller for PR #88
Summary
Support of injecting fields in existing structures: CustomNestedFields
Description
The code in this PR helps in accepting a custom nestedField defined using
type
as a part of its parentField. In the issue mentioned above when we inject a new fieldS3SHA256
intoCode
struct, instead of nesting it underCode
the generator created a separate field namedCodeS3SHA256
.The new field
S3SHA256
is defined like this ingenerator.yaml
file:After applying the changes in this PR, the field
S3SHA256
is properly injected as a part ofCode
struct.This PR contains code implementation to inject Nested fields into the Spec or Status struct and has respective model-test to validate the changes.
Acknowledgment
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.