Skip to content

Commit

Permalink
adds suggested changes to use apt utility and fix validate shape name…
Browse files Browse the repository at this point in the history
… method
  • Loading branch information
skotambkar committed Dec 11, 2019
1 parent ef3a90c commit f061c17
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 58 deletions.
8 changes: 4 additions & 4 deletions aws/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"github.com/aws/aws-sdk-go-v2/aws/defaults"
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
types2 "github.com/aws/aws-sdk-go-v2/service/sqs/types"
sqsTypes "github.com/aws/aws-sdk-go-v2/service/sqs/types"
)

func ExampleEndpointResolverFunc() {
Expand All @@ -35,7 +35,7 @@ func ExampleEndpointResolverFunc() {
// endpoint resolver wrapping the default endpoint resolver.
s3Svc := s3.New(cfg)
// Operation calls will be made to the custom endpoint.
objReq := s3Svc.GetObjectRequest(&types.GetObjectInput{
objReq := s3Svc.GetObjectRequest(&s3Types.GetObjectInput{
Bucket: aws.String("myBucket"),
Key: aws.String("myObjectKey"),
})
Expand All @@ -51,7 +51,7 @@ func ExampleEndpointResolverFunc() {
sqsSvc := sqs.New(cfg)
// Operation calls will be made to the default endpoint for SQS for the
// region configured.
msgReq := sqsSvc.ReceiveMessageRequest(&types2.ReceiveMessageInput{
msgReq := sqsSvc.ReceiveMessageRequest(&sqsTypes.ReceiveMessageInput{
QueueUrl: aws.String("my-queue-url"),
})
msgResp, err := msgReq.Send(context.Background())
Expand Down
14 changes: 7 additions & 7 deletions example/aws/endpoints/customEndpoint/customEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
dynamodb_types "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
dynamodbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3_types "github.com/aws/aws-sdk-go-v2/service/s3/types"
s3Types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/aws-sdk-go-v2/service/sqs"
sqs_types "github.com/aws/aws-sdk-go-v2/service/sqs/types"
sqsTypes "github.com/aws/aws-sdk-go-v2/service/sqs/types"
)

func main() {
Expand Down Expand Up @@ -41,7 +41,7 @@ func main() {
// endpoint resolver wrapping the default endpoint resolver.
s3Svc := s3.New(cfg)
// Operation calls will be made to the custom endpoint.
getReq := s3Svc.GetObjectRequest(&s3_types.GetObjectInput{
getReq := s3Svc.GetObjectRequest(&s3Types.GetObjectInput{
Bucket: aws.String("myBucket"),
Key: aws.String("myObjectKey"),
})
Expand All @@ -53,7 +53,7 @@ func main() {
sqsSvc := sqs.New(cfg)
// Operation calls will be made to the default endpoint for SQS for the
// region configured.
msgReq := sqsSvc.ReceiveMessageRequest(&sqs_types.ReceiveMessageInput{
msgReq := sqsSvc.ReceiveMessageRequest(&sqsTypes.ReceiveMessageInput{
QueueUrl: aws.String("my-queue-url"),
})
msgReq.Send(context.Background())
Expand All @@ -75,7 +75,7 @@ func main() {
ddbSvc := dynamodb.New(cfgCp)
// Operation calls will be made to the custom endpoint set in the
// ddCustResolverFn.
listReq := ddbSvc.ListTablesRequest(&dynamodb_types.ListTablesInput{})
listReq := ddbSvc.ListTablesRequest(&dynamodbTypes.ListTablesInput{})
listReq.Send(context.Background())

// Setting Config's Endpoint will override the EndpointResolver. Forcing
Expand All @@ -85,6 +85,6 @@ func main() {
cfgCp.EndpointResolver = aws.ResolveWithEndpointURL("http://localhost:8088")

ddbSvcLocal := dynamodb.New(cfgCp)
listReq = ddbSvcLocal.ListTablesRequest(&dynamodb_types.ListTablesInput{})
listReq = ddbSvcLocal.ListTablesRequest(&dynamodbTypes.ListTablesInput{})
listReq.Send(context.Background())
}
1 change: 0 additions & 1 deletion models/protocol_tests/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,6 @@ func main() {
fmt.Println("Generating test suite", os.Args[1:])
out := generateTestSuite(os.Args[1])

// Todo: fix test suites
if len(os.Args) == 3 {
f, err := os.Create(os.Args[2])
defer f.Close()
Expand Down
32 changes: 26 additions & 6 deletions private/model/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,27 @@ func (a *API) AddSDKImport(v ...string) error {
return nil
}

// AddSDKServiceImport adds a SDK service package import to the generated file's import.
func (a *API) AddSDKServiceImport(v ...string) error {
var e []string
e = append(e, a.BaseImportPath)
e = append(e, a.PackageName())
e = append(e, v...)

a.imports[path.Join(e...)] = true
return nil
}

// AddSDKServiceTypesImport adds a SDK service types package import to the generated file's import.
func (a *API) AddSDKServiceTypesImport() error {
return a.AddSDKServiceImport(ServiceTypesPkgName)
}

// AddSDKServiceEnumsImport adds a SDK service enums package import to the generated file's import.
func (a *API) AddSDKServiceEnumsImport() error {
return a.AddSDKServiceImport(ServiceEnumsPkgName)
}

// APIGoCode renders the API in Go code. Returning it as a string
func (a *API) APIGoCode() string {
a.resetImports()
Expand All @@ -305,7 +326,7 @@ func (a *API) APIGoCode() string {
a.AddSDKImport("private/protocol")
}
if !a.NoGenMarshalers || !a.NoGenUnmarshalers {
a.imports[path.Join(SDKImportRoot, "private/protocol")] = true
a.AddSDKImport("private/protocol")
}

var buf bytes.Buffer
Expand All @@ -324,7 +345,7 @@ func (a *API) APIOperationGoCode(op *Operation) string {
a.resetImports()
a.AddImport("context")
a.AddSDKImport("aws")
a.AddSDKImport("service", a.PackageName(), ServiceTypesPkgName)
a.AddSDKServiceTypesImport()

if op.OutputRef.Shape.Placeholder {
a.AddSDKImport("private/protocol", a.ProtocolPackage())
Expand Down Expand Up @@ -511,8 +532,6 @@ func (a *API) ProtocolCanonicalPackageName() string {
return "aws_ec2query"
case "query":
return "aws_query"
case "xml":
return "aws_xml"
case "json":
return "aws_jsonrpc"
default:
Expand Down Expand Up @@ -818,8 +837,9 @@ func (a *API) InterfaceGoCode() string {
if len(a.Waiters) != 0 {
a.AddSDKImport("aws")
}
a.AddImport(a.ImportPath())
a.AddSDKImport("service", a.PackageName(), ServiceTypesPkgName)

a.AddSDKServiceImport()
a.AddSDKServiceTypesImport()

var buf bytes.Buffer
err := tplInterface.Execute(&buf, a)
Expand Down
5 changes: 3 additions & 2 deletions private/model/api/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,9 @@ func (a *API) ExamplesGoCode() string {
a.AddSDKImport("aws")
a.AddSDKImport("aws/awserr")
a.AddSDKImport("aws/external")
a.AddSDKImport("service", a.PackageName())
a.AddSDKImport("service", a.PackageName(), "types")

a.AddSDKServiceImport()
a.AddSDKServiceTypesImport()

if err := exampleHeader.ExecuteTemplate(&buf, "exampleHeader", &exHeader{builder, a}); err != nil {
panic(err)
Expand Down
5 changes: 4 additions & 1 deletion private/model/api/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -173,7 +174,9 @@ func (a *API) Setup() {
}

a.fixStutterNames()
a.validateShapeNames()
if err := a.validateShapeNames(); err != nil {
log.Fatalf(err.Error())
}
a.renameExportable()
a.applyShapeNameAliases()
a.createInputOutputShapes()
Expand Down
71 changes: 46 additions & 25 deletions private/model/api/passes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"regexp"
"strings"
"unicode"
)

// updateTopLevelShapeReferences moves resultWrapper, locationName, and
Expand Down Expand Up @@ -160,36 +159,58 @@ func (a *API) fixStutterNames() {
}
}

func (a *API) validateShapeNames() {
// validateShapeNames is valid only for shapes of type structure or enums
// We validate a shape name to check if its a valid shape name
// A valid shape name would only contain alphanumeric characters and have an alphabet as leading character.
//
// If we encounter a shape name with underscores(_), we remove the underscores, and
// follow a canonical upper camel case naming scheme to create a new shape name.
// If the shape name collides with an existing shape name we return an error.
// The resulting shape name must be a valid shape name or throw an error.
func (a *API) validateShapeNames() error {
for _, s := range a.Shapes {
// name should start with an alphabet, that is ascii character 65 to 90, and 97 to 122;
// else skip that character
name := s.ShapeName
for len(name) > 1 {
if (s.Type == "structure" || s.IsEnum()) && !unicode.IsLetter(rune(name[0])) {
// Remove the leading underscores from the name of the shape, if shape is enum or structure
if name[0] == '_' {
name = name[1:]
} else {
// Throw an error if shape name starts with non alphabetic character and
// above condition is unsatisfied.
log.Fatalf("Shape starting with non alphabetical character found: %v", s.ShapeName)
}
} else {
break
if s.Type == "structure" || s.IsEnum() {
name := s.ShapeName

// The regexp used allows underscores(_) at the beginning of the shape name
// There may be 0 or more underscores(_). The next character which would be the leading character
// in the renamed shape must be an alphabetic character.
// The regex allows alphanumeric characters along with underscores(_) in rest of the string.
compiledRegExp, err := regexp.Compile("^[_]*[a-zA-Z][a-zA-Z0-9_]*$")
if err != nil {
return fmt.Errorf("failed to compile regex while validating shape name")
}

// check if the shape name satisfies the regex
if b := compiledRegExp.Match([]byte(name)); !b {
return fmt.Errorf("invalid shape name found: %v", s.ShapeName)
}
}

if s.ShapeName != name {
debugLogger.Logf("Renamed shape %v to %v for package %v \n", s.ShapeName, name, a.PackageName())
if a.Shapes[name] != nil {
// throw an error if shape with a new shape name already exists
log.Fatalf("Tried to rename shape %v to %v, but the new name results in shape name collision",
s.ShapeName, name)
// replace all _ with whitespace
name = strings.ReplaceAll(name, "_", " ")
// remove whitespace and get a slice of strings
slice := strings.Fields(name)

newName := ""
// creates the new shape name which would be upper camel cased
for _, word := range slice {
newName += strings.Title(word)
}

if s.ShapeName != newName {
if a.Shapes[newName] != nil {
// throw an error if shape with a new shape name already exists
err := fmt.Errorf("attempted to rename shape %v to %v, but the new name results in shape name collision",
s.ShapeName, newName)
log.Printf(err.Error())
return err
}
debugLogger.Logf("Renaming shape %v to %v for package %v \n", s.ShapeName, newName, a.PackageName())
s.Rename(newName)
}
s.Rename(name)
}
}
return nil
}

func (a *API) applyShapeNameAliases() {
Expand Down
Loading

0 comments on commit f061c17

Please sign in to comment.