Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 27 additions & 2 deletions pkg/generate/config/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@
package config

import (
"encoding/json"
Copy link
Collaborator

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.

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.


awssdkmodel "github.com/aws/aws-sdk-go/private/model/api"
Copy link
Member

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"
)

Copy link
Contributor Author

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 ?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you!


"github.com/aws-controllers-k8s/code-generator/pkg/util"
"github.com/aws-controllers-k8s/code-generator/pkg/util"
)

// StringArray is a type that can be represented in JSON as *either* a string
// *or* an array of strings
type StringArray []string

// OperationConfig represents instructions to the ACK code generator to
// specify the overriding values for API operation parameters and its custom implementation.
type OperationConfig struct {
Expand All @@ -37,7 +43,8 @@ type OperationConfig struct {
ResourceName string `json:"resource_name"`
// 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"`
OperationType StringArray `json:"operation_type"`
// PrimaryIdentifierFieldName provides the name of the field that should be
// interpreted as the "primary" identifier field. This field will be used as
// the primary field for resource adoption.
Expand Down Expand Up @@ -75,3 +82,21 @@ func (c *Config) ListOpMatchFieldNames(
}
return rConfig.ListOperation.MatchFields
}

// UnmarshalJSON parses input for a either a string or
// or a list and returns a StringArray.
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
}
38 changes: 24 additions & 14 deletions pkg/model/sdk_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package model

import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -27,6 +28,7 @@ import (
ackgenconfig "github.com/aws-controllers-k8s/code-generator/pkg/generate/config"
"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"
Copy link
Member

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

Copy link
Contributor Author

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.

)

Expand Down Expand Up @@ -206,11 +208,13 @@ func (a *SDKAPI) GetOperationMap(cfg *ackgenconfig.Config) *OperationMap {
// create an index of Operations by operation types and resource name
opMap := OperationMap{}
for opID, op := range a.API.Operations {
opType, resName := getOpTypeAndResourceName(opID, cfg)
if _, found := opMap[opType]; !found {
opMap[opType] = map[string]*awssdkmodel.Operation{}
opTypeArray, resName := getOpTypeAndResourceName(opID, cfg)
for _, opType := range opTypeArray {
if _, found := opMap[opType]; !found {
opMap[opType] = map[string]*awssdkmodel.Operation{}
}
opMap[opType][resName] = op
}
opMap[opType][resName] = op
}
a.opMap = &opMap
return &opMap
Expand Down Expand Up @@ -382,20 +386,26 @@ func (a *SDKAPI) SDKAPIInterfaceTypeName() string {
}

// Override the operation type and/or resource name if specified in config
func getOpTypeAndResourceName(opID string, cfg *ackgenconfig.Config) (OpType, string) {
func getOpTypeAndResourceName(opID string, cfg *ackgenconfig.Config) ([]OpType, string) {
var unmarshaledOpTypes ackgenconfig.StringArray
opType, resName := GetOpTypeAndResourceNameFromOpID(opID)
opTypes := []OpType{opType}

if cfg != nil {
if operationConfig, exists := cfg.Operations[opID]; exists {
if operationConfig.OperationType != "" {
opType = OpTypeFromString(operationConfig.OperationType)
}
if cfg == nil {
return opTypes, resName
}
if operationConfig, exists := cfg.Operations[opID]; exists {
if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
}

if operationConfig.ResourceName != "" {
resName = operationConfig.ResourceName
if b, err := json.Marshal(operationConfig.OperationType); err == nil {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep you are right :)

Copy link
Contributor Author

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 ?

Copy link
Member

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!

json.Unmarshal(b, &unmarshaledOpTypes)
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not applicable now.

for _, operationType := range unmarshaledOpTypes {
opType = OpTypeFromString(operationType)
opTypes = append(opTypes, opType)
}
}
}

return opType, resName
return opTypes, resName
}