-
Notifications
You must be signed in to change notification settings - Fork 226
Allow OperatorType to be a list such that one API can map to multiple operations #166
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
Conversation
Hi @mbaijal. Thanks for your PR. I'm waiting for a aws-controllers-k8s member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. 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. |
pkg/model/sdk_helper.go
Outdated
} | ||
|
||
return opType, resName | ||
if len(operationConfig.OperationType) > 0 { |
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.
clean up
c7357bd
to
630b228
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.
👍
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.
@mbaijal see inline for a helpful hint at solving the "need to update all the existing generator.yaml files" problem.
pkg/generate/config/operation.go
Outdated
// Override for operation type in case of heuristic failure | ||
// An example of this is `Put...` or `Register...` API operations not being correctly classified as `Create` op type | ||
OperationType string `json:"operation_type"` | ||
OperationType []string `json:"operation_type"` |
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'll give you a little Go code I wrote for a different project that will allow you to change the type of this field to an array of strings without requiring all the existing generator.yaml
files to be updated.
// NOTE(jaypipes): A type that can be represented in JSON as *either* a string
// *or* an array of strings, which is what JSONSchema's type field needs.
// see: https://github.com/go-yaml/yaml/issues/100
type StringArray []string
func (a *StringArray) UnmarshalJSON(b []byte) error {
var multi []string
err := json.Unmarshal(b, &multi)
if err != nil {
var single string
err := json.Unmarshal(b, &single)
if err != nil {
return err
}
*a = []string{single}
} else {
*a = multi
}
return nil
}
You'd then change the above field definition to this instead:
OperationType StringArray `json:"operation_type"`
The existing generator.yaml
files that specify operation_type
as a string will work as-is without modification and the generator.yaml
files that need to specify multiple operation types can specify a list of strings.
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 looks good, I'll update the PR later today, thanks !
630b228
to
335a6d0
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.
Couple tiny nits inline, @mbaijal, otherwise this looks great.
import ( | ||
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api" | ||
|
||
"encoding/json" |
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 is a standard library import, so please place above the awssdkmodel
import line and separate with an empty newline.
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.
pkg/model/sdk_helper.go
Outdated
"path/filepath" | ||
"sort" | ||
"strings" | ||
"encoding/json" |
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.
Please run go fmt
on your code or add an auto-formatter to your IDE. It will automatically correct the above into an alphabetical import order.
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.
add an auto-formatter to your IDE
I should really, always forget to run the formatter. Ran it manually on the two files for now.
335a6d0
to
323a563
Compare
|
||
"encoding/json" | ||
"github.com/aws-controllers-k8s/code-generator/pkg/util" | ||
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api" |
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.
please change the import section to:
import (
"encoding/json"
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
"github.com/aws-controllers-k8s/code-generator/pkg/util"
)
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.
Sure. gofmt
does not fix this. Would you recommend using goimports
or such to avoid such issues in the future ?
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.
Yes exactly :). Sometimes goimports
doesn't work as expected so you might need to fix the imports order manually. Here are the guidelines for imports https://github.com/golang/go/wiki/CodeReviewComments#imports
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.
Thank you!
pkg/model/sdk_helper.go
Outdated
if operationConfig.ResourceName != "" { | ||
resName = operationConfig.ResourceName | ||
if b, err := json.Marshal(operationConfig.OperationType); err == nil { | ||
unmarshaledOpTypes.UnmarshalJSON(b) |
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.
You don't have to call the method UnmarshalJSON
here, bur rather call json.Unmarshal(b, &unmarshaledOpTypes)
323a563
to
16781ee
Compare
pkg/model/sdk_helper.go
Outdated
if operationConfig.ResourceName != "" { | ||
resName = operationConfig.ResourceName | ||
if b, err := json.Marshal(operationConfig.OperationType); err == nil { | ||
json.Unmarshal(b, &unmarshaledOpTypes) |
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 Unmarshal
error should be handled here
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.
Not applicable now.
pkg/model/sdk_helper.go
Outdated
|
||
if operationConfig.ResourceName != "" { | ||
resName = operationConfig.ResourceName | ||
if b, err := json.Marshal(operationConfig.OperationType); err == nil { |
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.
Thinking a second time about this, i think that you don't have to call json.Marshal
/json.Unmarshall
here - because StringArray.UnmarshalJSON
will be called when model.Model
is getting initialized https://github.com/aws-controllers-k8s/code-generator/blob/main/pkg/model/model.go#L700
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.
Yep you are right :)
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.
In this case, is operation.go the right place for the UnmarshalJSON method ?
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.
Yes it makes sense to have the type and it methods on the same file so it's good!
/test unit-tests |
@mbaijal: Cannot trigger testing until a trusted user reviews the PR and leaves an 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. |
/ok-to-test |
3e23b12
to
d76c07f
Compare
"github.com/aws-controllers-k8s/code-generator/pkg/names" | ||
"github.com/aws-controllers-k8s/code-generator/pkg/util" | ||
|
||
awssdkmodel "github.com/aws/aws-sdk-go/private/model/api" |
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.
Tiny nit: this import should be in the second block of imports
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.
let me set up the goimports and create a tiny PR for this one.
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.
Looks good! thanks @mbaijal!
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: A-Hilaly, mbaijal 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 |
) Issue #, if available: aws-controllers-k8s/community#867 Description of changes: 1. Enable updates based on changes to code generator made in this PR - aws-controllers-k8s/code-generator#166 2. Add an integration test to check for updates By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
Allow OperatorType to be a list such that one API can map to multiple operations.
This is required in certain cases where the same API call maps to both Create and Update operations such as
RegisterScalableTarget
andPutScalingPolicy
Link to ApplicationAutoscaling PR with the related change
Note: This change will require all other services to update their generator configs as well.
Issue #, if available:
aws-controllers-k8s/community#867
Description of changes:
The
getOpTypeAndResourceName
method handles each of the following cases same as before in addition to the new case 5-Testing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.