plugin/etcdv3: Add etcd v3 plugin (#1702)

* Update dependencies and add etcdv3 client

* Update etcd plugin to support etcd v3 clients

Fixes #341
This commit is contained in:
Nitish Tiwari 2018-06-30 20:49:13 +05:30 committed by Miek Gieben
parent f3afd70021
commit 6fe27d99be
10327 changed files with 4196998 additions and 82 deletions

View file

@ -0,0 +1,853 @@
// +build codegen
// Package api represents API abstractions for rendering service generated files.
package api
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"path"
"path/filepath"
"sort"
"strings"
"text/template"
"unicode"
)
// An API defines a service API's definition. and logic to serialize the definition.
type API struct {
Metadata Metadata
Operations map[string]*Operation
Shapes map[string]*Shape
Waiters []Waiter
Documentation string
Examples Examples
// Set to true to avoid removing unused shapes
NoRemoveUnusedShapes bool
// Set to true to avoid renaming to 'Input/Output' postfixed shapes
NoRenameToplevelShapes bool
// Set to true to ignore service/request init methods (for testing)
NoInitMethods bool
// Set to true to ignore String() and GoString methods (for generated tests)
NoStringerMethods bool
// Set to true to not generate API service name constants
NoConstServiceNames bool
// Set to true to not generate validation shapes
NoValidataShapeMethods bool
// Set to true to not generate struct field accessors
NoGenStructFieldAccessors bool
SvcClientImportPath string
initialized bool
imports map[string]bool
name string
path string
BaseCrosslinkURL string
}
// A Metadata is the metadata about an API's definition.
type Metadata struct {
APIVersion string
EndpointPrefix string
SigningName string
ServiceAbbreviation string
ServiceFullName string
SignatureVersion string
JSONVersion string
TargetPrefix string
Protocol string
UID string
EndpointsID string
NoResolveEndpoint bool
}
var serviceAliases map[string]string
func Bootstrap() error {
b, err := ioutil.ReadFile(filepath.Join("..", "models", "customizations", "service-aliases.json"))
if err != nil {
return err
}
return json.Unmarshal(b, &serviceAliases)
}
// PackageName name of the API package
func (a *API) PackageName() string {
return strings.ToLower(a.StructName())
}
// InterfacePackageName returns the package name for the interface.
func (a *API) InterfacePackageName() string {
return a.PackageName() + "iface"
}
var stripServiceNamePrefixes = []string{
"Amazon",
"AWS",
}
// StructName returns the struct name for a given API.
func (a *API) StructName() string {
if len(a.name) != 0 {
return a.name
}
name := a.Metadata.ServiceAbbreviation
if len(name) == 0 {
name = a.Metadata.ServiceFullName
}
name = strings.TrimSpace(name)
// Strip out prefix names not reflected in service client symbol names.
for _, prefix := range stripServiceNamePrefixes {
if strings.HasPrefix(name, prefix) {
name = name[len(prefix):]
break
}
}
// Replace all Non-letter/number values with space
runes := []rune(name)
for i := 0; i < len(runes); i++ {
if r := runes[i]; !(unicode.IsNumber(r) || unicode.IsLetter(r)) {
runes[i] = ' '
}
}
name = string(runes)
// Title case name so its readable as a symbol.
name = strings.Title(name)
// Strip out spaces.
name = strings.Replace(name, " ", "", -1)
// Swap out for alias name if one is defined.
if alias, ok := serviceAliases[strings.ToLower(name)]; ok {
name = alias
}
a.name = name
return a.name
}
// UseInitMethods returns if the service's init method should be rendered.
func (a *API) UseInitMethods() bool {
return !a.NoInitMethods
}
// NiceName returns the human friendly API name.
func (a *API) NiceName() string {
if a.Metadata.ServiceAbbreviation != "" {
return a.Metadata.ServiceAbbreviation
}
return a.Metadata.ServiceFullName
}
// ProtocolPackage returns the package name of the protocol this API uses.
func (a *API) ProtocolPackage() string {
switch a.Metadata.Protocol {
case "json":
return "jsonrpc"
case "ec2":
return "ec2query"
default:
return strings.Replace(a.Metadata.Protocol, "-", "", -1)
}
}
// OperationNames returns a slice of API operations supported.
func (a *API) OperationNames() []string {
i, names := 0, make([]string, len(a.Operations))
for n := range a.Operations {
names[i] = n
i++
}
sort.Strings(names)
return names
}
// OperationList returns a slice of API operation pointers
func (a *API) OperationList() []*Operation {
list := make([]*Operation, len(a.Operations))
for i, n := range a.OperationNames() {
list[i] = a.Operations[n]
}
return list
}
// OperationHasOutputPlaceholder returns if any of the API operation input
// or output shapes are place holders.
func (a *API) OperationHasOutputPlaceholder() bool {
for _, op := range a.Operations {
if op.OutputRef.Shape.Placeholder {
return true
}
}
return false
}
// ShapeNames returns a slice of names for each shape used by the API.
func (a *API) ShapeNames() []string {
i, names := 0, make([]string, len(a.Shapes))
for n := range a.Shapes {
names[i] = n
i++
}
sort.Strings(names)
return names
}
// ShapeList returns a slice of shape pointers used by the API.
//
// Will exclude error shapes from the list of shapes returned.
func (a *API) ShapeList() []*Shape {
list := make([]*Shape, 0, len(a.Shapes))
for _, n := range a.ShapeNames() {
// Ignore error shapes in list
if s := a.Shapes[n]; !s.IsError {
list = append(list, s)
}
}
return list
}
// ShapeListErrors returns a list of the errors defined by the API model
func (a *API) ShapeListErrors() []*Shape {
list := []*Shape{}
for _, n := range a.ShapeNames() {
// Ignore error shapes in list
if s := a.Shapes[n]; s.IsError {
list = append(list, s)
}
}
return list
}
// resetImports resets the import map to default values.
func (a *API) resetImports() {
a.imports = map[string]bool{
"github.com/aws/aws-sdk-go/aws": true,
}
}
// importsGoCode returns the generated Go import code.
func (a *API) importsGoCode() string {
if len(a.imports) == 0 {
return ""
}
corePkgs, extPkgs := []string{}, []string{}
for i := range a.imports {
if strings.Contains(i, ".") {
extPkgs = append(extPkgs, i)
} else {
corePkgs = append(corePkgs, i)
}
}
sort.Strings(corePkgs)
sort.Strings(extPkgs)
code := "import (\n"
for _, i := range corePkgs {
code += fmt.Sprintf("\t%q\n", i)
}
if len(corePkgs) > 0 {
code += "\n"
}
for _, i := range extPkgs {
code += fmt.Sprintf("\t%q\n", i)
}
code += ")\n\n"
return code
}
// A tplAPI is the top level template for the API
var tplAPI = template.Must(template.New("api").Parse(`
{{ range $_, $o := .OperationList }}
{{ $o.GoCode }}
{{ end }}
{{ range $_, $s := .ShapeList }}
{{ if and $s.IsInternal (eq $s.Type "structure") }}{{ $s.GoCode }}{{ end }}
{{ end }}
{{ range $_, $s := .ShapeList }}
{{ if $s.IsEnum }}{{ $s.GoCode }}{{ end }}
{{ end }}
`))
// APIGoCode renders the API in Go code. Returning it as a string
func (a *API) APIGoCode() string {
a.resetImports()
a.imports["github.com/aws/aws-sdk-go/aws/awsutil"] = true
a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
if a.OperationHasOutputPlaceholder() {
a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
a.imports["github.com/aws/aws-sdk-go/private/protocol"] = true
}
for _, op := range a.Operations {
if op.AuthType == "none" {
a.imports["github.com/aws/aws-sdk-go/aws/credentials"] = true
break
}
}
var buf bytes.Buffer
err := tplAPI.Execute(&buf, a)
if err != nil {
panic(err)
}
code := a.importsGoCode() + strings.TrimSpace(buf.String())
return code
}
var noCrossLinkServices = map[string]struct{}{
"apigateway": {},
"budgets": {},
"cloudsearch": {},
"cloudsearchdomain": {},
"elastictranscoder": {},
"es": {},
"glacier": {},
"importexport": {},
"iot": {},
"iot-data": {},
"machinelearning": {},
"rekognition": {},
"sdb": {},
"swf": {},
}
// HasCrosslinks will return whether or not a service has crosslinking .
func HasCrosslinks(service string) bool {
_, ok := noCrossLinkServices[service]
return !ok
}
// GetCrosslinkURL returns the crosslinking URL for the shape based on the name and
// uid provided. Empty string is returned if no crosslink link could be determined.
func GetCrosslinkURL(baseURL, uid string, params ...string) string {
if uid == "" || baseURL == "" {
return ""
}
if !HasCrosslinks(strings.ToLower(ServiceIDFromUID(uid))) {
return ""
}
return strings.Join(append([]string{baseURL, "goto", "WebAPI", uid}, params...), "/")
}
// ServiceIDFromUID will parse the service id from the uid and return
// the service id that was found.
func ServiceIDFromUID(uid string) string {
found := 0
i := len(uid) - 1
for ; i >= 0; i-- {
if uid[i] == '-' {
found++
}
// Terminate after the date component is found, e.g. es-2017-11-11
if found == 3 {
break
}
}
return uid[0:i]
}
// APIName returns the API's service name.
func (a *API) APIName() string {
return a.name
}
var tplServiceDoc = template.Must(template.New("service docs").Funcs(template.FuncMap{
"GetCrosslinkURL": GetCrosslinkURL,
}).
Parse(`
// Package {{ .PackageName }} provides the client and types for making API
// requests to {{ .Metadata.ServiceFullName }}.
{{ if .Documentation -}}
//
{{ .Documentation }}
{{ end -}}
{{ $crosslinkURL := GetCrosslinkURL $.BaseCrosslinkURL $.Metadata.UID -}}
{{ if $crosslinkURL -}}
//
// See {{ $crosslinkURL }} for more information on this service.
{{ end -}}
//
// See {{ .PackageName }} package documentation for more information.
// https://docs.aws.amazon.com/sdk-for-go/api/service/{{ .PackageName }}/
//
// Using the Client
//
// To contact {{ .Metadata.ServiceFullName }} with the SDK use the New function to create
// a new service client. With that client you can make API requests to the service.
// These clients are safe to use concurrently.
//
// See the SDK's documentation for more information on how to use the SDK.
// https://docs.aws.amazon.com/sdk-for-go/api/
//
// See aws.Config documentation for more information on configuring SDK clients.
// https://docs.aws.amazon.com/sdk-for-go/api/aws/#Config
//
// See the {{ .Metadata.ServiceFullName }} client {{ .StructName }} for more
// information on creating client for this service.
// https://docs.aws.amazon.com/sdk-for-go/api/service/{{ .PackageName }}/#New
`))
// A tplService defines the template for the service generated code.
var tplService = template.Must(template.New("service").Funcs(template.FuncMap{
"ServiceNameValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
}
return "ServiceName"
},
"EndpointsIDConstValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
}
if a.Metadata.EndpointPrefix == a.Metadata.EndpointsID {
return "ServiceName"
}
return fmt.Sprintf("%q", a.Metadata.EndpointsID)
},
"EndpointsIDValue": func(a *API) string {
if a.NoConstServiceNames {
return fmt.Sprintf("%q", a.Metadata.EndpointPrefix)
}
return "EndpointsID"
},
}).Parse(`
// {{ .StructName }} provides the API operation methods for making requests to
// {{ .Metadata.ServiceFullName }}. See this package's package overview docs
// for details on the service.
//
// {{ .StructName }} methods are safe to use concurrently. It is not safe to
// modify mutate any of the struct's properties though.
type {{ .StructName }} struct {
*client.Client
}
{{ if .UseInitMethods }}// Used for custom client initialization logic
var initClient func(*client.Client)
// Used for custom request initialization logic
var initRequest func(*request.Request)
{{ end }}
{{ if not .NoConstServiceNames -}}
// Service information constants
const (
ServiceName = "{{ .Metadata.EndpointPrefix }}" // Service endpoint prefix API calls made to.
EndpointsID = {{ EndpointsIDConstValue . }} // Service ID for Regions and Endpoints metadata.
)
{{- end }}
// New creates a new instance of the {{ .StructName }} client with a session.
// If additional configuration is needed for the client instance use the optional
// aws.Config parameter to add your extra config.
//
// Example:
// // Create a {{ .StructName }} client from just a session.
// svc := {{ .PackageName }}.New(mySession)
//
// // Create a {{ .StructName }} client with additional configuration
// svc := {{ .PackageName }}.New(mySession, aws.NewConfig().WithRegion("us-west-2"))
func New(p client.ConfigProvider, cfgs ...*aws.Config) *{{ .StructName }} {
{{ if .Metadata.NoResolveEndpoint -}}
var c client.Config
if v, ok := p.(client.ConfigNoResolveEndpointProvider); ok {
c = v.ClientConfigNoResolveEndpoint(cfgs...)
} else {
c = p.ClientConfig({{ EndpointsIDValue . }}, cfgs...)
}
{{- else -}}
c := p.ClientConfig({{ EndpointsIDValue . }}, cfgs...)
{{- end }}
{{- if .Metadata.SigningName }}
if c.SigningNameDerived || len(c.SigningName) == 0{
c.SigningName = "{{ .Metadata.SigningName }}"
}
{{- end }}
return newClient(*c.Config, c.Handlers, c.Endpoint, c.SigningRegion, c.SigningName)
}
// newClient creates, initializes and returns a new service client instance.
func newClient(cfg aws.Config, handlers request.Handlers, endpoint, signingRegion, signingName string) *{{ .StructName }} {
svc := &{{ .StructName }}{
Client: client.New(
cfg,
metadata.ClientInfo{
ServiceName: {{ ServiceNameValue . }},
SigningName: signingName,
SigningRegion: signingRegion,
Endpoint: endpoint,
APIVersion: "{{ .Metadata.APIVersion }}",
{{ if .Metadata.JSONVersion -}}
JSONVersion: "{{ .Metadata.JSONVersion }}",
{{- end }}
{{ if .Metadata.TargetPrefix -}}
TargetPrefix: "{{ .Metadata.TargetPrefix }}",
{{- end }}
},
handlers,
),
}
// Handlers
svc.Handlers.Sign.PushBackNamed({{if eq .Metadata.SignatureVersion "v2"}}v2{{else}}v4{{end}}.SignRequestHandler)
{{- if eq .Metadata.SignatureVersion "v2" }}
svc.Handlers.Sign.PushBackNamed(corehandlers.BuildContentLengthHandler)
{{- end }}
svc.Handlers.Build.PushBackNamed({{ .ProtocolPackage }}.BuildHandler)
svc.Handlers.Unmarshal.PushBackNamed({{ .ProtocolPackage }}.UnmarshalHandler)
svc.Handlers.UnmarshalMeta.PushBackNamed({{ .ProtocolPackage }}.UnmarshalMetaHandler)
svc.Handlers.UnmarshalError.PushBackNamed({{ .ProtocolPackage }}.UnmarshalErrorHandler)
{{ if .UseInitMethods }}// Run custom client initialization if present
if initClient != nil {
initClient(svc.Client)
}
{{ end }}
return svc
}
// newRequest creates a new request for a {{ .StructName }} operation and runs any
// custom request initialization.
func (c *{{ .StructName }}) newRequest(op *request.Operation, params, data interface{}) *request.Request {
req := c.NewRequest(op, params, data)
{{ if .UseInitMethods }}// Run custom request initialization if present
if initRequest != nil {
initRequest(req)
}
{{ end }}
return req
}
`))
// ServicePackageDoc generates the contents of the doc file for the service.
//
// Will also read in the custom doc templates for the service if found.
func (a *API) ServicePackageDoc() string {
a.imports = map[string]bool{}
var buf bytes.Buffer
if err := tplServiceDoc.Execute(&buf, a); err != nil {
panic(err)
}
return buf.String()
}
// ServiceGoCode renders service go code. Returning it as a string.
func (a *API) ServiceGoCode() string {
a.resetImports()
a.imports["github.com/aws/aws-sdk-go/aws/client"] = true
a.imports["github.com/aws/aws-sdk-go/aws/client/metadata"] = true
a.imports["github.com/aws/aws-sdk-go/aws/request"] = true
if a.Metadata.SignatureVersion == "v2" {
a.imports["github.com/aws/aws-sdk-go/private/signer/v2"] = true
a.imports["github.com/aws/aws-sdk-go/aws/corehandlers"] = true
} else {
a.imports["github.com/aws/aws-sdk-go/aws/signer/v4"] = true
}
a.imports["github.com/aws/aws-sdk-go/private/protocol/"+a.ProtocolPackage()] = true
var buf bytes.Buffer
err := tplService.Execute(&buf, a)
if err != nil {
panic(err)
}
code := a.importsGoCode() + buf.String()
return code
}
// ExampleGoCode renders service example code. Returning it as a string.
func (a *API) ExampleGoCode() string {
exs := []string{}
imports := map[string]bool{}
for _, o := range a.OperationList() {
o.imports = map[string]bool{}
exs = append(exs, o.Example())
for k, v := range o.imports {
imports[k] = v
}
}
code := fmt.Sprintf("import (\n%q\n%q\n%q\n\n%q\n%q\n%q\n",
"bytes",
"fmt",
"time",
"github.com/aws/aws-sdk-go/aws",
"github.com/aws/aws-sdk-go/aws/session",
path.Join(a.SvcClientImportPath, a.PackageName()),
)
for k := range imports {
code += fmt.Sprintf("%q\n", k)
}
code += ")\n\n"
code += "var _ time.Duration\nvar _ bytes.Buffer\n\n"
code += strings.Join(exs, "\n\n")
return code
}
// A tplInterface defines the template for the service interface type.
var tplInterface = template.Must(template.New("interface").Parse(`
// {{ .StructName }}API provides an interface to enable mocking the
// {{ .PackageName }}.{{ .StructName }} service client's API operation,
// paginators, and waiters. This make unit testing your code that calls out
// to the SDK's service client's calls easier.
//
// The best way to use this interface is so the SDK's service client's calls
// can be stubbed out for unit testing your code with the SDK without needing
// to inject custom request handlers into the SDK's request pipeline.
//
// // myFunc uses an SDK service client to make a request to
// // {{.Metadata.ServiceFullName}}. {{ $opts := .OperationList }}{{ $opt := index $opts 0 }}
// func myFunc(svc {{ .InterfacePackageName }}.{{ .StructName }}API) bool {
// // Make svc.{{ $opt.ExportedName }} request
// }
//
// func main() {
// sess := session.New()
// svc := {{ .PackageName }}.New(sess)
//
// myFunc(svc)
// }
//
// In your _test.go file:
//
// // Define a mock struct to be used in your unit tests of myFunc.
// type mock{{ .StructName }}Client struct {
// {{ .InterfacePackageName }}.{{ .StructName }}API
// }
// func (m *mock{{ .StructName }}Client) {{ $opt.ExportedName }}(input {{ $opt.InputRef.GoTypeWithPkgName }}) ({{ $opt.OutputRef.GoTypeWithPkgName }}, error) {
// // mock response/functionality
// }
//
// func TestMyFunc(t *testing.T) {
// // Setup Test
// mockSvc := &mock{{ .StructName }}Client{}
//
// myfunc(mockSvc)
//
// // Verify myFunc's functionality
// }
//
// It is important to note that this interface will have breaking changes
// when the service model is updated and adds new API operations, paginators,
// and waiters. Its suggested to use the pattern above for testing, or using
// tooling to generate mocks to satisfy the interfaces.
type {{ .StructName }}API interface {
{{ range $_, $o := .OperationList }}
{{ $o.InterfaceSignature }}
{{ end }}
{{ range $_, $w := .Waiters }}
{{ $w.InterfaceSignature }}
{{ end }}
}
var _ {{ .StructName }}API = (*{{ .PackageName }}.{{ .StructName }})(nil)
`))
// InterfaceGoCode returns the go code for the service's API operations as an
// interface{}. Assumes that the interface is being created in a different
// package than the service API's package.
func (a *API) InterfaceGoCode() string {
a.resetImports()
a.imports = map[string]bool{
"github.com/aws/aws-sdk-go/aws": true,
"github.com/aws/aws-sdk-go/aws/request": true,
path.Join(a.SvcClientImportPath, a.PackageName()): true,
}
var buf bytes.Buffer
err := tplInterface.Execute(&buf, a)
if err != nil {
panic(err)
}
code := a.importsGoCode() + strings.TrimSpace(buf.String())
return code
}
// NewAPIGoCodeWithPkgName returns a string of instantiating the API prefixed
// with its package name. Takes a string depicting the Config.
func (a *API) NewAPIGoCodeWithPkgName(cfg string) string {
return fmt.Sprintf("%s.New(%s)", a.PackageName(), cfg)
}
// computes the validation chain for all input shapes
func (a *API) addShapeValidations() {
for _, o := range a.Operations {
resolveShapeValidations(o.InputRef.Shape)
}
}
// Updates the source shape and all nested shapes with the validations that
// could possibly be needed.
func resolveShapeValidations(s *Shape, ancestry ...*Shape) {
for _, a := range ancestry {
if a == s {
return
}
}
children := []string{}
for _, name := range s.MemberNames() {
ref := s.MemberRefs[name]
if s.IsRequired(name) && !s.Validations.Has(ref, ShapeValidationRequired) {
s.Validations = append(s.Validations, ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationRequired,
})
}
if ref.Shape.Min != 0 && !s.Validations.Has(ref, ShapeValidationMinVal) {
s.Validations = append(s.Validations, ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationMinVal,
})
}
switch ref.Shape.Type {
case "map", "list", "structure":
children = append(children, name)
}
}
ancestry = append(ancestry, s)
for _, name := range children {
ref := s.MemberRefs[name]
// Since this is a grab bag we will just continue since
// we can't validate because we don't know the valued shape.
if ref.JSONValue {
continue
}
nestedShape := ref.Shape.NestedShape()
var v *ShapeValidation
if len(nestedShape.Validations) > 0 {
v = &ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationNested,
}
} else {
resolveShapeValidations(nestedShape, ancestry...)
if len(nestedShape.Validations) > 0 {
v = &ShapeValidation{
Name: name, Ref: ref, Type: ShapeValidationNested,
}
}
}
if v != nil && !s.Validations.Has(v.Ref, v.Type) {
s.Validations = append(s.Validations, *v)
}
}
ancestry = ancestry[:len(ancestry)-1]
}
// A tplAPIErrors is the top level template for the API
var tplAPIErrors = template.Must(template.New("api").Parse(`
const (
{{ range $_, $s := $.ShapeListErrors }}
// {{ $s.ErrorCodeName }} for service response error code
// {{ printf "%q" $s.ErrorName }}.
{{ if $s.Docstring -}}
//
{{ $s.Docstring }}
{{ end -}}
{{ $s.ErrorCodeName }} = {{ printf "%q" $s.ErrorName }}
{{ end }}
)
`))
func (a *API) APIErrorsGoCode() string {
var buf bytes.Buffer
err := tplAPIErrors.Execute(&buf, a)
if err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
// removeOperation removes an operation, its input/output shapes, as well as
// any references/shapes that are unique to this operation.
func (a *API) removeOperation(name string) {
fmt.Println("removing operation,", name)
op := a.Operations[name]
delete(a.Operations, name)
delete(a.Examples, name)
a.removeShape(op.InputRef.Shape)
a.removeShape(op.OutputRef.Shape)
}
// removeShape removes the given shape, and all form member's reference target
// shapes. Will also remove member reference targeted shapes if those shapes do
// not have any additional references.
func (a *API) removeShape(s *Shape) {
fmt.Println("removing shape,", s.ShapeName)
delete(a.Shapes, s.ShapeName)
for name, ref := range s.MemberRefs {
a.removeShapeRef(ref)
delete(s.MemberRefs, name)
}
for _, ref := range []*ShapeRef{&s.MemberRef, &s.KeyRef, &s.ValueRef} {
if ref.Shape == nil {
continue
}
a.removeShapeRef(ref)
*ref = ShapeRef{}
}
}
// removeShapeRef removes the shape reference from its target shape. If the
// reference was the last reference to the target shape, the shape will also be
// removed.
func (a *API) removeShapeRef(ref *ShapeRef) {
if ref.Shape == nil {
return
}
ref.Shape.removeRef(ref)
if len(ref.Shape.refs) == 0 {
a.removeShape(ref.Shape)
}
}

View file

@ -0,0 +1,71 @@
// +build go1.8,codegen
package api
import (
"testing"
)
func TestAPI_StructName(t *testing.T) {
origAliases := serviceAliases
defer func() { serviceAliases = origAliases }()
cases := map[string]struct {
Aliases map[string]string
Metadata Metadata
StructName string
}{
"FullName": {
Metadata: Metadata{
ServiceFullName: "Amazon Service Name-100",
},
StructName: "ServiceName100",
},
"Abbreviation": {
Metadata: Metadata{
ServiceFullName: "Amazon Service Name-100",
ServiceAbbreviation: "AWS SN100",
},
StructName: "SN100",
},
"Lowercase Name": {
Metadata: Metadata{
EndpointPrefix: "other",
ServiceFullName: "AWS Lowercase service",
ServiceAbbreviation: "lowercase",
},
StructName: "Lowercase",
},
"Lowercase Name Mixed": {
Metadata: Metadata{
EndpointPrefix: "other",
ServiceFullName: "AWS Lowercase service",
ServiceAbbreviation: "lowercase name Goes heRe",
},
StructName: "LowercaseNameGoesHeRe",
},
"Alias": {
Aliases: map[string]string{
"elasticloadbalancing": "ELB",
},
Metadata: Metadata{
ServiceFullName: "Elastic Load Balancing",
},
StructName: "ELB",
},
}
for k, c := range cases {
t.Run(k, func(t *testing.T) {
serviceAliases = c.Aliases
a := API{
Metadata: c.Metadata,
}
if e, o := c.StructName, a.StructName(); e != o {
t.Errorf("expect %v structName, got %v", e, o)
}
})
}
}

View file

@ -0,0 +1,180 @@
// +build codegen
package api
import (
"io/ioutil"
"path/filepath"
"strings"
)
type service struct {
srcName string
dstName string
serviceVersion string
}
var mergeServices = map[string]service{
"dynamodbstreams": {
dstName: "dynamodb",
srcName: "streams.dynamodb",
},
"wafregional": {
dstName: "waf",
srcName: "waf-regional",
serviceVersion: "2015-08-24",
},
}
// customizationPasses Executes customization logic for the API by package name.
func (a *API) customizationPasses() {
var svcCustomizations = map[string]func(*API){
"s3": s3Customizations,
"cloudfront": cloudfrontCustomizations,
"rds": rdsCustomizations,
// Disable endpoint resolving for services that require customer
// to provide endpoint them selves.
"cloudsearchdomain": disableEndpointResolving,
"iotdataplane": disableEndpointResolving,
}
for k := range mergeServices {
svcCustomizations[k] = mergeServicesCustomizations
}
if fn := svcCustomizations[a.PackageName()]; fn != nil {
fn(a)
}
}
// s3Customizations customizes the API generation to replace values specific to S3.
func s3Customizations(a *API) {
var strExpires *Shape
var keepContentMD5Ref = map[string]struct{}{
"PutObjectInput": struct{}{},
"UploadPartInput": struct{}{},
}
for name, s := range a.Shapes {
// Remove ContentMD5 members unless specified otherwise.
if _, keep := keepContentMD5Ref[name]; !keep {
if _, have := s.MemberRefs["ContentMD5"]; have {
delete(s.MemberRefs, "ContentMD5")
}
}
// Generate getter methods for API operation fields used by customizations.
for _, refName := range []string{"Bucket", "SSECustomerKey", "CopySourceSSECustomerKey"} {
if ref, ok := s.MemberRefs[refName]; ok {
ref.GenerateGetter = true
}
}
// Expires should be a string not time.Time since the format is not
// enforced by S3, and any value can be set to this field outside of the SDK.
if strings.HasSuffix(name, "Output") {
if ref, ok := s.MemberRefs["Expires"]; ok {
if strExpires == nil {
newShape := *ref.Shape
strExpires = &newShape
strExpires.Type = "string"
strExpires.refs = []*ShapeRef{}
}
ref.Shape.removeRef(ref)
ref.Shape = strExpires
ref.Shape.refs = append(ref.Shape.refs, &s.MemberRef)
}
}
}
s3CustRemoveHeadObjectModeledErrors(a)
}
// S3 HeadObject API call incorrect models NoSuchKey as valid
// error code that can be returned. This operation does not
// return error codes, all error codes are derived from HTTP
// status codes.
//
// aws/aws-sdk-go#1208
func s3CustRemoveHeadObjectModeledErrors(a *API) {
op, ok := a.Operations["HeadObject"]
if !ok {
return
}
op.Documentation += `
//
// See http://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#RESTErrorResponses
// for more information on returned errors.`
op.ErrorRefs = []ShapeRef{}
}
// cloudfrontCustomizations customized the API generation to replace values
// specific to CloudFront.
func cloudfrontCustomizations(a *API) {
// MaxItems members should always be integers
for _, s := range a.Shapes {
if ref, ok := s.MemberRefs["MaxItems"]; ok {
ref.ShapeName = "Integer"
ref.Shape = a.Shapes["Integer"]
}
}
}
// mergeServicesCustomizations references any duplicate shapes from DynamoDB
func mergeServicesCustomizations(a *API) {
info := mergeServices[a.PackageName()]
p := strings.Replace(a.path, info.srcName, info.dstName, -1)
if info.serviceVersion != "" {
index := strings.LastIndex(p, "/")
files, _ := ioutil.ReadDir(p[:index])
if len(files) > 1 {
panic("New version was introduced")
}
p = p[:index] + "/" + info.serviceVersion
}
file := filepath.Join(p, "api-2.json")
serviceAPI := API{}
serviceAPI.Attach(file)
serviceAPI.Setup()
for n := range a.Shapes {
if _, ok := serviceAPI.Shapes[n]; ok {
a.Shapes[n].resolvePkg = "github.com/aws/aws-sdk-go/service/" + info.dstName
}
}
}
// rdsCustomizations are customization for the service/rds. This adds non-modeled fields used for presigning.
func rdsCustomizations(a *API) {
inputs := []string{
"CopyDBSnapshotInput",
"CreateDBInstanceReadReplicaInput",
"CopyDBClusterSnapshotInput",
"CreateDBClusterInput",
}
for _, input := range inputs {
if ref, ok := a.Shapes[input]; ok {
ref.MemberRefs["SourceRegion"] = &ShapeRef{
Documentation: docstring(`SourceRegion is the source region where the resource exists. This is not sent over the wire and is only used for presigning. This value should always have the same region as the source ARN.`),
ShapeName: "String",
Shape: a.Shapes["String"],
Ignore: true,
}
ref.MemberRefs["DestinationRegion"] = &ShapeRef{
Documentation: docstring(`DestinationRegion is used for presigning the request to a given region.`),
ShapeName: "String",
Shape: a.Shapes["String"],
}
}
}
}
func disableEndpointResolving(a *API) {
a.Metadata.NoResolveEndpoint = true
}

View file

@ -0,0 +1,411 @@
// +build codegen
package api
import (
"bytes"
"encoding/json"
"fmt"
"html"
"os"
"regexp"
"strings"
xhtml "golang.org/x/net/html"
)
type apiDocumentation struct {
*API
Operations map[string]string
Service string
Shapes map[string]shapeDocumentation
}
type shapeDocumentation struct {
Base string
Refs map[string]string
}
// AttachDocs attaches documentation from a JSON filename.
func (a *API) AttachDocs(filename string) {
d := apiDocumentation{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&d)
if err != nil {
panic(err)
}
d.setup()
}
func (d *apiDocumentation) setup() {
d.API.Documentation = docstring(d.Service)
for op, doc := range d.Operations {
d.API.Operations[op].Documentation = docstring(doc)
}
for shape, info := range d.Shapes {
if sh := d.API.Shapes[shape]; sh != nil {
sh.Documentation = docstring(info.Base)
}
for ref, doc := range info.Refs {
if doc == "" {
continue
}
parts := strings.Split(ref, "$")
if len(parts) != 2 {
fmt.Fprintf(os.Stderr, "Shape Doc %s has unexpected reference format, %q\n", shape, ref)
continue
}
if sh := d.API.Shapes[parts[0]]; sh != nil {
if m := sh.MemberRefs[parts[1]]; m != nil {
m.Documentation = docstring(doc)
}
}
}
}
}
var reNewline = regexp.MustCompile(`\r?\n`)
var reMultiSpace = regexp.MustCompile(`\s+`)
var reComments = regexp.MustCompile(`<!--.*?-->`)
var reFullnameBlock = regexp.MustCompile(`<fullname>(.+?)<\/fullname>`)
var reFullname = regexp.MustCompile(`<fullname>(.*?)</fullname>`)
var reExamples = regexp.MustCompile(`<examples?>.+?<\/examples?>`)
var reEndNL = regexp.MustCompile(`\n+$`)
// docstring rewrites a string to insert godocs formatting.
func docstring(doc string) string {
doc = strings.TrimSpace(doc)
if doc == "" {
return ""
}
doc = reNewline.ReplaceAllString(doc, "")
doc = reMultiSpace.ReplaceAllString(doc, " ")
doc = reComments.ReplaceAllString(doc, "")
var fullname string
parts := reFullnameBlock.FindStringSubmatch(doc)
if len(parts) > 1 {
fullname = parts[1]
}
// Remove full name block from doc string
doc = reFullname.ReplaceAllString(doc, "")
doc = reExamples.ReplaceAllString(doc, "")
doc = generateDoc(doc)
doc = reEndNL.ReplaceAllString(doc, "")
doc = html.UnescapeString(doc)
// Replace doc with full name if doc is empty.
doc = strings.TrimSpace(doc)
if len(doc) == 0 {
doc = fullname
}
return commentify(doc)
}
const (
indent = " "
)
// style is what we want to prefix a string with.
// For instance, <li>Foo</li><li>Bar</li>, will generate
// * Foo
// * Bar
var style = map[string]string{
"ul": indent + "* ",
"li": indent + "* ",
"code": indent,
"pre": indent,
}
// commentify converts a string to a Go comment
func commentify(doc string) string {
if len(doc) == 0 {
return ""
}
lines := strings.Split(doc, "\n")
out := make([]string, 0, len(lines))
for i := 0; i < len(lines); i++ {
line := lines[i]
if i > 0 && line == "" && lines[i-1] == "" {
continue
}
out = append(out, line)
}
if len(out) > 0 {
out[0] = "// " + out[0]
return strings.Join(out, "\n// ")
}
return ""
}
// wrap returns a rewritten version of text to have line breaks
// at approximately length characters. Line breaks will only be
// inserted into whitespace.
func wrap(text string, length int, isIndented bool) string {
var buf bytes.Buffer
var last rune
var lastNL bool
var col int
for _, c := range text {
switch c {
case '\r': // ignore this
continue // and also don't track `last`
case '\n': // ignore this too, but reset col
if col >= length || last == '\n' {
buf.WriteString("\n")
}
buf.WriteString("\n")
col = 0
case ' ', '\t': // opportunity to split
if col >= length {
buf.WriteByte('\n')
col = 0
if isIndented {
buf.WriteString(indent)
col += 3
}
} else {
// We only want to write a leading space if the col is greater than zero.
// This will provide the proper spacing for documentation.
buf.WriteRune(c)
col++ // count column
}
default:
buf.WriteRune(c)
col++
}
lastNL = c == '\n'
_ = lastNL
last = c
}
return buf.String()
}
type tagInfo struct {
tag string
key string
val string
txt string
raw string
closingTag bool
}
// generateDoc will generate the proper doc string for html encoded or plain text doc entries.
func generateDoc(htmlSrc string) string {
tokenizer := xhtml.NewTokenizer(strings.NewReader(htmlSrc))
tokens := buildTokenArray(tokenizer)
scopes := findScopes(tokens)
return walk(scopes)
}
func buildTokenArray(tokenizer *xhtml.Tokenizer) []tagInfo {
tokens := []tagInfo{}
for tt := tokenizer.Next(); tt != xhtml.ErrorToken; tt = tokenizer.Next() {
switch tt {
case xhtml.TextToken:
txt := string(tokenizer.Text())
if len(tokens) == 0 {
info := tagInfo{
raw: txt,
}
tokens = append(tokens, info)
}
tn, _ := tokenizer.TagName()
key, val, _ := tokenizer.TagAttr()
info := tagInfo{
tag: string(tn),
key: string(key),
val: string(val),
txt: txt,
}
tokens = append(tokens, info)
case xhtml.StartTagToken:
tn, _ := tokenizer.TagName()
key, val, _ := tokenizer.TagAttr()
info := tagInfo{
tag: string(tn),
key: string(key),
val: string(val),
}
tokens = append(tokens, info)
case xhtml.SelfClosingTagToken, xhtml.EndTagToken:
tn, _ := tokenizer.TagName()
key, val, _ := tokenizer.TagAttr()
info := tagInfo{
tag: string(tn),
key: string(key),
val: string(val),
closingTag: true,
}
tokens = append(tokens, info)
}
}
return tokens
}
// walk is used to traverse each scoped block. These scoped
// blocks will act as blocked text where we do most of our
// text manipulation.
func walk(scopes [][]tagInfo) string {
doc := ""
// Documentation will be chunked by scopes.
// Meaning, for each scope will be divided by one or more newlines.
for _, scope := range scopes {
indentStr, isIndented := priorityIndentation(scope)
block := ""
href := ""
after := false
level := 0
lastTag := ""
for _, token := range scope {
if token.closingTag {
endl := closeTag(token, level)
block += endl
level--
lastTag = ""
} else if token.txt == "" {
if token.val != "" {
href, after = formatText(token, "")
}
if level == 1 && isIndented {
block += indentStr
}
level++
lastTag = token.tag
} else {
if token.txt != " " {
str, _ := formatText(token, lastTag)
block += str
if after {
block += href
after = false
}
} else {
fmt.Println(token.tag)
str, _ := formatText(tagInfo{}, lastTag)
block += str
}
}
}
if !isIndented {
block = strings.TrimPrefix(block, " ")
}
block = wrap(block, 72, isIndented)
doc += block
}
return doc
}
// closeTag will divide up the blocks of documentation to be formated properly.
func closeTag(token tagInfo, level int) string {
switch token.tag {
case "pre", "li", "div":
return "\n"
case "p", "h1", "h2", "h3", "h4", "h5", "h6":
return "\n\n"
case "code":
// indented code is only at the 0th level.
if level == 0 {
return "\n"
}
}
return ""
}
// formatText will format any sort of text based off of a tag. It will also return
// a boolean to add the string after the text token.
func formatText(token tagInfo, lastTag string) (string, bool) {
switch token.tag {
case "a":
if token.val != "" {
return fmt.Sprintf(" (%s)", token.val), true
}
}
// We don't care about a single space nor no text.
if len(token.txt) == 0 || token.txt == " " {
return "", false
}
// Here we want to indent code blocks that are newlines
if lastTag == "code" {
// Greater than one, because we don't care about newlines in the beginning
block := ""
if lines := strings.Split(token.txt, "\n"); len(lines) > 1 {
for _, line := range lines {
block += indent + line
}
block += "\n"
return block, false
}
}
return token.txt, false
}
// This is a parser to check what type of indention is needed.
func priorityIndentation(blocks []tagInfo) (string, bool) {
if len(blocks) == 0 {
return "", false
}
v, ok := style[blocks[0].tag]
return v, ok
}
// Divides into scopes based off levels.
// For instance,
// <p>Testing<code>123</code></p><ul><li>Foo</li></ul>
// This has 2 scopes, the <p> and <ul>
func findScopes(tokens []tagInfo) [][]tagInfo {
level := 0
scope := []tagInfo{}
scopes := [][]tagInfo{}
for _, token := range tokens {
// we will clear empty tagged tokens from the array
txt := strings.TrimSpace(token.txt)
tag := strings.TrimSpace(token.tag)
if len(txt) == 0 && len(tag) == 0 {
continue
}
scope = append(scope, token)
// If it is a closing tag then we check what level
// we are on. If it is 0, then that means we have found a
// scoped block.
if token.closingTag {
level--
if level == 0 {
scopes = append(scopes, scope)
scope = []tagInfo{}
}
// Check opening tags and increment the level
} else if token.txt == "" {
level++
}
}
// In this case, we did not run into a closing tag. This would mean
// we have plaintext for documentation.
if len(scopes) == 0 {
scopes = append(scopes, scope)
}
return scopes
}

View file

@ -0,0 +1,100 @@
// +build 1.6,codegen
package api
import (
"testing"
)
func TestNonHTMLDocGen(t *testing.T) {
doc := "Testing 1 2 3"
expected := "// Testing 1 2 3\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestListsHTMLDocGen(t *testing.T) {
doc := "<ul><li>Testing 1 2 3</li> <li>FooBar</li></ul>"
expected := "// * Testing 1 2 3\n// * FooBar\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
doc = "<ul> <li>Testing 1 2 3</li> <li>FooBar</li> </ul>"
expected = "// * Testing 1 2 3\n// * FooBar\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
// Test leading spaces
doc = " <ul> <li>Testing 1 2 3</li> <li>FooBar</li> </ul>"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
// Paragraph check
doc = "<ul> <li> <p>Testing 1 2 3</p> </li><li> <p>FooBar</p></li></ul>"
expected = "// * Testing 1 2 3\n// \n// * FooBar\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestInlineCodeHTMLDocGen(t *testing.T) {
doc := "<ul> <li><code>Testing</code>: 1 2 3</li> <li>FooBar</li> </ul>"
expected := "// * Testing: 1 2 3\n// * FooBar\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestInlineCodeInParagraphHTMLDocGen(t *testing.T) {
doc := "<p><code>Testing</code>: 1 2 3</p>"
expected := "// Testing: 1 2 3\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestEmptyPREInlineCodeHTMLDocGen(t *testing.T) {
doc := "<pre><code>Testing</code></pre>"
expected := "// Testing\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestParagraph(t *testing.T) {
doc := "<p>Testing 1 2 3</p>"
expected := "// Testing 1 2 3\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}
func TestComplexListParagraphCode(t *testing.T) {
doc := "<ul> <li><p><code>FOO</code> Bar</p></li><li><p><code>Xyz</code> ABC</p></li></ul>"
expected := "// * FOO Bar\n// \n// * Xyz ABC\n"
doc = docstring(doc)
if expected != doc {
t.Errorf("Expected %s, but received %s", expected, doc)
}
}

View file

@ -0,0 +1,28 @@
// +build codegen
package api
func (a *API) suppressEventStreams() {
const eventStreamMemberName = "EventStream"
for name, op := range a.Operations {
outbound := hasEventStream(op.InputRef.Shape)
inbound := hasEventStream(op.OutputRef.Shape)
if !(outbound || inbound) {
continue
}
a.removeOperation(name)
}
}
func hasEventStream(topShape *Shape) bool {
for _, ref := range topShape.MemberRefs {
if ref.Shape.IsEventStream {
return true
}
}
return false
}

View file

@ -0,0 +1,79 @@
// +build go1.6,codegen
package api
import (
"testing"
)
func TestSuppressEventStream(t *testing.T) {
cases := []struct {
API *API
Ops []string
Shapes []string
}{
{
API: &API{
Operations: map[string]*Operation{
"abc": {
InputRef: ShapeRef{
ShapeName: "abcRequest",
},
OutputRef: ShapeRef{
ShapeName: "abcResponse",
},
},
"eventStreamOp": {
InputRef: ShapeRef{
ShapeName: "eventStreamOpRequest",
},
OutputRef: ShapeRef{
ShapeName: "eventStreamOpResponse",
},
},
},
Shapes: map[string]*Shape{
"abcRequest": {},
"abcResponse": {},
"eventStreamOpRequest": {},
"eventStreamOpResponse": {
MemberRefs: map[string]*ShapeRef{
"eventStreamShape": {
ShapeName: "eventStreamShape",
},
},
},
"eventStreamShape": {
IsEventStream: true,
},
},
},
Ops: []string{"Abc"},
Shapes: []string{"AbcInput", "AbcOutput"},
},
}
for _, c := range cases {
c.API.Setup()
if e, a := c.Ops, c.API.OperationNames(); !stringsEqual(e, a) {
t.Errorf("expect %v ops, got %v", e, a)
}
if e, a := c.Shapes, c.API.ShapeNames(); !stringsEqual(e, a) {
t.Errorf("expect %v ops, got %v", e, a)
}
}
}
func stringsEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}

View file

@ -0,0 +1,318 @@
// +build codegen
package api
import (
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/template"
"github.com/aws/aws-sdk-go/private/util"
)
type Examples map[string][]Example
// ExamplesDefinition is the structural representation of the examples-1.json file
type ExamplesDefinition struct {
*API `json:"-"`
Examples Examples `json:"examples"`
}
// Example is a single entry within the examples-1.json file.
type Example struct {
API *API `json:"-"`
Operation *Operation `json:"-"`
OperationName string `json:"-"`
Index string `json:"-"`
Builder examplesBuilder `json:"-"`
VisitedErrors map[string]struct{} `json:"-"`
Title string `json:"title"`
Description string `json:"description"`
ID string `json:"id"`
Comments Comments `json:"comments"`
Input map[string]interface{} `json:"input"`
Output map[string]interface{} `json:"output"`
}
type Comments struct {
Input map[string]interface{} `json:"input"`
Output map[string]interface{} `json:"output"`
}
var exampleFuncMap = template.FuncMap{
"commentify": commentify,
"wrap": wrap,
"generateExampleInput": generateExampleInput,
"generateTypes": generateTypes,
}
var exampleCustomizations = map[string]template.FuncMap{}
var exampleTmpls = template.Must(template.New("example").Funcs(exampleFuncMap).Parse(`
{{ generateTypes . }}
{{ commentify (wrap .Title 80 false) }}
//
{{ commentify (wrap .Description 80 false) }}
func Example{{ .API.StructName }}_{{ .MethodName }}() {
svc := {{ .API.PackageName }}.New(session.New())
input := &{{ .Operation.InputRef.Shape.GoTypeWithPkgNameElem }} {
{{ generateExampleInput . -}}
}
result, err := svc.{{ .OperationName }}(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
{{ range $_, $ref := .Operation.ErrorRefs -}}
{{ if not ($.HasVisitedError $ref) -}}
case {{ .API.PackageName }}.{{ $ref.Shape.ErrorCodeName }}:
fmt.Println({{ .API.PackageName }}.{{ $ref.Shape.ErrorCodeName }}, aerr.Error())
{{ end -}}
{{ end -}}
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
`))
// Names will return the name of the example. This will also be the name of the operation
// that is to be tested.
func (exs Examples) Names() []string {
names := make([]string, 0, len(exs))
for k := range exs {
names = append(names, k)
}
sort.Strings(names)
return names
}
func (exs Examples) GoCode() string {
buf := bytes.NewBuffer(nil)
for _, opName := range exs.Names() {
examples := exs[opName]
for _, ex := range examples {
buf.WriteString(util.GoFmt(ex.GoCode()))
buf.WriteString("\n")
}
}
return buf.String()
}
// ExampleCode will generate the example code for the given Example shape.
// TODO: Can delete
func (ex Example) GoCode() string {
var buf bytes.Buffer
m := exampleFuncMap
if fMap, ok := exampleCustomizations[ex.API.PackageName()]; ok {
m = fMap
}
tmpl := exampleTmpls.Funcs(m)
if err := tmpl.ExecuteTemplate(&buf, "example", &ex); err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
func generateExampleInput(ex Example) string {
if ex.Operation.HasInput() {
return ex.Builder.BuildShape(&ex.Operation.InputRef, ex.Input, false)
}
return ""
}
// generateTypes will generate no types for default examples, but customizations may
// require their own defined types.
func generateTypes(ex Example) string {
return ""
}
// correctType will cast the value to the correct type when printing the string.
// This is due to the json decoder choosing numbers to be floats, but the shape may
// actually be an int. To counter this, we pass the shape's type and properly do the
// casting here.
func correctType(memName string, t string, value interface{}) string {
if value == nil {
return ""
}
v := ""
switch value.(type) {
case string:
v = value.(string)
case int:
v = fmt.Sprintf("%d", value.(int))
case float64:
if t == "integer" || t == "long" || t == "int64" {
v = fmt.Sprintf("%d", int(value.(float64)))
} else {
v = fmt.Sprintf("%f", value.(float64))
}
case bool:
v = fmt.Sprintf("%t", value.(bool))
}
return convertToCorrectType(memName, t, v)
}
func convertToCorrectType(memName, t, v string) string {
return fmt.Sprintf("%s: %s,\n", memName, getValue(t, v))
}
func getValue(t, v string) string {
if t[0] == '*' {
t = t[1:]
}
switch t {
case "string":
return fmt.Sprintf("aws.String(%q)", v)
case "integer", "long", "int64":
return fmt.Sprintf("aws.Int64(%s)", v)
case "float", "float64", "double":
return fmt.Sprintf("aws.Float64(%s)", v)
case "boolean":
return fmt.Sprintf("aws.Bool(%s)", v)
default:
panic("Unsupported type: " + t)
}
}
// AttachExamples will create a new ExamplesDefinition from the examples file
// and reference the API object.
func (a *API) AttachExamples(filename string) {
p := ExamplesDefinition{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&p)
if err != nil {
panic(err)
}
p.setup()
}
var examplesBuilderCustomizations = map[string]examplesBuilder{
"wafregional": wafregionalExamplesBuilder{},
}
func (p *ExamplesDefinition) setup() {
var builder examplesBuilder
ok := false
if builder, ok = examplesBuilderCustomizations[p.API.PackageName()]; !ok {
builder = defaultExamplesBuilder{}
}
keys := p.Examples.Names()
for _, n := range keys {
examples := p.Examples[n]
for i, e := range examples {
n = p.ExportableName(n)
e.OperationName = n
e.API = p.API
e.Index = fmt.Sprintf("shared%02d", i)
e.Builder = builder
e.VisitedErrors = map[string]struct{}{}
op := p.API.Operations[e.OperationName]
e.OperationName = p.ExportableName(e.OperationName)
e.Operation = op
p.Examples[n][i] = e
}
}
p.API.Examples = p.Examples
}
var exampleHeader = template.Must(template.New("exampleHeader").Parse(`
import (
{{ .Builder.Imports .API }}
)
var _ time.Duration
var _ strings.Reader
var _ aws.Config
func parseTime(layout, value string) *time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return &t
}
`))
type exHeader struct {
Builder examplesBuilder
API *API
}
// ExamplesGoCode will return a code representation of the entry within the
// examples.json file.
func (a *API) ExamplesGoCode() string {
var buf bytes.Buffer
var builder examplesBuilder
ok := false
if builder, ok = examplesBuilderCustomizations[a.PackageName()]; !ok {
builder = defaultExamplesBuilder{}
}
if err := exampleHeader.ExecuteTemplate(&buf, "exampleHeader", &exHeader{builder, a}); err != nil {
panic(err)
}
code := a.Examples.GoCode()
if len(code) == 0 {
return ""
}
buf.WriteString(code)
return buf.String()
}
// TODO: In the operation docuentation where we list errors, this needs to be done
// there as well.
func (ex *Example) HasVisitedError(errRef *ShapeRef) bool {
errName := errRef.Shape.ErrorCodeName()
_, ok := ex.VisitedErrors[errName]
ex.VisitedErrors[errName] = struct{}{}
return ok
}
func parseTimeString(ref *ShapeRef, memName, v string) string {
if ref.Location == "header" {
return fmt.Sprintf("%s: parseTime(%q, %q),\n", memName, "Mon, 2 Jan 2006 15:04:05 GMT", v)
} else {
switch ref.API.Metadata.Protocol {
case "json", "rest-json":
return fmt.Sprintf("%s: parseTime(%q, %q),\n", memName, "2006-01-02T15:04:05Z", v)
case "rest-xml", "ec2", "query":
return fmt.Sprintf("%s: parseTime(%q, %q),\n", memName, "2006-01-02T15:04:05Z", v)
default:
panic("Unsupported time type: " + ref.API.Metadata.Protocol)
}
}
}
func (ex *Example) MethodName() string {
return fmt.Sprintf("%s_%s", ex.OperationName, ex.Index)
}

View file

@ -0,0 +1,322 @@
// +build 1.6,codegen
package api
import (
"encoding/json"
"testing"
)
func buildAPI() *API {
a := &API{}
stringShape := &Shape{
API: a,
ShapeName: "string",
Type: "string",
}
stringShapeRef := &ShapeRef{
API: a,
ShapeName: "string",
Shape: stringShape,
}
intShape := &Shape{
API: a,
ShapeName: "int",
Type: "int",
}
intShapeRef := &ShapeRef{
API: a,
ShapeName: "int",
Shape: intShape,
}
nestedComplexShape := &Shape{
API: a,
ShapeName: "NestedComplexShape",
MemberRefs: map[string]*ShapeRef{
"NestedField": stringShapeRef,
},
Type: "structure",
}
nestedComplexShapeRef := &ShapeRef{
API: a,
ShapeName: "NestedComplexShape",
Shape: nestedComplexShape,
}
nestedListShape := &Shape{
API: a,
ShapeName: "NestedListShape",
MemberRef: *nestedComplexShapeRef,
Type: "list",
}
nestedListShapeRef := &ShapeRef{
API: a,
ShapeName: "NestedListShape",
Shape: nestedListShape,
}
complexShape := &Shape{
API: a,
ShapeName: "ComplexShape",
MemberRefs: map[string]*ShapeRef{
"Field": stringShapeRef,
"List": nestedListShapeRef,
},
Type: "structure",
}
complexShapeRef := &ShapeRef{
API: a,
ShapeName: "ComplexShape",
Shape: complexShape,
}
listShape := &Shape{
API: a,
ShapeName: "ListShape",
MemberRef: *complexShapeRef,
Type: "list",
}
listShapeRef := &ShapeRef{
API: a,
ShapeName: "ListShape",
Shape: listShape,
}
listsShape := &Shape{
API: a,
ShapeName: "ListsShape",
MemberRef: *listShapeRef,
Type: "list",
}
listsShapeRef := &ShapeRef{
API: a,
ShapeName: "ListsShape",
Shape: listsShape,
}
input := &Shape{
API: a,
ShapeName: "FooInput",
MemberRefs: map[string]*ShapeRef{
"BarShape": stringShapeRef,
"ComplexField": complexShapeRef,
"ListField": listShapeRef,
"ListsField": listsShapeRef,
},
Type: "structure",
}
output := &Shape{
API: a,
ShapeName: "FooOutput",
MemberRefs: map[string]*ShapeRef{
"BazShape": intShapeRef,
"ComplexField": complexShapeRef,
"ListField": listShapeRef,
"ListsField": listsShapeRef,
},
Type: "structure",
}
inputRef := ShapeRef{
API: a,
ShapeName: "FooInput",
Shape: input,
}
outputRef := ShapeRef{
API: a,
ShapeName: "Foooutput",
Shape: output,
}
operations := map[string]*Operation{
"Foo": {
API: a,
Name: "Foo",
ExportedName: "Foo",
InputRef: inputRef,
OutputRef: outputRef,
},
}
a.Operations = operations
a.Shapes = map[string]*Shape{
"FooInput": input,
"FooOutput": output,
}
a.Metadata = Metadata{
ServiceAbbreviation: "FooService",
}
a.Setup()
return a
}
func TestExampleGeneration(t *testing.T) {
example := `
{
"version": "1.0",
"examples": {
"Foo": [
{
"input": {
"BarShape": "Hello world",
"ComplexField": {
"Field": "bar",
"List": [
{
"NestedField": "qux"
}
]
},
"ListField": [
{
"Field": "baz"
}
],
"ListsField": [
[
{
"Field": "baz"
}
]
]
},
"output": {
"BazShape": 1
},
"comments": {
"input": {
},
"output": {
}
},
"description": "Foo bar baz qux",
"title": "I pity the foo"
}
]
}
}
`
a := buildAPI()
def := &ExamplesDefinition{}
err := json.Unmarshal([]byte(example), def)
if err != nil {
t.Error(err)
}
def.API = a
def.setup()
expected := `
import (
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/fooservice"
)
var _ time.Duration
var _ strings.Reader
var _ aws.Config
func parseTime(layout, value string) *time.Time {
t, err := time.Parse(layout, value)
if err != nil {
panic(err)
}
return &t
}
// I pity the foo
//
// Foo bar baz qux
func ExampleFooService_Foo_shared00() {
svc := fooservice.New(session.New())
input := &fooservice.FooInput{
BarShape: aws.String("Hello world"),
ComplexField: &fooservice.ComplexShape{
Field: aws.String("bar"),
List: []*fooservice.NestedComplexShape{
{
NestedField: aws.String("qux"),
},
},
},
ListField: []*fooservice.ComplexShape{
{
Field: aws.String("baz"),
},
},
ListsField: [][]*fooservice.ComplexShape{
{
{
Field: aws.String("baz"),
},
},
},
}
result, err := svc.Foo(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return
}
fmt.Println(result)
}
`
if expected != a.ExamplesGoCode() {
t.Log([]byte(expected))
t.Log([]byte(a.ExamplesGoCode()))
t.Errorf("Expected:\n%s\nReceived:\n%s\n", expected, a.ExamplesGoCode())
}
}
func TestBuildShape(t *testing.T) {
a := buildAPI()
cases := []struct {
defs map[string]interface{}
expected string
}{
{
defs: map[string]interface{}{
"barShape": "Hello World",
},
expected: "BarShape: aws.String(\"Hello World\"),\n",
},
{
defs: map[string]interface{}{
"BarShape": "Hello World",
},
expected: "BarShape: aws.String(\"Hello World\"),\n",
},
}
for _, c := range cases {
ref := a.Operations["Foo"].InputRef
shapeStr := defaultExamplesBuilder{}.BuildShape(&ref, c.defs, false)
if c.expected != shapeStr {
t.Errorf("Expected:\n%s\nReceived:\n%s", c.expected, shapeStr)
}
}
}

View file

@ -0,0 +1,222 @@
// +build codegen
package api
import (
"bytes"
"fmt"
"reflect"
"sort"
"strings"
)
type examplesBuilder interface {
BuildShape(*ShapeRef, map[string]interface{}, bool) string
BuildList(string, string, *ShapeRef, []interface{}) string
BuildComplex(string, string, *ShapeRef, map[string]interface{}) string
Imports(*API) string
}
type defaultExamplesBuilder struct{}
// BuildShape will recursively build the referenced shape based on the json object
// provided.
// isMap will dictate how the field name is specified. If isMap is true, we will expect
// the member name to be quotes like "Foo".
func (builder defaultExamplesBuilder) BuildShape(ref *ShapeRef, shapes map[string]interface{}, isMap bool) string {
order := make([]string, len(shapes))
for k := range shapes {
order = append(order, k)
}
sort.Strings(order)
ret := ""
for _, name := range order {
if name == "" {
continue
}
shape := shapes[name]
// If the shape isn't a map, we want to export the value, since every field
// defined in our shapes are exported.
if len(name) > 0 && !isMap && strings.ToLower(name[0:1]) == name[0:1] {
name = strings.Title(name)
}
memName := name
passRef := ref.Shape.MemberRefs[name]
if isMap {
memName = fmt.Sprintf("%q", memName)
passRef = &ref.Shape.ValueRef
}
switch v := shape.(type) {
case map[string]interface{}:
ret += builder.BuildComplex(name, memName, passRef, v)
case []interface{}:
ret += builder.BuildList(name, memName, passRef, v)
default:
ret += builder.BuildScalar(name, memName, passRef, v, ref.Shape.Payload == name)
}
}
return ret
}
// BuildList will construct a list shape based off the service's definition
// of that list.
func (builder defaultExamplesBuilder) BuildList(name, memName string, ref *ShapeRef, v []interface{}) string {
ret := ""
if len(v) == 0 || ref == nil {
return ""
}
passRef := &ref.Shape.MemberRef
ret += fmt.Sprintf("%s: %s {\n", memName, builder.GoType(ref, false))
ret += builder.buildListElements(passRef, v)
ret += "},\n"
return ret
}
func (builder defaultExamplesBuilder) buildListElements(ref *ShapeRef, v []interface{}) string {
if len(v) == 0 || ref == nil {
return ""
}
ret := ""
format := ""
isComplex := false
isList := false
// get format for atomic type. If it is not an atomic type,
// get the element.
switch v[0].(type) {
case string:
format = "%s"
case bool:
format = "%t"
case float64:
switch ref.Shape.Type {
case "integer", "int64", "long":
format = "%d"
default:
format = "%f"
}
case []interface{}:
isList = true
case map[string]interface{}:
isComplex = true
}
for _, elem := range v {
if isComplex {
ret += fmt.Sprintf("{\n%s\n},\n", builder.BuildShape(ref, elem.(map[string]interface{}), ref.Shape.Type == "map"))
} else if isList {
ret += fmt.Sprintf("{\n%s\n},\n", builder.buildListElements(&ref.Shape.MemberRef, elem.([]interface{})))
} else {
switch ref.Shape.Type {
case "integer", "int64", "long":
elem = int(elem.(float64))
}
ret += fmt.Sprintf("%s,\n", getValue(ref.Shape.Type, fmt.Sprintf(format, elem)))
}
}
return ret
}
// BuildScalar will build atomic Go types.
func (builder defaultExamplesBuilder) BuildScalar(name, memName string, ref *ShapeRef, shape interface{}, isPayload bool) string {
if ref == nil || ref.Shape == nil {
return ""
}
switch v := shape.(type) {
case bool:
return convertToCorrectType(memName, ref.Shape.Type, fmt.Sprintf("%t", v))
case int:
if ref.Shape.Type == "timestamp" {
return parseTimeString(ref, memName, fmt.Sprintf("%d", v))
}
return convertToCorrectType(memName, ref.Shape.Type, fmt.Sprintf("%d", v))
case float64:
dataType := ref.Shape.Type
if dataType == "integer" || dataType == "int64" || dataType == "long" {
return convertToCorrectType(memName, ref.Shape.Type, fmt.Sprintf("%d", int(shape.(float64))))
}
return convertToCorrectType(memName, ref.Shape.Type, fmt.Sprintf("%f", v))
case string:
t := ref.Shape.Type
switch t {
case "timestamp":
return parseTimeString(ref, memName, fmt.Sprintf("%s", v))
case "blob":
if (ref.Streaming || ref.Shape.Streaming) && isPayload {
return fmt.Sprintf("%s: aws.ReadSeekCloser(strings.NewReader(%q)),\n", memName, v)
}
return fmt.Sprintf("%s: []byte(%q),\n", memName, v)
default:
return convertToCorrectType(memName, t, v)
}
default:
panic(fmt.Errorf("Unsupported scalar type: %v", reflect.TypeOf(v)))
}
return ""
}
func (builder defaultExamplesBuilder) BuildComplex(name, memName string, ref *ShapeRef, v map[string]interface{}) string {
switch ref.Shape.Type {
case "structure":
return fmt.Sprintf(`%s: &%s{
%s
},
`, memName, builder.GoType(ref, true), builder.BuildShape(ref, v, false))
case "map":
return fmt.Sprintf(`%s: %s{
%s
},
`, name, builder.GoType(ref, false), builder.BuildShape(ref, v, true))
default:
panic(fmt.Sprintf("Expected complex type but recieved %q", ref.Shape.Type))
}
return ""
}
func (builder defaultExamplesBuilder) GoType(ref *ShapeRef, elem bool) string {
if ref.Shape.Type != "structure" && ref.Shape.Type != "list" && ref.Shape.Type != "map" {
return ref.GoTypeWithPkgName()
}
prefix := ""
if ref.Shape.Type == "list" {
ref = &ref.Shape.MemberRef
prefix = "[]"
}
name := ref.GoTypeWithPkgName()
if elem {
name = ref.GoTypeElem()
if !strings.Contains(name, ".") {
name = strings.Join([]string{ref.API.PackageName(), name}, ".")
}
}
return prefix + name
}
func (builder defaultExamplesBuilder) Imports(a *API) string {
buf := bytes.NewBuffer(nil)
buf.WriteString(`"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
`)
buf.WriteString(fmt.Sprintf("\"%s/%s\"", "github.com/aws/aws-sdk-go/service", a.PackageName()))
return buf.String()
}

View file

@ -0,0 +1,28 @@
// +build codegen
package api
import (
"bytes"
"fmt"
)
type wafregionalExamplesBuilder struct {
defaultExamplesBuilder
}
func (builder wafregionalExamplesBuilder) Imports(a *API) string {
buf := bytes.NewBuffer(nil)
buf.WriteString(`"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/waf"
`)
buf.WriteString(fmt.Sprintf("\"%s/%s\"", "github.com/aws/aws-sdk-go/service", a.PackageName()))
return buf.String()
}

View file

@ -0,0 +1,14 @@
// +build codegen
package api
import "strings"
// ExportableName a name which is exportable as a value or name in Go code
func (a *API) ExportableName(name string) string {
if name == "" {
return name
}
return strings.ToUpper(name[0:1]) + name[1:]
}

View file

@ -0,0 +1,495 @@
package api
// shamelist is used to not rename certain operation's input and output shapes.
// We need to maintain backwards compatibility with pre-existing services. Since
// not generating unique input/output shapes is not desired, we will generate
// unique input/output shapes for new operations.
var shamelist = map[string]map[string]struct {
input bool
output bool
}{
"APIGateway": {
"CreateApiKey": {
output: true,
},
"CreateAuthorizer": {
output: true,
},
"CreateBasePathMapping": {
output: true,
},
"CreateDeployment": {
output: true,
},
"CreateDocumentationPart": {
output: true,
},
"CreateDocumentationVersion": {
output: true,
},
"CreateDomainName": {
output: true,
},
"CreateModel": {
output: true,
},
"CreateResource": {
output: true,
},
"CreateRestApi": {
output: true,
},
"CreateStage": {
output: true,
},
"CreateUsagePlan": {
output: true,
},
"CreateUsagePlanKey": {
output: true,
},
"GenerateClientCertificate": {
output: true,
},
"GetAccount": {
output: true,
},
"GetApiKey": {
output: true,
},
"GetAuthorizer": {
output: true,
},
"GetBasePathMapping": {
output: true,
},
"GetClientCertificate": {
output: true,
},
"GetDeployment": {
output: true,
},
"GetDocumentationPart": {
output: true,
},
"GetDocumentationVersion": {
output: true,
},
"GetDomainName": {
output: true,
},
"GetIntegration": {
output: true,
},
"GetIntegrationResponse": {
output: true,
},
"GetMethod": {
output: true,
},
"GetMethodResponse": {
output: true,
},
"GetModel": {
output: true,
},
"GetResource": {
output: true,
},
"GetRestApi": {
output: true,
},
"GetSdkType": {
output: true,
},
"GetStage": {
output: true,
},
"GetUsage": {
output: true,
},
"GetUsagePlan": {
output: true,
},
"GetUsagePlanKey": {
output: true,
},
"ImportRestApi": {
output: true,
},
"PutIntegration": {
output: true,
},
"PutIntegrationResponse": {
output: true,
},
"PutMethod": {
output: true,
},
"PutMethodResponse": {
output: true,
},
"PutRestApi": {
output: true,
},
"UpdateAccount": {
output: true,
},
"UpdateApiKey": {
output: true,
},
"UpdateAuthorizer": {
output: true,
},
"UpdateBasePathMapping": {
output: true,
},
"UpdateClientCertificate": {
output: true,
},
"UpdateDeployment": {
output: true,
},
"UpdateDocumentationPart": {
output: true,
},
"UpdateDocumentationVersion": {
output: true,
},
"UpdateDomainName": {
output: true,
},
"UpdateIntegration": {
output: true,
},
"UpdateIntegrationResponse": {
output: true,
},
"UpdateMethod": {
output: true,
},
"UpdateMethodResponse": {
output: true,
},
"UpdateModel": {
output: true,
},
"UpdateResource": {
output: true,
},
"UpdateRestApi": {
output: true,
},
"UpdateStage": {
output: true,
},
"UpdateUsage": {
output: true,
},
"UpdateUsagePlan": {
output: true,
},
},
"AutoScaling": {
"ResumeProcesses": {
input: true,
},
"SuspendProcesses": {
input: true,
},
},
"CognitoIdentity": {
"CreateIdentityPool": {
output: true,
},
"DescribeIdentity": {
output: true,
},
"DescribeIdentityPool": {
output: true,
},
"UpdateIdentityPool": {
input: true,
output: true,
},
},
"DirectConnect": {
"AllocateConnectionOnInterconnect": {
output: true,
},
"AllocateHostedConnection": {
output: true,
},
"AllocatePrivateVirtualInterface": {
output: true,
},
"AllocatePublicVirtualInterface": {
output: true,
},
"AssociateConnectionWithLag": {
output: true,
},
"AssociateHostedConnection": {
output: true,
},
"AssociateVirtualInterface": {
output: true,
},
"CreateConnection": {
output: true,
},
"CreateInterconnect": {
output: true,
},
"CreateLag": {
output: true,
},
"CreatePrivateVirtualInterface": {
output: true,
},
"CreatePublicVirtualInterface": {
output: true,
},
"DeleteConnection": {
output: true,
},
"DeleteLag": {
output: true,
},
"DescribeConnections": {
output: true,
},
"DescribeConnectionsOnInterconnect": {
output: true,
},
"DescribeHostedConnections": {
output: true,
},
"DescribeLoa": {
output: true,
},
"DisassociateConnectionFromLag": {
output: true,
},
"UpdateLag": {
output: true,
},
},
"EC2": {
"AttachVolume": {
output: true,
},
"CreateSnapshot": {
output: true,
},
"CreateVolume": {
output: true,
},
"DetachVolume": {
output: true,
},
"RunInstances": {
output: true,
},
},
"EFS": {
"CreateFileSystem": {
output: true,
},
"CreateMountTarget": {
output: true,
},
},
"ElastiCache": {
"AddTagsToResource": {
output: true,
},
"ListTagsForResource": {
output: true,
},
"ModifyCacheParameterGroup": {
output: true,
},
"RemoveTagsFromResource": {
output: true,
},
"ResetCacheParameterGroup": {
output: true,
},
},
"ElasticBeanstalk": {
"ComposeEnvironments": {
output: true,
},
"CreateApplication": {
output: true,
},
"CreateApplicationVersion": {
output: true,
},
"CreateConfigurationTemplate": {
output: true,
},
"CreateEnvironment": {
output: true,
},
"DescribeEnvironments": {
output: true,
},
"TerminateEnvironment": {
output: true,
},
"UpdateApplication": {
output: true,
},
"UpdateApplicationVersion": {
output: true,
},
"UpdateConfigurationTemplate": {
output: true,
},
"UpdateEnvironment": {
output: true,
},
},
"Glacier": {
"DescribeJob": {
output: true,
},
"UploadArchive": {
output: true,
},
"CompleteMultipartUpload": {
output: true,
},
},
"IAM": {
"GetContextKeysForCustomPolicy": {
output: true,
},
"GetContextKeysForPrincipalPolicy": {
output: true,
},
"SimulateCustomPolicy": {
output: true,
},
"SimulatePrincipalPolicy": {
output: true,
},
},
"Kinesis": {
"DisableEnhancedMonitoring": {
output: true,
},
"EnableEnhancedMonitoring": {
output: true,
},
},
"KMS": {
"ListGrants": {
output: true,
},
"ListRetirableGrants": {
output: true,
},
},
"Lambda": {
"CreateAlias": {
output: true,
},
"CreateEventSourceMapping": {
output: true,
},
"CreateFunction": {
output: true,
},
"DeleteEventSourceMapping": {
output: true,
},
"GetAlias": {
output: true,
},
"GetEventSourceMapping": {
output: true,
},
"GetFunctionConfiguration": {
output: true,
},
"PublishVersion": {
output: true,
},
"UpdateAlias": {
output: true,
},
"UpdateEventSourceMapping": {
output: true,
},
"UpdateFunctionCode": {
output: true,
},
"UpdateFunctionConfiguration": {
output: true,
},
},
"RDS": {
"ModifyDBClusterParameterGroup": {
output: true,
},
"ModifyDBParameterGroup": {
output: true,
},
"ResetDBClusterParameterGroup": {
output: true,
},
"ResetDBParameterGroup": {
output: true,
},
},
"Redshift": {
"DescribeLoggingStatus": {
output: true,
},
"DisableLogging": {
output: true,
},
"EnableLogging": {
output: true,
},
"ModifyClusterParameterGroup": {
output: true,
},
"ResetClusterParameterGroup": {
output: true,
},
},
"S3": {
"GetBucketNotification": {
input: true,
output: true,
},
"GetBucketNotificationConfiguration": {
input: true,
output: true,
},
},
"SWF": {
"CountClosedWorkflowExecutions": {
output: true,
},
"CountOpenWorkflowExecutions": {
output: true,
},
"CountPendingActivityTasks": {
output: true,
},
"CountPendingDecisionTasks": {
output: true,
},
"ListClosedWorkflowExecutions": {
output: true,
},
"ListOpenWorkflowExecutions": {
output: true,
},
},
}

View file

@ -0,0 +1,62 @@
// +build codegen
package api
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)
// Attach opens a file by name, and unmarshal its JSON data.
// Will proceed to setup the API if not already done so.
func (a *API) Attach(filename string) {
a.path = filepath.Dir(filename)
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
if err := json.NewDecoder(f).Decode(a); err != nil {
panic(fmt.Errorf("failed to decode %s, err: %v", filename, err))
}
}
// AttachString will unmarshal a raw JSON string, and setup the
// API if not already done so.
func (a *API) AttachString(str string) {
json.Unmarshal([]byte(str), a)
if !a.initialized {
a.Setup()
}
}
// Setup initializes the API.
func (a *API) Setup() {
a.setMetadataEndpointsKey()
a.writeShapeNames()
a.resolveReferences()
a.fixStutterNames()
a.renameExportable()
if !a.NoRenameToplevelShapes {
a.renameToplevelShapes()
}
a.renameCollidingFields()
a.updateTopLevelShapeReferences()
a.createInputOutputShapes()
a.suppressEventStreams()
a.customizationPasses()
if !a.NoRemoveUnusedShapes {
a.removeUnusedShapes()
}
if !a.NoValidataShapeMethods {
a.addShapeValidations()
}
a.initialized = true
}

View file

@ -0,0 +1,32 @@
// +build 1.6,codegen
package api
import (
"testing"
)
func TestResolvedReferences(t *testing.T) {
json := `{
"operations": {
"OperationName": {
"input": { "shape": "TestName" }
}
},
"shapes": {
"TestName": {
"type": "structure",
"members": {
"memberName1": { "shape": "OtherTest" },
"memberName2": { "shape": "OtherTest" }
}
},
"OtherTest": { "type": "string" }
}
}`
a := API{}
a.AttachString(json)
if len(a.Shapes["OtherTest"].refs) != 2 {
t.Errorf("Expected %d, but received %d", 2, len(a.Shapes["OtherTest"].refs))
}
}

View file

@ -0,0 +1,492 @@
// +build codegen
package api
import (
"bytes"
"fmt"
"regexp"
"sort"
"strings"
"text/template"
)
// An Operation defines a specific API Operation.
type Operation struct {
API *API `json:"-"`
ExportedName string
Name string
Documentation string
HTTP HTTPInfo
InputRef ShapeRef `json:"input"`
OutputRef ShapeRef `json:"output"`
ErrorRefs []ShapeRef `json:"errors"`
Paginator *Paginator
Deprecated bool `json:"deprecated"`
AuthType string `json:"authtype"`
imports map[string]bool
}
// A HTTPInfo defines the method of HTTP request for the Operation.
type HTTPInfo struct {
Method string
RequestURI string
ResponseCode uint
}
// HasInput returns if the Operation accepts an input paramater
func (o *Operation) HasInput() bool {
return o.InputRef.ShapeName != ""
}
// HasOutput returns if the Operation accepts an output parameter
func (o *Operation) HasOutput() bool {
return o.OutputRef.ShapeName != ""
}
func (o *Operation) GetSigner() string {
if o.AuthType == "v4-unsigned-body" {
o.API.imports["github.com/aws/aws-sdk-go/aws/signer/v4"] = true
}
buf := bytes.NewBuffer(nil)
switch o.AuthType {
case "none":
buf.WriteString("req.Config.Credentials = credentials.AnonymousCredentials")
case "v4-unsigned-body":
buf.WriteString("req.Handlers.Sign.Remove(v4.SignRequestHandler)\n")
buf.WriteString("handler := v4.BuildNamedHandler(\"v4.CustomSignerHandler\", v4.WithUnsignedPayload)\n")
buf.WriteString("req.Handlers.Sign.PushFrontNamed(handler)")
}
buf.WriteString("\n")
return buf.String()
}
// tplOperation defines a template for rendering an API Operation
var tplOperation = template.Must(template.New("operation").Funcs(template.FuncMap{
"GetCrosslinkURL": GetCrosslinkURL,
}).Parse(`
const op{{ .ExportedName }} = "{{ .Name }}"
// {{ .ExportedName }}Request generates a "aws/request.Request" representing the
// client's request for the {{ .ExportedName }} operation. The "output" return
// value will be populated with the request's response once the request completes
// successfuly.
//
// Use "Send" method on the returned Request to send the API call to the service.
// the "output" return value is not valid until after Send returns without error.
//
// See {{ .ExportedName }} for more information on using the {{ .ExportedName }}
// API call, and error handling.
//
// This method is useful when you want to inject custom logic or configuration
// into the SDK's request lifecycle. Such as custom headers, or retry logic.
//
//
// // Example sending a request using the {{ .ExportedName }}Request method.
// req, resp := client.{{ .ExportedName }}Request(params)
//
// err := req.Send()
// if err == nil { // resp is now filled
// fmt.Println(resp)
// }
{{ $crosslinkURL := GetCrosslinkURL $.API.BaseCrosslinkURL $.API.Metadata.UID $.ExportedName -}}
{{ if ne $crosslinkURL "" -}}
//
// See also, {{ $crosslinkURL }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}Request(` +
`input {{ .InputRef.GoType }}) (req *request.Request, output {{ .OutputRef.GoType }}) {
{{ if (or .Deprecated (or .InputRef.Deprecated .OutputRef.Deprecated)) }}if c.Client.Config.Logger != nil {
c.Client.Config.Logger.Log("This operation, {{ .ExportedName }}, has been deprecated")
}
op := &request.Operation{ {{ else }} op := &request.Operation{ {{ end }}
Name: op{{ .ExportedName }},
{{ if ne .HTTP.Method "" }}HTTPMethod: "{{ .HTTP.Method }}",
{{ end }}HTTPPath: {{ if ne .HTTP.RequestURI "" }}"{{ .HTTP.RequestURI }}"{{ else }}"/"{{ end }},
{{ if .Paginator }}Paginator: &request.Paginator{
InputTokens: {{ .Paginator.InputTokensString }},
OutputTokens: {{ .Paginator.OutputTokensString }},
LimitToken: "{{ .Paginator.LimitKey }}",
TruncationToken: "{{ .Paginator.MoreResults }}",
},
{{ end }}
}
if input == nil {
input = &{{ .InputRef.GoTypeElem }}{}
}
output = &{{ .OutputRef.GoTypeElem }}{}
req = c.newRequest(op, input, output){{ if eq .OutputRef.Shape.Placeholder true }}
req.Handlers.Unmarshal.Remove({{ .API.ProtocolPackage }}.UnmarshalHandler)
req.Handlers.Unmarshal.PushBackNamed(protocol.UnmarshalDiscardBodyHandler){{ end }}
{{ if ne .AuthType "" }}{{ .GetSigner }}{{ end -}}
return
}
// {{ .ExportedName }} API operation for {{ .API.Metadata.ServiceFullName }}.
{{ if .Documentation -}}
//
{{ .Documentation }}
{{ end -}}
//
// Returns awserr.Error for service API and SDK errors. Use runtime type assertions
// with awserr.Error's Code and Message methods to get detailed information about
// the error.
//
// See the AWS API reference guide for {{ .API.Metadata.ServiceFullName }}'s
// API operation {{ .ExportedName }} for usage and error information.
{{ if .ErrorRefs -}}
//
// Returned Error Codes:
{{ range $_, $err := .ErrorRefs -}}
// * {{ $err.Shape.ErrorCodeName }} "{{ $err.Shape.ErrorName}}"
{{ if $err.Docstring -}}
{{ $err.IndentedDocstring }}
{{ end -}}
//
{{ end -}}
{{ end -}}
{{ $crosslinkURL := GetCrosslinkURL $.API.BaseCrosslinkURL $.API.Metadata.UID $.ExportedName -}}
{{ if ne $crosslinkURL "" -}}
// See also, {{ $crosslinkURL }}
{{ end -}}
func (c *{{ .API.StructName }}) {{ .ExportedName }}(` +
`input {{ .InputRef.GoType }}) ({{ .OutputRef.GoType }}, error) {
req, out := c.{{ .ExportedName }}Request(input)
return out, req.Send()
}
// {{ .ExportedName }}WithContext is the same as {{ .ExportedName }} with the addition of
// the ability to pass a context and additional request options.
//
// See {{ .ExportedName }} for details on how to use this API operation.
//
// The context must be non-nil and will be used for request cancellation. If
// the context is nil a panic will occur. In the future the SDK may create
// sub-contexts for http.Requests. See https://golang.org/pkg/context/
// for more information on using Contexts.
func (c *{{ .API.StructName }}) {{ .ExportedName }}WithContext(` +
`ctx aws.Context, input {{ .InputRef.GoType }}, opts ...request.Option) ` +
`({{ .OutputRef.GoType }}, error) {
req, out := c.{{ .ExportedName }}Request(input)
req.SetContext(ctx)
req.ApplyOptions(opts...)
return out, req.Send()
}
{{ if .Paginator }}
// {{ .ExportedName }}Pages iterates over the pages of a {{ .ExportedName }} operation,
// calling the "fn" function with the response data for each page. To stop
// iterating, return false from the fn function.
//
// See {{ .ExportedName }} method for more information on how to use this operation.
//
// Note: This operation can generate multiple requests to a service.
//
// // Example iterating over at most 3 pages of a {{ .ExportedName }} operation.
// pageNum := 0
// err := client.{{ .ExportedName }}Pages(params,
// func(page {{ .OutputRef.GoType }}, lastPage bool) bool {
// pageNum++
// fmt.Println(page)
// return pageNum <= 3
// })
//
func (c *{{ .API.StructName }}) {{ .ExportedName }}Pages(` +
`input {{ .InputRef.GoType }}, fn func({{ .OutputRef.GoType }}, bool) bool) error {
return c.{{ .ExportedName }}PagesWithContext(aws.BackgroundContext(), input, fn)
}
// {{ .ExportedName }}PagesWithContext same as {{ .ExportedName }}Pages except
// it takes a Context and allows setting request options on the pages.
//
// The context must be non-nil and will be used for request cancellation. If
// the context is nil a panic will occur. In the future the SDK may create
// sub-contexts for http.Requests. See https://golang.org/pkg/context/
// for more information on using Contexts.
func (c *{{ .API.StructName }}) {{ .ExportedName }}PagesWithContext(` +
`ctx aws.Context, ` +
`input {{ .InputRef.GoType }}, ` +
`fn func({{ .OutputRef.GoType }}, bool) bool, ` +
`opts ...request.Option) error {
p := request.Pagination {
NewRequest: func() (*request.Request, error) {
var inCpy {{ .InputRef.GoType }}
if input != nil {
tmp := *input
inCpy = &tmp
}
req, _ := c.{{ .ExportedName }}Request(inCpy)
req.SetContext(ctx)
req.ApplyOptions(opts...)
return req, nil
},
}
cont := true
for p.Next() && cont {
cont = fn(p.Page().({{ .OutputRef.GoType }}), !p.HasNextPage())
}
return p.Err()
}
{{ end }}
`))
// GoCode returns a string of rendered GoCode for this Operation
func (o *Operation) GoCode() string {
var buf bytes.Buffer
err := tplOperation.Execute(&buf, o)
if err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
// tplInfSig defines the template for rendering an Operation's signature within an Interface definition.
var tplInfSig = template.Must(template.New("opsig").Parse(`
{{ .ExportedName }}({{ .InputRef.GoTypeWithPkgName }}) ({{ .OutputRef.GoTypeWithPkgName }}, error)
{{ .ExportedName }}WithContext(aws.Context, {{ .InputRef.GoTypeWithPkgName }}, ...request.Option) ({{ .OutputRef.GoTypeWithPkgName }}, error)
{{ .ExportedName }}Request({{ .InputRef.GoTypeWithPkgName }}) (*request.Request, {{ .OutputRef.GoTypeWithPkgName }})
{{ if .Paginator -}}
{{ .ExportedName }}Pages({{ .InputRef.GoTypeWithPkgName }}, func({{ .OutputRef.GoTypeWithPkgName }}, bool) bool) error
{{ .ExportedName }}PagesWithContext(aws.Context, {{ .InputRef.GoTypeWithPkgName }}, func({{ .OutputRef.GoTypeWithPkgName }}, bool) bool, ...request.Option) error
{{- end }}
`))
// InterfaceSignature returns a string representing the Operation's interface{}
// functional signature.
func (o *Operation) InterfaceSignature() string {
var buf bytes.Buffer
err := tplInfSig.Execute(&buf, o)
if err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
// tplExample defines the template for rendering an Operation example
var tplExample = template.Must(template.New("operationExample").Parse(`
func Example{{ .API.StructName }}_{{ .ExportedName }}() {
sess := session.Must(session.NewSession())
svc := {{ .API.PackageName }}.New(sess)
{{ .ExampleInput }}
resp, err := svc.{{ .ExportedName }}(params)
if err != nil {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
return
}
// Pretty-print the response data.
fmt.Println(resp)
}
`))
// Example returns a string of the rendered Go code for the Operation
func (o *Operation) Example() string {
var buf bytes.Buffer
err := tplExample.Execute(&buf, o)
if err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
// ExampleInput return a string of the rendered Go code for an example's input parameters
func (o *Operation) ExampleInput() string {
if len(o.InputRef.Shape.MemberRefs) == 0 {
if strings.Contains(o.InputRef.GoTypeElem(), ".") {
o.imports["github.com/aws/aws-sdk-go/service/"+strings.Split(o.InputRef.GoTypeElem(), ".")[0]] = true
return fmt.Sprintf("var params *%s", o.InputRef.GoTypeElem())
}
return fmt.Sprintf("var params *%s.%s",
o.API.PackageName(), o.InputRef.GoTypeElem())
}
e := example{o, map[string]int{}}
return "params := " + e.traverseAny(o.InputRef.Shape, false, false)
}
// A example provides
type example struct {
*Operation
visited map[string]int
}
// traverseAny returns rendered Go code for the shape.
func (e *example) traverseAny(s *Shape, required, payload bool) string {
str := ""
e.visited[s.ShapeName]++
switch s.Type {
case "structure":
str = e.traverseStruct(s, required, payload)
case "list":
str = e.traverseList(s, required, payload)
case "map":
str = e.traverseMap(s, required, payload)
case "jsonvalue":
str = "aws.JSONValue{\"key\": \"value\"}"
if required {
str += " // Required"
}
default:
str = e.traverseScalar(s, required, payload)
}
e.visited[s.ShapeName]--
return str
}
var reType = regexp.MustCompile(`\b([A-Z])`)
// traverseStruct returns rendered Go code for a structure type shape.
func (e *example) traverseStruct(s *Shape, required, payload bool) string {
var buf bytes.Buffer
if s.resolvePkg != "" {
e.imports[s.resolvePkg] = true
buf.WriteString("&" + s.GoTypeElem() + "{")
} else {
buf.WriteString("&" + s.API.PackageName() + "." + s.GoTypeElem() + "{")
}
if required {
buf.WriteString(" // Required")
}
buf.WriteString("\n")
req := make([]string, len(s.Required))
copy(req, s.Required)
sort.Strings(req)
if e.visited[s.ShapeName] < 2 {
for _, n := range req {
m := s.MemberRefs[n].Shape
p := n == s.Payload && (s.MemberRefs[n].Streaming || m.Streaming)
buf.WriteString(n + ": " + e.traverseAny(m, true, p) + ",")
if m.Type != "list" && m.Type != "structure" && m.Type != "map" {
buf.WriteString(" // Required")
}
buf.WriteString("\n")
}
for _, n := range s.MemberNames() {
if s.IsRequired(n) {
continue
}
m := s.MemberRefs[n].Shape
p := n == s.Payload && (s.MemberRefs[n].Streaming || m.Streaming)
buf.WriteString(n + ": " + e.traverseAny(m, false, p) + ",\n")
}
} else {
buf.WriteString("// Recursive values...\n")
}
buf.WriteString("}")
return buf.String()
}
// traverseMap returns rendered Go code for a map type shape.
func (e *example) traverseMap(s *Shape, required, payload bool) string {
var buf bytes.Buffer
t := ""
if s.resolvePkg != "" {
e.imports[s.resolvePkg] = true
t = s.GoTypeElem()
} else {
t = reType.ReplaceAllString(s.GoTypeElem(), s.API.PackageName()+".$1")
}
buf.WriteString(t + "{")
if required {
buf.WriteString(" // Required")
}
buf.WriteString("\n")
if e.visited[s.ShapeName] < 2 {
m := s.ValueRef.Shape
buf.WriteString("\"Key\": " + e.traverseAny(m, true, false) + ",")
if m.Type != "list" && m.Type != "structure" && m.Type != "map" {
buf.WriteString(" // Required")
}
buf.WriteString("\n// More values...\n")
} else {
buf.WriteString("// Recursive values...\n")
}
buf.WriteString("}")
return buf.String()
}
// traverseList returns rendered Go code for a list type shape.
func (e *example) traverseList(s *Shape, required, payload bool) string {
var buf bytes.Buffer
t := ""
if s.resolvePkg != "" {
e.imports[s.resolvePkg] = true
t = s.GoTypeElem()
} else {
t = reType.ReplaceAllString(s.GoTypeElem(), s.API.PackageName()+".$1")
}
buf.WriteString(t + "{")
if required {
buf.WriteString(" // Required")
}
buf.WriteString("\n")
if e.visited[s.ShapeName] < 2 {
m := s.MemberRef.Shape
buf.WriteString(e.traverseAny(m, true, false) + ",")
if m.Type != "list" && m.Type != "structure" && m.Type != "map" {
buf.WriteString(" // Required")
}
buf.WriteString("\n// More values...\n")
} else {
buf.WriteString("// Recursive values...\n")
}
buf.WriteString("}")
return buf.String()
}
// traverseScalar returns an AWS Type string representation initialized to a value.
// Will panic if s is an unsupported shape type.
func (e *example) traverseScalar(s *Shape, required, payload bool) string {
str := ""
switch s.Type {
case "integer", "long":
str = `aws.Int64(1)`
case "float", "double":
str = `aws.Float64(1.0)`
case "string", "character":
str = `aws.String("` + s.ShapeName + `")`
case "blob":
if payload {
str = `bytes.NewReader([]byte("PAYLOAD"))`
} else {
str = `[]byte("PAYLOAD")`
}
case "boolean":
str = `aws.Bool(true)`
case "timestamp":
str = `aws.Time(time.Now())`
default:
panic("unsupported shape " + s.Type)
}
return str
}

View file

@ -0,0 +1,91 @@
// +build codegen
package api
import (
"encoding/json"
"fmt"
"os"
)
// Paginator keeps track of pagination configuration for an API operation.
type Paginator struct {
InputTokens interface{} `json:"input_token"`
OutputTokens interface{} `json:"output_token"`
LimitKey string `json:"limit_key"`
MoreResults string `json:"more_results"`
}
// InputTokensString returns output tokens formatted as a list
func (p *Paginator) InputTokensString() string {
str := p.InputTokens.([]string)
return fmt.Sprintf("%#v", str)
}
// OutputTokensString returns output tokens formatted as a list
func (p *Paginator) OutputTokensString() string {
str := p.OutputTokens.([]string)
return fmt.Sprintf("%#v", str)
}
// used for unmarshaling from the paginators JSON file
type paginationDefinitions struct {
*API
Pagination map[string]Paginator
}
// AttachPaginators attaches pagination configuration from filename to the API.
func (a *API) AttachPaginators(filename string) {
p := paginationDefinitions{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&p)
if err != nil {
panic(err)
}
p.setup()
}
// setup runs post-processing on the paginator configuration.
func (p *paginationDefinitions) setup() {
for n, e := range p.Pagination {
if e.InputTokens == nil || e.OutputTokens == nil {
continue
}
paginator := e
switch t := paginator.InputTokens.(type) {
case string:
paginator.InputTokens = []string{t}
case []interface{}:
toks := []string{}
for _, e := range t {
s := e.(string)
toks = append(toks, s)
}
paginator.InputTokens = toks
}
switch t := paginator.OutputTokens.(type) {
case string:
paginator.OutputTokens = []string{t}
case []interface{}:
toks := []string{}
for _, e := range t {
s := e.(string)
toks = append(toks, s)
}
paginator.OutputTokens = toks
}
if o, ok := p.Operations[n]; ok {
o.Paginator = &paginator
} else {
panic("unknown operation for paginator " + n)
}
}
}

View file

@ -0,0 +1,147 @@
// +build codegen
package api
import (
"fmt"
"encoding/json"
"reflect"
"strings"
"github.com/aws/aws-sdk-go/private/util"
)
// A paramFiller provides string formatting for a shape and its types.
type paramFiller struct {
prefixPackageName bool
}
// typeName returns the type name of a shape.
func (f paramFiller) typeName(shape *Shape) string {
if f.prefixPackageName && shape.Type == "structure" {
return "*" + shape.API.PackageName() + "." + shape.GoTypeElem()
}
return shape.GoType()
}
// ParamsStructFromJSON returns a JSON string representation of a structure.
func ParamsStructFromJSON(value interface{}, shape *Shape, prefixPackageName bool) string {
f := paramFiller{prefixPackageName: prefixPackageName}
return util.GoFmt(f.paramsStructAny(value, shape))
}
// paramsStructAny returns the string representation of any value.
func (f paramFiller) paramsStructAny(value interface{}, shape *Shape) string {
if value == nil {
return ""
}
switch shape.Type {
case "structure":
if value != nil {
vmap := value.(map[string]interface{})
return f.paramsStructStruct(vmap, shape)
}
case "list":
vlist := value.([]interface{})
return f.paramsStructList(vlist, shape)
case "map":
vmap := value.(map[string]interface{})
return f.paramsStructMap(vmap, shape)
case "string", "character":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.String(%#v)", v.Interface())
}
case "blob":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() && shape.Streaming {
return fmt.Sprintf("bytes.NewReader([]byte(%#v))", v.Interface())
} else if v.IsValid() {
return fmt.Sprintf("[]byte(%#v)", v.Interface())
}
case "boolean":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.Bool(%#v)", v.Interface())
}
case "integer", "long":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.Int64(%v)", v.Interface())
}
case "float", "double":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.Float64(%v)", v.Interface())
}
case "timestamp":
v := reflect.Indirect(reflect.ValueOf(value))
if v.IsValid() {
return fmt.Sprintf("aws.Time(time.Unix(%d, 0))", int(v.Float()))
}
case "jsonvalue":
v, err := json.Marshal(value)
if err != nil {
panic("failed to marshal JSONValue, "+err.Error())
}
const tmpl = `func() aws.JSONValue {
var m aws.JSONValue
if err := json.Unmarshal([]byte(%q), &m); err != nil {
panic("failed to unmarshal JSONValue, "+err.Error())
}
return m
}()`
return fmt.Sprintf(tmpl, string(v))
default:
panic("Unhandled type " + shape.Type)
}
return ""
}
// paramsStructStruct returns the string representation of a structure
func (f paramFiller) paramsStructStruct(value map[string]interface{}, shape *Shape) string {
out := "&" + f.typeName(shape)[1:] + "{\n"
for _, n := range shape.MemberNames() {
ref := shape.MemberRefs[n]
name := findParamMember(value, n)
if val := f.paramsStructAny(value[name], ref.Shape); val != "" {
out += fmt.Sprintf("%s: %s,\n", n, val)
}
}
out += "}"
return out
}
// paramsStructMap returns the string representation of a map of values
func (f paramFiller) paramsStructMap(value map[string]interface{}, shape *Shape) string {
out := f.typeName(shape) + "{\n"
keys := util.SortedKeys(value)
for _, k := range keys {
v := value[k]
out += fmt.Sprintf("%q: %s,\n", k, f.paramsStructAny(v, shape.ValueRef.Shape))
}
out += "}"
return out
}
// paramsStructList returns the string representation of slice of values
func (f paramFiller) paramsStructList(value []interface{}, shape *Shape) string {
out := f.typeName(shape) + "{\n"
for _, v := range value {
out += fmt.Sprintf("%s,\n", f.paramsStructAny(v, shape.MemberRef.Shape))
}
out += "}"
return out
}
// findParamMember searches a map for a key ignoring case. Returns the map key if found.
func findParamMember(value map[string]interface{}, key string) string {
for actualKey := range value {
if strings.ToLower(key) == strings.ToLower(actualKey) {
return actualKey
}
}
return ""
}

View file

@ -0,0 +1,354 @@
// +build codegen
package api
import (
"fmt"
"regexp"
"strings"
)
// updateTopLevelShapeReferences moves resultWrapper, locationName, and
// xmlNamespace traits from toplevel shape references to the toplevel
// shapes for easier code generation
func (a *API) updateTopLevelShapeReferences() {
for _, o := range a.Operations {
// these are for REST-XML services
if o.InputRef.LocationName != "" {
o.InputRef.Shape.LocationName = o.InputRef.LocationName
}
if o.InputRef.Location != "" {
o.InputRef.Shape.Location = o.InputRef.Location
}
if o.InputRef.Payload != "" {
o.InputRef.Shape.Payload = o.InputRef.Payload
}
if o.InputRef.XMLNamespace.Prefix != "" {
o.InputRef.Shape.XMLNamespace.Prefix = o.InputRef.XMLNamespace.Prefix
}
if o.InputRef.XMLNamespace.URI != "" {
o.InputRef.Shape.XMLNamespace.URI = o.InputRef.XMLNamespace.URI
}
}
}
// writeShapeNames sets each shape's API and shape name values. Binding the
// shape to its parent API.
func (a *API) writeShapeNames() {
for n, s := range a.Shapes {
s.API = a
s.ShapeName = n
}
}
func (a *API) resolveReferences() {
resolver := referenceResolver{API: a, visited: map[*ShapeRef]bool{}}
for _, s := range a.Shapes {
resolver.resolveShape(s)
}
for _, o := range a.Operations {
o.API = a // resolve parent reference
resolver.resolveReference(&o.InputRef)
resolver.resolveReference(&o.OutputRef)
// Resolve references for errors also
for i := range o.ErrorRefs {
resolver.resolveReference(&o.ErrorRefs[i])
o.ErrorRefs[i].Shape.IsError = true
}
}
}
// A referenceResolver provides a way to resolve shape references to
// shape definitions.
type referenceResolver struct {
*API
visited map[*ShapeRef]bool
}
var jsonvalueShape = &Shape{
ShapeName: "JSONValue",
Type: "jsonvalue",
ValueRef: ShapeRef{
JSONValue: true,
},
}
// resolveReference updates a shape reference to reference the API and
// its shape definition. All other nested references are also resolved.
func (r *referenceResolver) resolveReference(ref *ShapeRef) {
if ref.ShapeName == "" {
return
}
shape, ok := r.API.Shapes[ref.ShapeName]
if !ok {
panic(fmt.Sprintf("unable resolve reference, %s", ref.ShapeName))
return
}
if ref.JSONValue {
ref.ShapeName = "JSONValue"
r.API.Shapes[ref.ShapeName] = jsonvalueShape
}
ref.API = r.API // resolve reference back to API
ref.Shape = shape // resolve shape reference
if r.visited[ref] {
return
}
r.visited[ref] = true
shape.refs = append(shape.refs, ref) // register the ref
// resolve shape's references, if it has any
r.resolveShape(shape)
}
// resolveShape resolves a shape's Member Key Value, and nested member
// shape references.
func (r *referenceResolver) resolveShape(shape *Shape) {
r.resolveReference(&shape.MemberRef)
r.resolveReference(&shape.KeyRef)
r.resolveReference(&shape.ValueRef)
for _, m := range shape.MemberRefs {
r.resolveReference(m)
}
}
// renameToplevelShapes renames all top level shapes of an API to their
// exportable variant. The shapes are also updated to include notations
// if they are Input or Outputs.
func (a *API) renameToplevelShapes() {
for _, v := range a.OperationList() {
if v.HasInput() {
name := v.ExportedName + "Input"
switch {
case a.Shapes[name] == nil:
if service, ok := shamelist[a.name]; ok {
if check, ok := service[v.Name]; ok && check.input {
break
}
}
v.InputRef.Shape.Rename(name)
}
}
if v.HasOutput() {
name := v.ExportedName + "Output"
switch {
case a.Shapes[name] == nil:
if service, ok := shamelist[a.name]; ok {
if check, ok := service[v.Name]; ok && check.output {
break
}
}
v.OutputRef.Shape.Rename(name)
}
}
v.InputRef.Payload = a.ExportableName(v.InputRef.Payload)
v.OutputRef.Payload = a.ExportableName(v.OutputRef.Payload)
}
}
// fixStutterNames fixes all name struttering based on Go naming conventions.
// "Stuttering" is when the prefix of a structure or function matches the
// package name (case insensitive).
func (a *API) fixStutterNames() {
str, end := a.StructName(), ""
if len(str) > 1 {
l := len(str) - 1
str, end = str[0:l], str[l:]
}
re := regexp.MustCompile(fmt.Sprintf(`\A(?i:%s)%s`, str, end))
for name, op := range a.Operations {
newName := re.ReplaceAllString(name, "")
if newName != name && len(newName) > 0 {
delete(a.Operations, name)
a.Operations[newName] = op
}
op.ExportedName = newName
}
for k, s := range a.Shapes {
newName := re.ReplaceAllString(k, "")
if newName != s.ShapeName && len(newName) > 0 {
s.Rename(newName)
}
}
}
// renameExportable renames all operation names to be exportable names.
// All nested Shape names are also updated to the exportable variant.
func (a *API) renameExportable() {
for name, op := range a.Operations {
newName := a.ExportableName(name)
if newName != name {
delete(a.Operations, name)
a.Operations[newName] = op
}
op.ExportedName = newName
}
for k, s := range a.Shapes {
// FIXME SNS has lower and uppercased shape names with the same name,
// except the lowercased variant is used exclusively for string and
// other primitive types. Renaming both would cause a collision.
// We work around this by only renaming the structure shapes.
if s.Type == "string" {
continue
}
for mName, member := range s.MemberRefs {
newName := a.ExportableName(mName)
if newName != mName {
delete(s.MemberRefs, mName)
s.MemberRefs[newName] = member
// also apply locationName trait so we keep the old one
// but only if there's no locationName trait on ref or shape
if member.LocationName == "" && member.Shape.LocationName == "" {
member.LocationName = mName
}
}
if newName == "_" {
panic("Shape " + s.ShapeName + " uses reserved member name '_'")
}
}
newName := a.ExportableName(k)
if newName != s.ShapeName {
s.Rename(newName)
}
s.Payload = a.ExportableName(s.Payload)
// fix required trait names
for i, n := range s.Required {
s.Required[i] = a.ExportableName(n)
}
}
for _, s := range a.Shapes {
// fix enum names
if s.IsEnum() {
s.EnumConsts = make([]string, len(s.Enum))
for i := range s.Enum {
shape := s.ShapeName
shape = strings.ToUpper(shape[0:1]) + shape[1:]
s.EnumConsts[i] = shape + s.EnumName(i)
}
}
}
}
// renameCollidingFields will rename any fields that uses an SDK or Golang
// specific name.
func (a *API) renameCollidingFields() {
for _, v := range a.Shapes {
namesWithSet := map[string]struct{}{}
for k, field := range v.MemberRefs {
if strings.HasPrefix(k, "Set") {
namesWithSet[k] = struct{}{}
}
if collides(k) {
renameCollidingField(k, v, field)
}
}
// checks if any field names collide with setters.
for name := range namesWithSet {
if field, ok := v.MemberRefs["Set"+name]; ok {
renameCollidingField(name, v, field)
}
}
}
}
func renameCollidingField(name string, v *Shape, field *ShapeRef) {
newName := name + "_"
fmt.Printf("Shape %s's field %q renamed to %q\n", v.ShapeName, name, newName)
delete(v.MemberRefs, name)
v.MemberRefs[newName] = field
}
// collides will return true if it is a name used by the SDK or Golang.
func collides(name string) bool {
switch name {
case "String",
"GoString",
"Validate":
return true
default:
return false
}
}
// createInputOutputShapes creates toplevel input/output shapes if they
// have not been defined in the API. This normalizes all APIs to always
// have an input and output structure in the signature.
func (a *API) createInputOutputShapes() {
for _, op := range a.Operations {
if !op.HasInput() {
setAsPlacholderShape(&op.InputRef, op.ExportedName+"Input", a)
}
if !op.HasOutput() {
setAsPlacholderShape(&op.OutputRef, op.ExportedName+"Output", a)
}
}
}
func setAsPlacholderShape(tgtShapeRef *ShapeRef, name string, a *API) {
shape := a.makeIOShape(name)
shape.Placeholder = true
*tgtShapeRef = ShapeRef{API: a, ShapeName: shape.ShapeName, Shape: shape}
shape.refs = append(shape.refs, tgtShapeRef)
}
// makeIOShape returns a pointer to a new Shape initialized by the name provided.
func (a *API) makeIOShape(name string) *Shape {
shape := &Shape{
API: a, ShapeName: name, Type: "structure",
MemberRefs: map[string]*ShapeRef{},
}
a.Shapes[name] = shape
return shape
}
// removeUnusedShapes removes shapes from the API which are not referenced by any
// other shape in the API.
func (a *API) removeUnusedShapes() {
for _, s := range a.Shapes {
if len(s.refs) == 0 {
a.removeShape(s)
}
}
}
// Represents the service package name to EndpointsID mapping
var custEndpointsKey = map[string]string{
"applicationautoscaling": "application-autoscaling",
}
// Sents the EndpointsID field of Metadata with the value of the
// EndpointPrefix if EndpointsID is not set. Also adds
// customizations for services if EndpointPrefix is not a valid key.
func (a *API) setMetadataEndpointsKey() {
if len(a.Metadata.EndpointsID) != 0 {
return
}
if v, ok := custEndpointsKey[a.PackageName()]; ok {
a.Metadata.EndpointsID = v
} else {
a.Metadata.EndpointsID = a.Metadata.EndpointPrefix
}
}

View file

@ -0,0 +1,211 @@
// +build 1.6,codegen
package api
import (
"testing"
)
func TestUniqueInputAndOutputs(t *testing.T) {
shamelist["FooService"] = map[string]struct {
input bool
output bool
}{}
v := shamelist["FooService"]["OpOutputNoRename"]
v.output = true
shamelist["FooService"]["OpOutputNoRename"] = v
v = shamelist["FooService"]["InputNoRename"]
v.input = true
shamelist["FooService"]["OpInputNoRename"] = v
v = shamelist["FooService"]["BothNoRename"]
v.input = true
v.output = true
shamelist["FooService"]["OpBothNoRename"] = v
cases := [][]struct {
expectedInput string
expectedOutput string
operation string
input string
inputRef string
output string
outputRef string
}{
{
{
expectedInput: "FooOperationInput",
expectedOutput: "FooOperationOutput",
operation: "FooOperation",
input: "FooInputShape",
inputRef: "FooInputShapeRef",
output: "FooOutputShape",
outputRef: "FooOutputShapeRef",
},
{
expectedInput: "BarOperationInput",
expectedOutput: "BarOperationOutput",
operation: "BarOperation",
input: "FooInputShape",
inputRef: "FooInputShapeRef",
output: "FooOutputShape",
outputRef: "FooOutputShapeRef",
},
},
{
{
expectedInput: "FooOperationInput",
expectedOutput: "FooOperationOutput",
operation: "FooOperation",
input: "FooInputShape",
inputRef: "FooInputShapeRef",
output: "FooOutputShape",
outputRef: "FooOutputShapeRef",
},
{
expectedInput: "OpOutputNoRenameInput",
expectedOutput: "OpOutputNoRenameOutputShape",
operation: "OpOutputNoRename",
input: "OpOutputNoRenameInputShape",
inputRef: "OpOutputNoRenameInputRef",
output: "OpOutputNoRenameOutputShape",
outputRef: "OpOutputNoRenameOutputRef",
},
},
{
{
expectedInput: "FooOperationInput",
expectedOutput: "FooOperationOutput",
operation: "FooOperation",
input: "FooInputShape",
inputRef: "FooInputShapeRef",
output: "FooOutputShape",
outputRef: "FooOutputShapeRef",
},
{
expectedInput: "OpInputNoRenameInputShape",
expectedOutput: "OpInputNoRenameOutput",
operation: "OpInputNoRename",
input: "OpInputNoRenameInputShape",
inputRef: "OpInputNoRenameInputRef",
output: "OpInputNoRenameOutputShape",
outputRef: "OpInputNoRenameOutputRef",
},
},
{
{
expectedInput: "FooOperationInput",
expectedOutput: "FooOperationOutput",
operation: "FooOperation",
input: "FooInputShape",
inputRef: "FooInputShapeRef",
output: "FooOutputShape",
outputRef: "FooOutputShapeRef",
},
{
expectedInput: "OpInputNoRenameInputShape",
expectedOutput: "OpInputNoRenameOutputShape",
operation: "OpBothNoRename",
input: "OpInputNoRenameInputShape",
inputRef: "OpInputNoRenameInputRef",
output: "OpInputNoRenameOutputShape",
outputRef: "OpInputNoRenameOutputRef",
},
},
}
for _, c := range cases {
a := &API{
name: "FooService",
Operations: map[string]*Operation{},
}
expected := map[string][]string{}
a.Shapes = map[string]*Shape{}
for _, op := range c {
a.Operations[op.operation] = &Operation{
ExportedName: op.operation,
}
a.Operations[op.operation].Name = op.operation
a.Operations[op.operation].InputRef = ShapeRef{
API: a,
ShapeName: op.inputRef,
Shape: &Shape{
API: a,
ShapeName: op.input,
},
}
a.Operations[op.operation].OutputRef = ShapeRef{
API: a,
ShapeName: op.outputRef,
Shape: &Shape{
API: a,
ShapeName: op.output,
},
}
a.Shapes[op.input] = &Shape{
ShapeName: op.input,
}
a.Shapes[op.output] = &Shape{
ShapeName: op.output,
}
expected[op.operation] = append(expected[op.operation], op.expectedInput)
expected[op.operation] = append(expected[op.operation], op.expectedOutput)
}
a.fixStutterNames()
a.renameToplevelShapes()
for k, v := range expected {
if a.Operations[k].InputRef.Shape.ShapeName != v[0] {
t.Errorf("Error %d case: Expected %q, but received %q", k, v[0], a.Operations[k].InputRef.Shape.ShapeName)
}
if a.Operations[k].OutputRef.Shape.ShapeName != v[1] {
t.Errorf("Error %d case: Expected %q, but received %q", k, v[1], a.Operations[k].OutputRef.Shape.ShapeName)
}
}
}
}
func TestCollidingFields(t *testing.T) {
cases := []struct {
api *API
expected []*Shapes
}{
{
&API{
name: "FooService",
Shapes: []*Shapes{
{
MemberRefs: map[string]*ShapeRef{
"String": &ShapeRef{},
"GoString": &ShapeRef{},
"Validate": &ShapeRef{},
"Foo": &ShapeRef{},
"SetFoo": &ShapeRef{},
},
},
},
},
[]*Shapes{
{
MemberRefs: map[string]*ShapeRef{
"String_": &ShapeRef{},
"GoString_": &ShapeRef{},
"Validate_": &ShapeRef{},
"Foo": &ShapeRef{},
"SetFoo_": &ShapeRef{},
},
},
},
},
}
for _, c := range testCases {
c.api.renameCollidingFields()
if !reflect.DeepEqual(c.api.Shapes, c.expected) {
t.Errorf("expected %v, but received %v", c.expected, c.api.Shapes)
}
}
}

View file

@ -0,0 +1,675 @@
// +build codegen
package api
import (
"bytes"
"fmt"
"path"
"regexp"
"sort"
"strings"
"text/template"
)
// A ShapeRef defines the usage of a shape within the API.
type ShapeRef struct {
API *API `json:"-"`
Shape *Shape `json:"-"`
Documentation string
ShapeName string `json:"shape"`
Location string
LocationName string
QueryName string
Flattened bool
Streaming bool
XMLAttribute bool
// Ignore, if set, will not be sent over the wire
Ignore bool
XMLNamespace XMLInfo
Payload string
IdempotencyToken bool `json:"idempotencyToken"`
JSONValue bool `json:"jsonvalue"`
Deprecated bool `json:"deprecated"`
OrigShapeName string `json:"-"`
GenerateGetter bool
}
// ErrorInfo represents the error block of a shape's structure
type ErrorInfo struct {
Code string
HTTPStatusCode int
}
// A XMLInfo defines URL and prefix for Shapes when rendered as XML
type XMLInfo struct {
Prefix string
URI string
}
// A Shape defines the definition of a shape type
type Shape struct {
API *API `json:"-"`
ShapeName string
Documentation string
MemberRefs map[string]*ShapeRef `json:"members"`
MemberRef ShapeRef `json:"member"` // List ref
KeyRef ShapeRef `json:"key"` // map key ref
ValueRef ShapeRef `json:"value"` // map value ref
Required []string
Payload string
Type string
Exception bool
Enum []string
EnumConsts []string
Flattened bool
Streaming bool
Location string
LocationName string
IdempotencyToken bool `json:"idempotencyToken"`
XMLNamespace XMLInfo
Min float64 // optional Minimum length (string, list) or value (number)
Max float64 // optional Maximum length (string, list) or value (number)
IsEventStream bool `json:"eventstream"`
refs []*ShapeRef // References to this shape
resolvePkg string // use this package in the goType() if present
OrigShapeName string `json:"-"`
// Defines if the shape is a placeholder and should not be used directly
Placeholder bool
Deprecated bool `json:"deprecated"`
Validations ShapeValidations
// Error information that is set if the shape is an error shape.
IsError bool
ErrorInfo ErrorInfo `json:"error"`
}
// ErrorCodeName will return the error shape's name formated for
// error code const.
func (s *Shape) ErrorCodeName() string {
return "ErrCode" + s.ShapeName
}
// ErrorName will return the shape's name or error code if available based
// on the API's protocol. This is the error code string returned by the service.
func (s *Shape) ErrorName() string {
name := s.ShapeName
switch s.API.Metadata.Protocol {
case "query", "ec2query", "rest-xml":
if len(s.ErrorInfo.Code) > 0 {
name = s.ErrorInfo.Code
}
}
return name
}
// GoTags returns the struct tags for a shape.
func (s *Shape) GoTags(root, required bool) string {
ref := &ShapeRef{ShapeName: s.ShapeName, API: s.API, Shape: s}
return ref.GoTags(root, required)
}
// Rename changes the name of the Shape to newName. Also updates
// the associated API's reference to use newName.
func (s *Shape) Rename(newName string) {
for _, r := range s.refs {
r.OrigShapeName = r.ShapeName
r.ShapeName = newName
}
delete(s.API.Shapes, s.ShapeName)
s.OrigShapeName = s.ShapeName
s.API.Shapes[newName] = s
s.ShapeName = newName
}
// MemberNames returns a slice of struct member names.
func (s *Shape) MemberNames() []string {
i, names := 0, make([]string, len(s.MemberRefs))
for n := range s.MemberRefs {
names[i] = n
i++
}
sort.Strings(names)
return names
}
// GoTypeWithPkgName returns a shape's type as a string with the package name in
// <packageName>.<type> format. Package naming only applies to structures.
func (s *Shape) GoTypeWithPkgName() string {
return goType(s, true)
}
func (s *Shape) GoTypeWithPkgNameElem() string {
t := goType(s, true)
if strings.HasPrefix(t, "*") {
return t[1:]
}
return t
}
// GenAccessors returns if the shape's reference should have setters generated.
func (s *ShapeRef) UseIndirection() bool {
switch s.Shape.Type {
case "map", "list", "blob", "structure", "jsonvalue":
return false
}
if s.Streaming || s.Shape.Streaming {
return false
}
if s.JSONValue {
return false
}
return true
}
// GoStructValueType returns the Shape's Go type value instead of a pointer
// for the type.
func (s *Shape) GoStructValueType(name string, ref *ShapeRef) string {
v := s.GoStructType(name, ref)
if ref.UseIndirection() && v[0] == '*' {
return v[1:]
}
return v
}
// GoStructType returns the type of a struct field based on the API
// model definition.
func (s *Shape) GoStructType(name string, ref *ShapeRef) string {
if (ref.Streaming || ref.Shape.Streaming) && s.Payload == name {
rtype := "io.ReadSeeker"
if strings.HasSuffix(s.ShapeName, "Output") {
rtype = "io.ReadCloser"
}
s.API.imports["io"] = true
return rtype
}
if ref.JSONValue {
s.API.imports["github.com/aws/aws-sdk-go/aws"] = true
return "aws.JSONValue"
}
for _, v := range s.Validations {
// TODO move this to shape validation resolution
if (v.Ref.Shape.Type == "map" || v.Ref.Shape.Type == "list") && v.Type == ShapeValidationNested {
s.API.imports["fmt"] = true
}
}
return ref.GoType()
}
// GoType returns a shape's Go type
func (s *Shape) GoType() string {
return goType(s, false)
}
// GoType returns a shape ref's Go type.
func (ref *ShapeRef) GoType() string {
if ref.Shape == nil {
panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
}
return ref.Shape.GoType()
}
// GoTypeWithPkgName returns a shape's type as a string with the package name in
// <packageName>.<type> format. Package naming only applies to structures.
func (ref *ShapeRef) GoTypeWithPkgName() string {
if ref.Shape == nil {
panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
}
return ref.Shape.GoTypeWithPkgName()
}
// Returns a string version of the Shape's type.
// If withPkgName is true, the package name will be added as a prefix
func goType(s *Shape, withPkgName bool) string {
switch s.Type {
case "structure":
if withPkgName || s.resolvePkg != "" {
pkg := s.resolvePkg
if pkg != "" {
s.API.imports[pkg] = true
pkg = path.Base(pkg)
} else {
pkg = s.API.PackageName()
}
return fmt.Sprintf("*%s.%s", pkg, s.ShapeName)
}
return "*" + s.ShapeName
case "map":
return "map[string]" + goType(s.ValueRef.Shape, withPkgName)
case "jsonvalue":
return "aws.JSONValue"
case "list":
return "[]" + goType(s.MemberRef.Shape, withPkgName)
case "boolean":
return "*bool"
case "string", "character":
return "*string"
case "blob":
return "[]byte"
case "integer", "long":
return "*int64"
case "float", "double":
return "*float64"
case "timestamp":
s.API.imports["time"] = true
return "*time.Time"
default:
panic("Unsupported shape type: " + s.Type)
}
}
// GoTypeElem returns the Go type for the Shape. If the shape type is a pointer just
// the type will be returned minus the pointer *.
func (s *Shape) GoTypeElem() string {
t := s.GoType()
if strings.HasPrefix(t, "*") {
return t[1:]
}
return t
}
// GoTypeElem returns the Go type for the Shape. If the shape type is a pointer just
// the type will be returned minus the pointer *.
func (ref *ShapeRef) GoTypeElem() string {
if ref.Shape == nil {
panic(fmt.Errorf("missing shape definition on reference for %#v", ref))
}
return ref.Shape.GoTypeElem()
}
// ShapeTag is a struct tag that will be applied to a shape's generated code
type ShapeTag struct {
Key, Val string
}
// String returns the string representation of the shape tag
func (s ShapeTag) String() string {
return fmt.Sprintf(`%s:"%s"`, s.Key, s.Val)
}
// ShapeTags is a collection of shape tags and provides serialization of the
// tags in an ordered list.
type ShapeTags []ShapeTag
// Join returns an ordered serialization of the shape tags with the provided
// separator.
func (s ShapeTags) Join(sep string) string {
o := &bytes.Buffer{}
for i, t := range s {
o.WriteString(t.String())
if i < len(s)-1 {
o.WriteString(sep)
}
}
return o.String()
}
// String is an alias for Join with the empty space separator.
func (s ShapeTags) String() string {
return s.Join(" ")
}
// GoTags returns the rendered tags string for the ShapeRef
func (ref *ShapeRef) GoTags(toplevel bool, isRequired bool) string {
tags := ShapeTags{}
if ref.Location != "" {
tags = append(tags, ShapeTag{"location", ref.Location})
} else if ref.Shape.Location != "" {
tags = append(tags, ShapeTag{"location", ref.Shape.Location})
}
if ref.LocationName != "" {
tags = append(tags, ShapeTag{"locationName", ref.LocationName})
} else if ref.Shape.LocationName != "" {
tags = append(tags, ShapeTag{"locationName", ref.Shape.LocationName})
}
if ref.QueryName != "" {
tags = append(tags, ShapeTag{"queryName", ref.QueryName})
}
if ref.Shape.MemberRef.LocationName != "" {
tags = append(tags, ShapeTag{"locationNameList", ref.Shape.MemberRef.LocationName})
}
if ref.Shape.KeyRef.LocationName != "" {
tags = append(tags, ShapeTag{"locationNameKey", ref.Shape.KeyRef.LocationName})
}
if ref.Shape.ValueRef.LocationName != "" {
tags = append(tags, ShapeTag{"locationNameValue", ref.Shape.ValueRef.LocationName})
}
if ref.Shape.Min > 0 {
tags = append(tags, ShapeTag{"min", fmt.Sprintf("%v", ref.Shape.Min)})
}
if ref.Deprecated || ref.Shape.Deprecated {
tags = append(tags, ShapeTag{"deprecated", "true"})
}
// All shapes have a type
tags = append(tags, ShapeTag{"type", ref.Shape.Type})
// embed the timestamp type for easier lookups
if ref.Shape.Type == "timestamp" {
t := ShapeTag{Key: "timestampFormat"}
if ref.Location == "header" {
t.Val = "rfc822"
} else {
switch ref.API.Metadata.Protocol {
case "json", "rest-json":
t.Val = "unix"
case "rest-xml", "ec2", "query":
t.Val = "iso8601"
}
}
tags = append(tags, t)
}
if ref.Shape.Flattened || ref.Flattened {
tags = append(tags, ShapeTag{"flattened", "true"})
}
if ref.XMLAttribute {
tags = append(tags, ShapeTag{"xmlAttribute", "true"})
}
if isRequired {
tags = append(tags, ShapeTag{"required", "true"})
}
if ref.Shape.IsEnum() {
tags = append(tags, ShapeTag{"enum", ref.ShapeName})
}
if toplevel {
if ref.Shape.Payload != "" {
tags = append(tags, ShapeTag{"payload", ref.Shape.Payload})
}
}
if ref.XMLNamespace.Prefix != "" {
tags = append(tags, ShapeTag{"xmlPrefix", ref.XMLNamespace.Prefix})
} else if ref.Shape.XMLNamespace.Prefix != "" {
tags = append(tags, ShapeTag{"xmlPrefix", ref.Shape.XMLNamespace.Prefix})
}
if ref.XMLNamespace.URI != "" {
tags = append(tags, ShapeTag{"xmlURI", ref.XMLNamespace.URI})
} else if ref.Shape.XMLNamespace.URI != "" {
tags = append(tags, ShapeTag{"xmlURI", ref.Shape.XMLNamespace.URI})
}
if ref.IdempotencyToken || ref.Shape.IdempotencyToken {
tags = append(tags, ShapeTag{"idempotencyToken", "true"})
}
if ref.Ignore {
tags = append(tags, ShapeTag{"ignore", "true"})
}
return fmt.Sprintf("`%s`", tags)
}
// Docstring returns the godocs formated documentation
func (ref *ShapeRef) Docstring() string {
if ref.Documentation != "" {
return strings.Trim(ref.Documentation, "\n ")
}
return ref.Shape.Docstring()
}
// Docstring returns the godocs formated documentation
func (s *Shape) Docstring() string {
return strings.Trim(s.Documentation, "\n ")
}
// IndentedDocstring is the indented form of the doc string.
func (ref *ShapeRef) IndentedDocstring() string {
doc := ref.Docstring()
return strings.Replace(doc, "// ", "// ", -1)
}
var goCodeStringerTmpl = template.Must(template.New("goCodeStringerTmpl").Parse(`
// String returns the string representation
func (s {{ .ShapeName }}) String() string {
return awsutil.Prettify(s)
}
// GoString returns the string representation
func (s {{ .ShapeName }}) GoString() string {
return s.String()
}
`))
// GoCodeStringers renders the Stringers for API input/output shapes
func (s *Shape) GoCodeStringers() string {
w := bytes.Buffer{}
if err := goCodeStringerTmpl.Execute(&w, s); err != nil {
panic(fmt.Sprintln("Unexpected error executing GoCodeStringers template", err))
}
return w.String()
}
var enumStrip = regexp.MustCompile(`[^a-zA-Z0-9_:\./-]`)
var enumDelims = regexp.MustCompile(`[-_:\./]+`)
var enumCamelCase = regexp.MustCompile(`([a-z])([A-Z])`)
// EnumName returns the Nth enum in the shapes Enum list
func (s *Shape) EnumName(n int) string {
enum := s.Enum[n]
enum = enumStrip.ReplaceAllLiteralString(enum, "")
enum = enumCamelCase.ReplaceAllString(enum, "$1-$2")
parts := enumDelims.Split(enum, -1)
for i, v := range parts {
v = strings.ToLower(v)
parts[i] = ""
if len(v) > 0 {
parts[i] = strings.ToUpper(v[0:1])
}
if len(v) > 1 {
parts[i] += v[1:]
}
}
enum = strings.Join(parts, "")
enum = strings.ToUpper(enum[0:1]) + enum[1:]
return enum
}
// NestedShape returns the shape pointer value for the shape which is nested
// under the current shape. If the shape is not nested nil will be returned.
//
// strucutures, the current shape is returned
// map: the value shape of the map is returned
// list: the element shape of the list is returned
func (s *Shape) NestedShape() *Shape {
var nestedShape *Shape
switch s.Type {
case "structure":
nestedShape = s
case "map":
nestedShape = s.ValueRef.Shape
case "list":
nestedShape = s.MemberRef.Shape
}
return nestedShape
}
var structShapeTmpl = template.Must(template.New("StructShape").Funcs(template.FuncMap{
"GetCrosslinkURL": GetCrosslinkURL,
}).Parse(`
{{ .Docstring }}
{{ $context := . -}}
type {{ .ShapeName }} struct {
_ struct{} {{ .GoTags true false }}
{{ range $_, $name := $context.MemberNames -}}
{{ $elem := index $context.MemberRefs $name -}}
{{ $isBlob := $context.WillRefBeBase64Encoded $name -}}
{{ $isRequired := $context.IsRequired $name -}}
{{ $doc := $elem.Docstring -}}
{{ if $doc -}}
{{ $doc }}
{{ end -}}
{{ if $isBlob -}}
{{ if $doc -}}
//
{{ end -}}
// {{ $name }} is automatically base64 encoded/decoded by the SDK.
{{ end -}}
{{ if $isRequired -}}
{{ if or $doc $isBlob -}}
//
{{ end -}}
// {{ $name }} is a required field
{{ end -}}
{{ $name }} {{ $context.GoStructType $name $elem }} {{ $elem.GoTags false $isRequired }}
{{ end }}
}
{{ if not .API.NoStringerMethods }}
{{ .GoCodeStringers }}
{{ end }}
{{ if not .API.NoValidataShapeMethods }}
{{ if .Validations -}}
{{ .Validations.GoCode . }}
{{ end }}
{{ end }}
{{ if not .API.NoGenStructFieldAccessors }}
{{ $builderShapeName := print .ShapeName -}}
{{ range $_, $name := $context.MemberNames -}}
{{ $elem := index $context.MemberRefs $name -}}
// Set{{ $name }} sets the {{ $name }} field's value.
func (s *{{ $builderShapeName }}) Set{{ $name }}(v {{ $context.GoStructValueType $name $elem }}) *{{ $builderShapeName }} {
{{ if $elem.UseIndirection -}}
s.{{ $name }} = &v
{{ else -}}
s.{{ $name }} = v
{{ end -}}
return s
}
{{ if $elem.GenerateGetter -}}
func (s *{{ $builderShapeName }}) get{{ $name }}() (v {{ $context.GoStructValueType $name $elem }}) {
{{ if $elem.UseIndirection -}}
if s.{{ $name }} == nil {
return v
}
return *s.{{ $name }}
{{ else -}}
return s.{{ $name }}
{{ end -}}
}
{{- end }}
{{ end }}
{{ end }}
`))
var enumShapeTmpl = template.Must(template.New("EnumShape").Parse(`
{{ .Docstring }}
const (
{{ $context := . -}}
{{ range $index, $elem := .Enum -}}
{{ $name := index $context.EnumConsts $index -}}
// {{ $name }} is a {{ $context.ShapeName }} enum value
{{ $name }} = "{{ $elem }}"
{{ end }}
)
`))
// GoCode returns the rendered Go code for the Shape.
func (s *Shape) GoCode() string {
b := &bytes.Buffer{}
switch {
case s.Type == "structure":
if err := structShapeTmpl.Execute(b, s); err != nil {
panic(fmt.Sprintf("Failed to generate struct shape %s, %v\n", s.ShapeName, err))
}
case s.IsEnum():
if err := enumShapeTmpl.Execute(b, s); err != nil {
panic(fmt.Sprintf("Failed to generate enum shape %s, %v\n", s.ShapeName, err))
}
default:
panic(fmt.Sprintln("Cannot generate toplevel shape for", s.Type))
}
return b.String()
}
// IsEnum returns whether this shape is an enum list
func (s *Shape) IsEnum() bool {
return s.Type == "string" && len(s.Enum) > 0
}
// IsRequired returns if member is a required field.
func (s *Shape) IsRequired(member string) bool {
for _, n := range s.Required {
if n == member {
return true
}
}
return false
}
// IsInternal returns whether the shape was defined in this package
func (s *Shape) IsInternal() bool {
return s.resolvePkg == ""
}
// removeRef removes a shape reference from the list of references this
// shape is used in.
func (s *Shape) removeRef(ref *ShapeRef) {
r := s.refs
for i := 0; i < len(r); i++ {
if r[i] == ref {
j := i + 1
copy(r[i:], r[j:])
for k, n := len(r)-j+i, len(r); k < n; k++ {
r[k] = nil // free up the end of the list
} // for k
s.refs = r[:len(r)-j+i]
break
}
}
}
func (s *Shape) WillRefBeBase64Encoded(refName string) bool {
payloadRefName := s.Payload
if payloadRefName == refName {
return false
}
ref, ok := s.MemberRefs[refName]
if !ok {
panic(fmt.Sprintf("shape %s does not contain %q refName", s.ShapeName, refName))
}
return ref.Shape.Type == "blob"
}

View file

@ -0,0 +1,155 @@
// +build codegen
package api
import (
"bytes"
"fmt"
"text/template"
)
// A ShapeValidationType is the type of validation that a shape needs
type ShapeValidationType int
const (
// ShapeValidationRequired states the shape must be set
ShapeValidationRequired = iota
// ShapeValidationMinVal states the shape must have at least a number of
// elements, or for numbers a minimum value
ShapeValidationMinVal
// ShapeValidationNested states the shape has nested values that need
// to be validated
ShapeValidationNested
)
// A ShapeValidation contains information about a shape and the type of validation
// that is needed
type ShapeValidation struct {
// Name of the shape to be validated
Name string
// Reference to the shape within the context the shape is referenced
Ref *ShapeRef
// Type of validation needed
Type ShapeValidationType
}
var validationGoCodeTmpls = template.Must(template.New("validationGoCodeTmpls").Parse(`
{{ define "requiredValue" -}}
if s.{{ .Name }} == nil {
invalidParams.Add(request.NewErrParamRequired("{{ .Name }}"))
}
{{- end }}
{{ define "minLen" -}}
if s.{{ .Name }} != nil && len(s.{{ .Name }}) < {{ .Ref.Shape.Min }} {
invalidParams.Add(request.NewErrParamMinLen("{{ .Name }}", {{ .Ref.Shape.Min }}))
}
{{- end }}
{{ define "minLenString" -}}
if s.{{ .Name }} != nil && len(*s.{{ .Name }}) < {{ .Ref.Shape.Min }} {
invalidParams.Add(request.NewErrParamMinLen("{{ .Name }}", {{ .Ref.Shape.Min }}))
}
{{- end }}
{{ define "minVal" -}}
if s.{{ .Name }} != nil && *s.{{ .Name }} < {{ .Ref.Shape.Min }} {
invalidParams.Add(request.NewErrParamMinValue("{{ .Name }}", {{ .Ref.Shape.Min }}))
}
{{- end }}
{{ define "nestedMapList" -}}
if s.{{ .Name }} != nil {
for i, v := range s.{{ .Name }} {
if v == nil { continue }
if err := v.Validate(); err != nil {
invalidParams.AddNested(fmt.Sprintf("%s[%v]", "{{ .Name }}", i), err.(request.ErrInvalidParams))
}
}
}
{{- end }}
{{ define "nestedStruct" -}}
if s.{{ .Name }} != nil {
if err := s.{{ .Name }}.Validate(); err != nil {
invalidParams.AddNested("{{ .Name }}", err.(request.ErrInvalidParams))
}
}
{{- end }}
`))
// GoCode returns the generated Go code for the Shape with its validation type.
func (sv ShapeValidation) GoCode() string {
var err error
w := &bytes.Buffer{}
switch sv.Type {
case ShapeValidationRequired:
err = validationGoCodeTmpls.ExecuteTemplate(w, "requiredValue", sv)
case ShapeValidationMinVal:
switch sv.Ref.Shape.Type {
case "list", "map", "blob":
err = validationGoCodeTmpls.ExecuteTemplate(w, "minLen", sv)
case "string":
err = validationGoCodeTmpls.ExecuteTemplate(w, "minLenString", sv)
case "integer", "long", "float", "double":
err = validationGoCodeTmpls.ExecuteTemplate(w, "minVal", sv)
default:
panic(fmt.Sprintf("ShapeValidation.GoCode, %s's type %s, no min value handling",
sv.Name, sv.Ref.Shape.Type))
}
case ShapeValidationNested:
switch sv.Ref.Shape.Type {
case "map", "list":
err = validationGoCodeTmpls.ExecuteTemplate(w, "nestedMapList", sv)
default:
err = validationGoCodeTmpls.ExecuteTemplate(w, "nestedStruct", sv)
}
default:
panic(fmt.Sprintf("ShapeValidation.GoCode, %s's type %d, unknown validation type",
sv.Name, sv.Type))
}
if err != nil {
panic(fmt.Sprintf("ShapeValidation.GoCode failed, err: %v", err))
}
return w.String()
}
// A ShapeValidations is a collection of shape validations needed nested within
// a parent shape
type ShapeValidations []ShapeValidation
var validateShapeTmpl = template.Must(template.New("ValidateShape").Parse(`
// Validate inspects the fields of the type to determine if they are valid.
func (s *{{ .Shape.ShapeName }}) Validate() error {
invalidParams := request.ErrInvalidParams{Context: "{{ .Shape.ShapeName }}"}
{{ range $_, $v := .Validations -}}
{{ $v.GoCode }}
{{ end }}
if invalidParams.Len() > 0 {
return invalidParams
}
return nil
}
`))
// GoCode generates the Go code needed to perform validations for the
// shape and its nested fields.
func (vs ShapeValidations) GoCode(shape *Shape) string {
buf := &bytes.Buffer{}
validateShapeTmpl.Execute(buf, map[string]interface{}{
"Shape": shape,
"Validations": vs,
})
return buf.String()
}
// Has returns true or false if the ShapeValidations already contains the
// the reference and validation type.
func (vs ShapeValidations) Has(ref *ShapeRef, typ ShapeValidationType) bool {
for _, v := range vs {
if v.Ref == ref && v.Type == typ {
return true
}
}
return false
}

View file

@ -0,0 +1,28 @@
// +build 1.6,codegen
package api_test
import (
"testing"
"github.com/aws/aws-sdk-go/private/model/api"
)
func TestShapeTagJoin(t *testing.T) {
s := api.ShapeTags{
{Key: "location", Val: "query"},
{Key: "locationName", Val: "abc"},
{Key: "type", Val: "string"},
}
expected := `location:"query" locationName:"abc" type:"string"`
o := s.Join(" ")
o2 := s.String()
if expected != o {
t.Errorf("Expected %s, but received %s", expected, o)
}
if expected != o2 {
t.Errorf("Expected %s, but received %s", expected, o2)
}
}

View file

@ -0,0 +1,189 @@
// +build codegen
package api
import (
"bytes"
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"text/template"
)
// WaiterAcceptor is the acceptors defined in the model the SDK will use
// to wait on resource states with.
type WaiterAcceptor struct {
State string
Matcher string
Argument string
Expected interface{}
}
// ExpectedString returns the string that was expected by the WaiterAcceptor
func (a *WaiterAcceptor) ExpectedString() string {
switch a.Expected.(type) {
case string:
return fmt.Sprintf("%q", a.Expected)
default:
return fmt.Sprintf("%v", a.Expected)
}
}
// A Waiter is an individual waiter definition.
type Waiter struct {
Name string
Delay int
MaxAttempts int
OperationName string `json:"operation"`
Operation *Operation
Acceptors []WaiterAcceptor
}
// WaitersGoCode generates and returns Go code for each of the waiters of
// this API.
func (a *API) WaitersGoCode() string {
var buf bytes.Buffer
fmt.Fprintf(&buf, "import (\n%q\n\n%q\n%q\n)",
"time",
"github.com/aws/aws-sdk-go/aws",
"github.com/aws/aws-sdk-go/aws/request",
)
for _, w := range a.Waiters {
buf.WriteString(w.GoCode())
}
return buf.String()
}
// used for unmarshaling from the waiter JSON file
type waiterDefinitions struct {
*API
Waiters map[string]Waiter
}
// AttachWaiters reads a file of waiter definitions, and adds those to the API.
// Will panic if an error occurs.
func (a *API) AttachWaiters(filename string) {
p := waiterDefinitions{API: a}
f, err := os.Open(filename)
defer f.Close()
if err != nil {
panic(err)
}
err = json.NewDecoder(f).Decode(&p)
if err != nil {
panic(err)
}
p.setup()
}
func (p *waiterDefinitions) setup() {
p.API.Waiters = []Waiter{}
i, keys := 0, make([]string, len(p.Waiters))
for k := range p.Waiters {
keys[i] = k
i++
}
sort.Strings(keys)
for _, n := range keys {
e := p.Waiters[n]
n = p.ExportableName(n)
e.Name = n
e.OperationName = p.ExportableName(e.OperationName)
e.Operation = p.API.Operations[e.OperationName]
if e.Operation == nil {
panic("unknown operation " + e.OperationName + " for waiter " + n)
}
p.API.Waiters = append(p.API.Waiters, e)
}
}
var waiterTmpls = template.Must(template.New("waiterTmpls").Funcs(
template.FuncMap{
"titleCase": func(v string) string {
return strings.Title(v)
},
},
).Parse(`
{{ define "waiter"}}
// WaitUntil{{ .Name }} uses the {{ .Operation.API.NiceName }} API operation
// {{ .OperationName }} to wait for a condition to be met before returning.
// If the condition is not met within the max attempt window, an error will
// be returned.
func (c *{{ .Operation.API.StructName }}) WaitUntil{{ .Name }}(input {{ .Operation.InputRef.GoType }}) error {
return c.WaitUntil{{ .Name }}WithContext(aws.BackgroundContext(), input)
}
// WaitUntil{{ .Name }}WithContext is an extended version of WaitUntil{{ .Name }}.
// With the support for passing in a context and options to configure the
// Waiter and the underlying request options.
//
// The context must be non-nil and will be used for request cancellation. If
// the context is nil a panic will occur. In the future the SDK may create
// sub-contexts for http.Requests. See https://golang.org/pkg/context/
// for more information on using Contexts.
func (c *{{ .Operation.API.StructName }}) WaitUntil{{ .Name }}WithContext(` +
`ctx aws.Context, input {{ .Operation.InputRef.GoType }}, opts ...request.WaiterOption) error {
w := request.Waiter{
Name: "WaitUntil{{ .Name }}",
MaxAttempts: {{ .MaxAttempts }},
Delay: request.ConstantWaiterDelay({{ .Delay }} * time.Second),
Acceptors: []request.WaiterAcceptor{
{{ range $_, $a := .Acceptors }}{
State: request.{{ titleCase .State }}WaiterState,
Matcher: request.{{ titleCase .Matcher }}WaiterMatch,
{{- if .Argument }}Argument: "{{ .Argument }}",{{ end }}
Expected: {{ .ExpectedString }},
},
{{ end }}
},
Logger: c.Config.Logger,
NewRequest: func(opts []request.Option) (*request.Request, error) {
var inCpy {{ .Operation.InputRef.GoType }}
if input != nil {
tmp := *input
inCpy = &tmp
}
req, _ := c.{{ .OperationName }}Request(inCpy)
req.SetContext(ctx)
req.ApplyOptions(opts...)
return req, nil
},
}
w.ApplyOptions(opts...)
return w.WaitWithContext(ctx)
}
{{- end }}
{{ define "waiter interface" }}
WaitUntil{{ .Name }}({{ .Operation.InputRef.GoTypeWithPkgName }}) error
WaitUntil{{ .Name }}WithContext(aws.Context, {{ .Operation.InputRef.GoTypeWithPkgName }}, ...request.WaiterOption) error
{{- end }}
`))
// InterfaceSignature returns a string representing the Waiter's interface
// function signature.
func (w *Waiter) InterfaceSignature() string {
var buf bytes.Buffer
if err := waiterTmpls.ExecuteTemplate(&buf, "waiter interface", w); err != nil {
panic(err)
}
return strings.TrimSpace(buf.String())
}
// GoCode returns the generated Go code for an individual waiter.
func (w *Waiter) GoCode() string {
var buf bytes.Buffer
if err := waiterTmpls.ExecuteTemplate(&buf, "waiter", w); err != nil {
panic(err)
}
return buf.String()
}

View file

@ -0,0 +1,29 @@
// +build codegen
package main
import (
"fmt"
"os"
"path/filepath"
"sort"
"github.com/aws/aws-sdk-go/private/model/api"
)
func main() {
dir, _ := os.Open(filepath.Join("models", "apis"))
names, _ := dir.Readdirnames(0)
for _, name := range names {
m, _ := filepath.Glob(filepath.Join("models", "apis", name, "*", "api-2.json"))
if len(m) == 0 {
continue
}
sort.Strings(m)
f := m[len(m)-1]
a := api.API{}
a.Attach(f)
fmt.Printf("%s\t%s\n", a.Metadata.ServiceFullName, a.Metadata.APIVersion)
}
}

View file

@ -0,0 +1,315 @@
// +build codegen
// Command aws-gen-gocli parses a JSON description of an AWS API and generates a
// Go file containing a client for the API.
//
// aws-gen-gocli apis/s3/2006-03-03/api-2.json
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime/debug"
"sort"
"strings"
"sync"
"github.com/aws/aws-sdk-go/private/model/api"
"github.com/aws/aws-sdk-go/private/util"
)
type generateInfo struct {
*api.API
PackageDir string
}
var excludeServices = map[string]struct{}{
"importexport": {},
}
// newGenerateInfo initializes the service API's folder structure for a specific service.
// If the SERVICES environment variable is set, and this service is not apart of the list
// this service will be skipped.
func newGenerateInfo(modelFile, svcPath, svcImportPath string) *generateInfo {
g := &generateInfo{API: &api.API{SvcClientImportPath: svcImportPath, BaseCrosslinkURL: "https://docs.aws.amazon.com"}}
g.API.Attach(modelFile)
if _, ok := excludeServices[g.API.PackageName()]; ok {
return nil
}
paginatorsFile := strings.Replace(modelFile, "api-2.json", "paginators-1.json", -1)
if _, err := os.Stat(paginatorsFile); err == nil {
g.API.AttachPaginators(paginatorsFile)
} else if !os.IsNotExist(err) {
fmt.Println("api-2.json error:", err)
}
docsFile := strings.Replace(modelFile, "api-2.json", "docs-2.json", -1)
if _, err := os.Stat(docsFile); err == nil {
g.API.AttachDocs(docsFile)
} else {
fmt.Println("docs-2.json error:", err)
}
waitersFile := strings.Replace(modelFile, "api-2.json", "waiters-2.json", -1)
if _, err := os.Stat(waitersFile); err == nil {
g.API.AttachWaiters(waitersFile)
} else if !os.IsNotExist(err) {
fmt.Println("waiters-2.json error:", err)
}
examplesFile := strings.Replace(modelFile, "api-2.json", "examples-1.json", -1)
if _, err := os.Stat(examplesFile); err == nil {
g.API.AttachExamples(examplesFile)
} else if !os.IsNotExist(err) {
fmt.Println("examples-1.json error:", err)
}
// pkgDocAddonsFile := strings.Replace(modelFile, "api-2.json", "go-pkg-doc.gotmpl", -1)
// if _, err := os.Stat(pkgDocAddonsFile); err == nil {
// g.API.AttachPackageDocAddons(pkgDocAddonsFile)
// } else if !os.IsNotExist(err) {
// fmt.Println("go-pkg-doc.gotmpl error:", err)
// }
g.API.Setup()
if svc := os.Getenv("SERVICES"); svc != "" {
svcs := strings.Split(svc, ",")
included := false
for _, s := range svcs {
if s == g.API.PackageName() {
included = true
break
}
}
if !included {
// skip this non-included service
return nil
}
}
// ensure the directory exists
pkgDir := filepath.Join(svcPath, g.API.PackageName())
os.MkdirAll(pkgDir, 0775)
os.MkdirAll(filepath.Join(pkgDir, g.API.InterfacePackageName()), 0775)
g.PackageDir = pkgDir
return g
}
// Generates service api, examples, and interface from api json definition files.
//
// Flags:
// -path alternative service path to write generated files to for each service.
//
// Env:
// SERVICES comma separated list of services to generate.
func main() {
var svcPath, sessionPath, svcImportPath string
flag.StringVar(&svcPath, "path", "service", "directory to generate service clients in")
flag.StringVar(&sessionPath, "sessionPath", filepath.Join("aws", "session"), "generate session service client factories")
flag.StringVar(&svcImportPath, "svc-import-path", "github.com/aws/aws-sdk-go/service", "namespace to generate service client Go code import path under")
flag.Parse()
api.Bootstrap()
files := []string{}
for i := 0; i < flag.NArg(); i++ {
file := flag.Arg(i)
if strings.Contains(file, "*") {
paths, _ := filepath.Glob(file)
files = append(files, paths...)
} else {
files = append(files, file)
}
}
for svcName := range excludeServices {
if strings.Contains(os.Getenv("SERVICES"), svcName) {
fmt.Fprintf(os.Stderr, "Service %s is not supported\n", svcName)
os.Exit(1)
}
}
sort.Strings(files)
// Remove old API versions from list
m := map[string]bool{}
// caches paths to ensure we are not overriding previously generated
// code.
servicePaths := map[string]struct{}{}
for i := range files {
idx := len(files) - 1 - i
parts := strings.Split(files[idx], string(filepath.Separator))
svc := parts[len(parts)-3] // service name is 2nd-to-last component
if m[svc] {
files[idx] = "" // wipe this one out if we already saw the service
}
m[svc] = true
}
wg := sync.WaitGroup{}
for i := range files {
filename := files[i]
if filename == "" { // empty file
continue
}
genInfo := newGenerateInfo(filename, svcPath, svcImportPath)
if genInfo == nil {
continue
}
if _, ok := excludeServices[genInfo.API.PackageName()]; ok {
// Skip services not yet supported.
continue
}
if _, ok := servicePaths[genInfo.PackageDir]; ok {
fmt.Fprintf(os.Stderr, "Path %q has already been generated", genInfo.PackageDir)
os.Exit(1)
}
servicePaths[genInfo.PackageDir] = struct{}{}
wg.Add(1)
go func(g *generateInfo, filename string) {
defer wg.Done()
writeServiceFiles(g, filename)
}(genInfo, filename)
}
wg.Wait()
}
func writeServiceFiles(g *generateInfo, filename string) {
defer func() {
if r := recover(); r != nil {
fmt.Fprintf(os.Stderr, "Error generating %s\n%s\n%s\n",
filename, r, debug.Stack())
os.Exit(1)
}
}()
fmt.Printf("Generating %s (%s)...\n",
g.API.PackageName(), g.API.Metadata.APIVersion)
// write files for service client and API
Must(writeServiceDocFile(g))
Must(writeAPIFile(g))
Must(writeServiceFile(g))
Must(writeInterfaceFile(g))
Must(writeWaitersFile(g))
Must(writeAPIErrorsFile(g))
Must(writeExamplesFile(g))
}
// Must will panic if the error passed in is not nil.
func Must(err error) {
if err != nil {
panic(err)
}
}
const codeLayout = `// Code generated by private/model/cli/gen-api/main.go. DO NOT EDIT.
%s
package %s
%s
`
func writeGoFile(file string, layout string, args ...interface{}) error {
return ioutil.WriteFile(file, []byte(util.GoFmt(fmt.Sprintf(layout, args...))), 0664)
}
// writeServiceDocFile generates the documentation for service package.
func writeServiceDocFile(g *generateInfo) error {
return writeGoFile(filepath.Join(g.PackageDir, "doc.go"),
codeLayout,
strings.TrimSpace(g.API.ServicePackageDoc()),
g.API.PackageName(),
"",
)
}
// writeExamplesFile writes out the service example file.
func writeExamplesFile(g *generateInfo) error {
code := g.API.ExamplesGoCode()
if len(code) > 0 {
return writeGoFile(filepath.Join(g.PackageDir, "examples_test.go"),
codeLayout,
"",
g.API.PackageName()+"_test",
code,
)
}
return nil
}
// writeServiceFile writes out the service initialization file.
func writeServiceFile(g *generateInfo) error {
return writeGoFile(filepath.Join(g.PackageDir, "service.go"),
codeLayout,
"",
g.API.PackageName(),
g.API.ServiceGoCode(),
)
}
// writeInterfaceFile writes out the service interface file.
func writeInterfaceFile(g *generateInfo) error {
const pkgDoc = `
// Package %s provides an interface to enable mocking the %s service client
// for testing your code.
//
// It is important to note that this interface will have breaking changes
// when the service model is updated and adds new API operations, paginators,
// and waiters.`
return writeGoFile(filepath.Join(g.PackageDir, g.API.InterfacePackageName(), "interface.go"),
codeLayout,
fmt.Sprintf(pkgDoc, g.API.InterfacePackageName(), g.API.Metadata.ServiceFullName),
g.API.InterfacePackageName(),
g.API.InterfaceGoCode(),
)
}
func writeWaitersFile(g *generateInfo) error {
if len(g.API.Waiters) == 0 {
return nil
}
return writeGoFile(filepath.Join(g.PackageDir, "waiters.go"),
codeLayout,
"",
g.API.PackageName(),
g.API.WaitersGoCode(),
)
}
// writeAPIFile writes out the service API file.
func writeAPIFile(g *generateInfo) error {
return writeGoFile(filepath.Join(g.PackageDir, "api.go"),
codeLayout,
"",
g.API.PackageName(),
g.API.APIGoCode(),
)
}
// writeAPIErrorsFile writes out the service API errors file.
func writeAPIErrorsFile(g *generateInfo) error {
return writeGoFile(filepath.Join(g.PackageDir, "errors.go"),
codeLayout,
"",
g.API.PackageName(),
g.API.APIErrorsGoCode(),
)
}

View file

@ -0,0 +1,56 @@
// +build codegen
// Command gen-endpoints parses a JSON description of the AWS endpoint
// discovery logic and generates a Go file which returns an endpoint.
//
// aws-gen-goendpoints apis/_endpoints.json aws/endpoints_map.go
package main
import (
"flag"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws/endpoints"
)
// Generates the endpoints from json description
//
// Args:
// -model The definition file to use
// -out The output file to generate
func main() {
var modelName, outName string
flag.StringVar(&modelName, "model", "", "Endpoints definition model")
flag.StringVar(&outName, "out", "", "File to write generated endpoints to.")
flag.Parse()
if len(modelName) == 0 || len(outName) == 0 {
exitErrorf("model and out both required.")
}
modelFile, err := os.Open(modelName)
if err != nil {
exitErrorf("failed to open model definition, %v.", err)
}
defer modelFile.Close()
outFile, err := os.Create(outName)
if err != nil {
exitErrorf("failed to open out file, %v.", err)
}
defer func() {
if err := outFile.Close(); err != nil {
exitErrorf("failed to successfully write %q file, %v", outName, err)
}
}()
if err := endpoints.CodeGenModel(modelFile, outFile); err != nil {
exitErrorf("failed to codegen model, %v", err)
}
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}

View file

@ -0,0 +1,35 @@
// Package ec2query provides serialization of AWS EC2 requests and responses.
package ec2query
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/ec2.json build_test.go
import (
"net/url"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/protocol/query/queryutil"
)
// BuildHandler is a named request handler for building ec2query protocol requests
var BuildHandler = request.NamedHandler{Name: "awssdk.ec2query.Build", Fn: Build}
// Build builds a request for the EC2 protocol.
func Build(r *request.Request) {
body := url.Values{
"Action": {r.Operation.Name},
"Version": {r.ClientInfo.APIVersion},
}
if err := queryutil.Parse(body, r.Params, true); err != nil {
r.Error = awserr.New("SerializationError", "failed encoding EC2 Query request", err)
}
if !r.IsPresigned() {
r.HTTPRequest.Method = "POST"
r.HTTPRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")
r.SetBufferBody([]byte(body.Encode()))
} else { // This is a pre-signed request
r.HTTPRequest.Method = "GET"
r.HTTPRequest.URL.RawQuery = body.Encode()
}
}

View file

@ -0,0 +1,85 @@
// +build bench
package ec2query_test
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/private/protocol/ec2query"
"github.com/aws/aws-sdk-go/service/ec2"
)
func BenchmarkEC2QueryBuild_Complex_ec2AuthorizeSecurityGroupEgress(b *testing.B) {
params := &ec2.AuthorizeSecurityGroupEgressInput{
GroupId: aws.String("String"), // Required
CidrIp: aws.String("String"),
DryRun: aws.Bool(true),
FromPort: aws.Int64(1),
IpPermissions: []*ec2.IpPermission{
{ // Required
FromPort: aws.Int64(1),
IpProtocol: aws.String("String"),
IpRanges: []*ec2.IpRange{
{ // Required
CidrIp: aws.String("String"),
},
// More values...
},
PrefixListIds: []*ec2.PrefixListId{
{ // Required
PrefixListId: aws.String("String"),
},
// More values...
},
ToPort: aws.Int64(1),
UserIdGroupPairs: []*ec2.UserIdGroupPair{
{ // Required
GroupId: aws.String("String"),
GroupName: aws.String("String"),
UserId: aws.String("String"),
},
// More values...
},
},
// More values...
},
IpProtocol: aws.String("String"),
SourceSecurityGroupName: aws.String("String"),
SourceSecurityGroupOwnerId: aws.String("String"),
ToPort: aws.Int64(1),
}
benchEC2QueryBuild(b, "AuthorizeSecurityGroupEgress", params)
}
func BenchmarkEC2QueryBuild_Simple_ec2AttachNetworkInterface(b *testing.B) {
params := &ec2.AttachNetworkInterfaceInput{
DeviceIndex: aws.Int64(1), // Required
InstanceId: aws.String("String"), // Required
NetworkInterfaceId: aws.String("String"), // Required
DryRun: aws.Bool(true),
}
benchEC2QueryBuild(b, "AttachNetworkInterface", params)
}
func benchEC2QueryBuild(b *testing.B, opName string, params interface{}) {
svc := awstesting.NewClient()
svc.ServiceName = "ec2"
svc.APIVersion = "2015-04-15"
for i := 0; i < b.N; i++ {
r := svc.NewRequest(&request.Operation{
Name: opName,
HTTPMethod: "POST",
HTTPPath: "/",
}, params, nil)
ec2query.Build(r)
if r.Error != nil {
b.Fatal("Unexpected error", r.Error)
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,63 @@
package ec2query
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/ec2.json unmarshal_test.go
import (
"encoding/xml"
"io"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
)
// UnmarshalHandler is a named request handler for unmarshaling ec2query protocol requests
var UnmarshalHandler = request.NamedHandler{Name: "awssdk.ec2query.Unmarshal", Fn: Unmarshal}
// UnmarshalMetaHandler is a named request handler for unmarshaling ec2query protocol request metadata
var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalMeta", Fn: UnmarshalMeta}
// UnmarshalErrorHandler is a named request handler for unmarshaling ec2query protocol request errors
var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.ec2query.UnmarshalError", Fn: UnmarshalError}
// Unmarshal unmarshals a response body for the EC2 protocol.
func Unmarshal(r *request.Request) {
defer r.HTTPResponse.Body.Close()
if r.DataFilled() {
decoder := xml.NewDecoder(r.HTTPResponse.Body)
err := xmlutil.UnmarshalXML(r.Data, decoder, "")
if err != nil {
r.Error = awserr.New("SerializationError", "failed decoding EC2 Query response", err)
return
}
}
}
// UnmarshalMeta unmarshals response headers for the EC2 protocol.
func UnmarshalMeta(r *request.Request) {
// TODO implement unmarshaling of request IDs
}
type xmlErrorResponse struct {
XMLName xml.Name `xml:"Response"`
Code string `xml:"Errors>Error>Code"`
Message string `xml:"Errors>Error>Message"`
RequestID string `xml:"RequestID"`
}
// UnmarshalError unmarshals a response error for the EC2 protocol.
func UnmarshalError(r *request.Request) {
defer r.HTTPResponse.Body.Close()
resp := &xmlErrorResponse{}
err := xml.NewDecoder(r.HTTPResponse.Body).Decode(resp)
if err != nil && err != io.EOF {
r.Error = awserr.New("SerializationError", "failed decoding EC2 Query error response", err)
} else {
r.Error = awserr.NewRequestFailure(
awserr.New(resp.Code, resp.Message, nil),
r.HTTPResponse.StatusCode,
resp.RequestID,
)
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,286 @@
// Package jsonutil provides JSON serialization of AWS requests and responses.
package jsonutil
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"math"
"reflect"
"sort"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/private/protocol"
)
var timeType = reflect.ValueOf(time.Time{}).Type()
var byteSliceType = reflect.ValueOf([]byte{}).Type()
// BuildJSON builds a JSON string for a given object v.
func BuildJSON(v interface{}) ([]byte, error) {
var buf bytes.Buffer
err := buildAny(reflect.ValueOf(v), &buf, "")
return buf.Bytes(), err
}
func buildAny(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
origVal := value
value = reflect.Indirect(value)
if !value.IsValid() {
return nil
}
vtype := value.Type()
t := tag.Get("type")
if t == "" {
switch vtype.Kind() {
case reflect.Struct:
// also it can't be a time object
if value.Type() != timeType {
t = "structure"
}
case reflect.Slice:
// also it can't be a byte slice
if _, ok := value.Interface().([]byte); !ok {
t = "list"
}
case reflect.Map:
// cannot be a JSONValue map
if _, ok := value.Interface().(aws.JSONValue); !ok {
t = "map"
}
}
}
switch t {
case "structure":
if field, ok := vtype.FieldByName("_"); ok {
tag = field.Tag
}
return buildStruct(value, buf, tag)
case "list":
return buildList(value, buf, tag)
case "map":
return buildMap(value, buf, tag)
default:
return buildScalar(origVal, buf, tag)
}
}
func buildStruct(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
if !value.IsValid() {
return nil
}
// unwrap payloads
if payload := tag.Get("payload"); payload != "" {
field, _ := value.Type().FieldByName(payload)
tag = field.Tag
value = elemOf(value.FieldByName(payload))
if !value.IsValid() {
return nil
}
}
buf.WriteByte('{')
t := value.Type()
first := true
for i := 0; i < t.NumField(); i++ {
member := value.Field(i)
// This allocates the most memory.
// Additionally, we cannot skip nil fields due to
// idempotency auto filling.
field := t.Field(i)
if field.PkgPath != "" {
continue // ignore unexported fields
}
if field.Tag.Get("json") == "-" {
continue
}
if field.Tag.Get("location") != "" {
continue // ignore non-body elements
}
if field.Tag.Get("ignore") != "" {
continue
}
if protocol.CanSetIdempotencyToken(member, field) {
token := protocol.GetIdempotencyToken()
member = reflect.ValueOf(&token)
}
if (member.Kind() == reflect.Ptr || member.Kind() == reflect.Slice || member.Kind() == reflect.Map) && member.IsNil() {
continue // ignore unset fields
}
if first {
first = false
} else {
buf.WriteByte(',')
}
// figure out what this field is called
name := field.Name
if locName := field.Tag.Get("locationName"); locName != "" {
name = locName
}
writeString(name, buf)
buf.WriteString(`:`)
err := buildAny(member, buf, field.Tag)
if err != nil {
return err
}
}
buf.WriteString("}")
return nil
}
func buildList(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
buf.WriteString("[")
for i := 0; i < value.Len(); i++ {
buildAny(value.Index(i), buf, "")
if i < value.Len()-1 {
buf.WriteString(",")
}
}
buf.WriteString("]")
return nil
}
type sortedValues []reflect.Value
func (sv sortedValues) Len() int { return len(sv) }
func (sv sortedValues) Swap(i, j int) { sv[i], sv[j] = sv[j], sv[i] }
func (sv sortedValues) Less(i, j int) bool { return sv[i].String() < sv[j].String() }
func buildMap(value reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
buf.WriteString("{")
sv := sortedValues(value.MapKeys())
sort.Sort(sv)
for i, k := range sv {
if i > 0 {
buf.WriteByte(',')
}
writeString(k.String(), buf)
buf.WriteString(`:`)
buildAny(value.MapIndex(k), buf, "")
}
buf.WriteString("}")
return nil
}
func buildScalar(v reflect.Value, buf *bytes.Buffer, tag reflect.StructTag) error {
// prevents allocation on the heap.
scratch := [64]byte{}
switch value := reflect.Indirect(v); value.Kind() {
case reflect.String:
writeString(value.String(), buf)
case reflect.Bool:
if value.Bool() {
buf.WriteString("true")
} else {
buf.WriteString("false")
}
case reflect.Int64:
buf.Write(strconv.AppendInt(scratch[:0], value.Int(), 10))
case reflect.Float64:
f := value.Float()
if math.IsInf(f, 0) || math.IsNaN(f) {
return &json.UnsupportedValueError{Value: v, Str: strconv.FormatFloat(f, 'f', -1, 64)}
}
buf.Write(strconv.AppendFloat(scratch[:0], f, 'f', -1, 64))
default:
switch converted := value.Interface().(type) {
case time.Time:
buf.Write(strconv.AppendInt(scratch[:0], converted.UTC().Unix(), 10))
case []byte:
if !value.IsNil() {
buf.WriteByte('"')
if len(converted) < 1024 {
// for small buffers, using Encode directly is much faster.
dst := make([]byte, base64.StdEncoding.EncodedLen(len(converted)))
base64.StdEncoding.Encode(dst, converted)
buf.Write(dst)
} else {
// for large buffers, avoid unnecessary extra temporary
// buffer space.
enc := base64.NewEncoder(base64.StdEncoding, buf)
enc.Write(converted)
enc.Close()
}
buf.WriteByte('"')
}
case aws.JSONValue:
str, err := protocol.EncodeJSONValue(converted, protocol.QuotedEscape)
if err != nil {
return fmt.Errorf("unable to encode JSONValue, %v", err)
}
buf.WriteString(str)
default:
return fmt.Errorf("unsupported JSON value %v (%s)", value.Interface(), value.Type())
}
}
return nil
}
var hex = "0123456789abcdef"
func writeString(s string, buf *bytes.Buffer) {
buf.WriteByte('"')
for i := 0; i < len(s); i++ {
if s[i] == '"' {
buf.WriteString(`\"`)
} else if s[i] == '\\' {
buf.WriteString(`\\`)
} else if s[i] == '\b' {
buf.WriteString(`\b`)
} else if s[i] == '\f' {
buf.WriteString(`\f`)
} else if s[i] == '\r' {
buf.WriteString(`\r`)
} else if s[i] == '\t' {
buf.WriteString(`\t`)
} else if s[i] == '\n' {
buf.WriteString(`\n`)
} else if s[i] < 32 {
buf.WriteString("\\u00")
buf.WriteByte(hex[s[i]>>4])
buf.WriteByte(hex[s[i]&0xF])
} else {
buf.WriteByte(s[i])
}
}
buf.WriteByte('"')
}
// Returns the reflection element of a value, if it is a pointer.
func elemOf(value reflect.Value) reflect.Value {
for value.Kind() == reflect.Ptr {
value = value.Elem()
}
return value
}

View file

@ -0,0 +1,109 @@
package jsonutil_test
import (
"encoding/json"
"testing"
"time"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
"github.com/stretchr/testify/assert"
)
func S(s string) *string {
return &s
}
func D(s int64) *int64 {
return &s
}
func F(s float64) *float64 {
return &s
}
func T(s time.Time) *time.Time {
return &s
}
type J struct {
S *string
SS []string
D *int64
F *float64
T *time.Time
}
var zero = 0.0
var jsonTests = []struct {
in interface{}
out string
err string
}{
{
J{},
`{}`,
``,
},
{
J{
S: S("str"),
SS: []string{"A", "B", "C"},
D: D(123),
F: F(4.56),
T: T(time.Unix(987, 0)),
},
`{"S":"str","SS":["A","B","C"],"D":123,"F":4.56,"T":987}`,
``,
},
{
J{
S: S(`"''"`),
},
`{"S":"\"''\""}`,
``,
},
{
J{
S: S("\x00føø\u00FF\n\\\"\r\t\b\f"),
},
`{"S":"\u0000føøÿ\n\\\"\r\t\b\f"}`,
``,
},
{
J{
F: F(4.56 / zero),
},
"",
`json: unsupported value: +Inf`,
},
}
func TestBuildJSON(t *testing.T) {
for _, test := range jsonTests {
out, err := jsonutil.BuildJSON(test.in)
if test.err != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), test.err)
} else {
assert.NoError(t, err)
assert.Equal(t, string(out), test.out)
}
}
}
func BenchmarkBuildJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range jsonTests {
jsonutil.BuildJSON(test.in)
}
}
}
func BenchmarkStdlibJSON(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range jsonTests {
json.Marshal(test.in)
}
}
}

View file

@ -0,0 +1,226 @@
package jsonutil
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"reflect"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/private/protocol"
)
// UnmarshalJSON reads a stream and unmarshals the results in object v.
func UnmarshalJSON(v interface{}, stream io.Reader) error {
var out interface{}
b, err := ioutil.ReadAll(stream)
if err != nil {
return err
}
if len(b) == 0 {
return nil
}
if err := json.Unmarshal(b, &out); err != nil {
return err
}
return unmarshalAny(reflect.ValueOf(v), out, "")
}
func unmarshalAny(value reflect.Value, data interface{}, tag reflect.StructTag) error {
vtype := value.Type()
if vtype.Kind() == reflect.Ptr {
vtype = vtype.Elem() // check kind of actual element type
}
t := tag.Get("type")
if t == "" {
switch vtype.Kind() {
case reflect.Struct:
// also it can't be a time object
if _, ok := value.Interface().(*time.Time); !ok {
t = "structure"
}
case reflect.Slice:
// also it can't be a byte slice
if _, ok := value.Interface().([]byte); !ok {
t = "list"
}
case reflect.Map:
// cannot be a JSONValue map
if _, ok := value.Interface().(aws.JSONValue); !ok {
t = "map"
}
}
}
switch t {
case "structure":
if field, ok := vtype.FieldByName("_"); ok {
tag = field.Tag
}
return unmarshalStruct(value, data, tag)
case "list":
return unmarshalList(value, data, tag)
case "map":
return unmarshalMap(value, data, tag)
default:
return unmarshalScalar(value, data, tag)
}
}
func unmarshalStruct(value reflect.Value, data interface{}, tag reflect.StructTag) error {
if data == nil {
return nil
}
mapData, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("JSON value is not a structure (%#v)", data)
}
t := value.Type()
if value.Kind() == reflect.Ptr {
if value.IsNil() { // create the structure if it's nil
s := reflect.New(value.Type().Elem())
value.Set(s)
value = s
}
value = value.Elem()
t = t.Elem()
}
// unwrap any payloads
if payload := tag.Get("payload"); payload != "" {
field, _ := t.FieldByName(payload)
return unmarshalAny(value.FieldByName(payload), data, field.Tag)
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.PkgPath != "" {
continue // ignore unexported fields
}
// figure out what this field is called
name := field.Name
if locName := field.Tag.Get("locationName"); locName != "" {
name = locName
}
member := value.FieldByIndex(field.Index)
err := unmarshalAny(member, mapData[name], field.Tag)
if err != nil {
return err
}
}
return nil
}
func unmarshalList(value reflect.Value, data interface{}, tag reflect.StructTag) error {
if data == nil {
return nil
}
listData, ok := data.([]interface{})
if !ok {
return fmt.Errorf("JSON value is not a list (%#v)", data)
}
if value.IsNil() {
l := len(listData)
value.Set(reflect.MakeSlice(value.Type(), l, l))
}
for i, c := range listData {
err := unmarshalAny(value.Index(i), c, "")
if err != nil {
return err
}
}
return nil
}
func unmarshalMap(value reflect.Value, data interface{}, tag reflect.StructTag) error {
if data == nil {
return nil
}
mapData, ok := data.(map[string]interface{})
if !ok {
return fmt.Errorf("JSON value is not a map (%#v)", data)
}
if value.IsNil() {
value.Set(reflect.MakeMap(value.Type()))
}
for k, v := range mapData {
kvalue := reflect.ValueOf(k)
vvalue := reflect.New(value.Type().Elem()).Elem()
unmarshalAny(vvalue, v, "")
value.SetMapIndex(kvalue, vvalue)
}
return nil
}
func unmarshalScalar(value reflect.Value, data interface{}, tag reflect.StructTag) error {
errf := func() error {
return fmt.Errorf("unsupported value: %v (%s)", value.Interface(), value.Type())
}
switch d := data.(type) {
case nil:
return nil // nothing to do here
case string:
switch value.Interface().(type) {
case *string:
value.Set(reflect.ValueOf(&d))
case []byte:
b, err := base64.StdEncoding.DecodeString(d)
if err != nil {
return err
}
value.Set(reflect.ValueOf(b))
case aws.JSONValue:
// No need to use escaping as the value is a non-quoted string.
v, err := protocol.DecodeJSONValue(d, protocol.NoEscape)
if err != nil {
return err
}
value.Set(reflect.ValueOf(v))
default:
return errf()
}
case float64:
switch value.Interface().(type) {
case *int64:
di := int64(d)
value.Set(reflect.ValueOf(&di))
case *float64:
value.Set(reflect.ValueOf(&d))
case *time.Time:
t := time.Unix(int64(d), 0).UTC()
value.Set(reflect.ValueOf(&t))
default:
return errf()
}
case bool:
switch value.Interface().(type) {
case *bool:
value.Set(reflect.ValueOf(&d))
default:
return errf()
}
default:
return fmt.Errorf("unsupported JSON value (%v)", data)
}
return nil
}

View file

@ -0,0 +1,71 @@
// +build bench
package jsonrpc_test
import (
"bytes"
"encoding/json"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
"github.com/aws/aws-sdk-go/private/protocol/jsonrpc"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
func BenchmarkJSONRPCBuild_Simple_dynamodbPutItem(b *testing.B) {
svc := awstesting.NewClient()
params := getDynamodbPutItemParams(b)
for i := 0; i < b.N; i++ {
r := svc.NewRequest(&request.Operation{Name: "Operation"}, params, nil)
jsonrpc.Build(r)
if r.Error != nil {
b.Fatal("Unexpected error", r.Error)
}
}
}
func BenchmarkJSONUtilBuild_Simple_dynamodbPutItem(b *testing.B) {
svc := awstesting.NewClient()
params := getDynamodbPutItemParams(b)
for i := 0; i < b.N; i++ {
r := svc.NewRequest(&request.Operation{Name: "Operation"}, params, nil)
_, err := jsonutil.BuildJSON(r.Params)
if err != nil {
b.Fatal("Unexpected error", err)
}
}
}
func BenchmarkEncodingJSONMarshal_Simple_dynamodbPutItem(b *testing.B) {
params := getDynamodbPutItemParams(b)
for i := 0; i < b.N; i++ {
buf := &bytes.Buffer{}
encoder := json.NewEncoder(buf)
if err := encoder.Encode(params); err != nil {
b.Fatal("Unexpected error", err)
}
}
}
func getDynamodbPutItemParams(b *testing.B) *dynamodb.PutItemInput {
av, err := dynamodbattribute.ConvertToMap(struct {
Key string
Data string
}{Key: "MyKey", Data: "MyData"})
if err != nil {
b.Fatal("benchPutItem, expect no ConvertToMap errors", err)
}
return &dynamodb.PutItemInput{
Item: av,
TableName: aws.String("tablename"),
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,111 @@
// Package jsonrpc provides JSON RPC utilities for serialization of AWS
// requests and responses.
package jsonrpc
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/json.json build_test.go
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/json.json unmarshal_test.go
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/protocol/json/jsonutil"
"github.com/aws/aws-sdk-go/private/protocol/rest"
)
var emptyJSON = []byte("{}")
// BuildHandler is a named request handler for building jsonrpc protocol requests
var BuildHandler = request.NamedHandler{Name: "awssdk.jsonrpc.Build", Fn: Build}
// UnmarshalHandler is a named request handler for unmarshaling jsonrpc protocol requests
var UnmarshalHandler = request.NamedHandler{Name: "awssdk.jsonrpc.Unmarshal", Fn: Unmarshal}
// UnmarshalMetaHandler is a named request handler for unmarshaling jsonrpc protocol request metadata
var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.jsonrpc.UnmarshalMeta", Fn: UnmarshalMeta}
// UnmarshalErrorHandler is a named request handler for unmarshaling jsonrpc protocol request errors
var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.jsonrpc.UnmarshalError", Fn: UnmarshalError}
// Build builds a JSON payload for a JSON RPC request.
func Build(req *request.Request) {
var buf []byte
var err error
if req.ParamsFilled() {
buf, err = jsonutil.BuildJSON(req.Params)
if err != nil {
req.Error = awserr.New("SerializationError", "failed encoding JSON RPC request", err)
return
}
} else {
buf = emptyJSON
}
if req.ClientInfo.TargetPrefix != "" || string(buf) != "{}" {
req.SetBufferBody(buf)
}
if req.ClientInfo.TargetPrefix != "" {
target := req.ClientInfo.TargetPrefix + "." + req.Operation.Name
req.HTTPRequest.Header.Add("X-Amz-Target", target)
}
if req.ClientInfo.JSONVersion != "" {
jsonVersion := req.ClientInfo.JSONVersion
req.HTTPRequest.Header.Add("Content-Type", "application/x-amz-json-"+jsonVersion)
}
}
// Unmarshal unmarshals a response for a JSON RPC service.
func Unmarshal(req *request.Request) {
defer req.HTTPResponse.Body.Close()
if req.DataFilled() {
err := jsonutil.UnmarshalJSON(req.Data, req.HTTPResponse.Body)
if err != nil {
req.Error = awserr.New("SerializationError", "failed decoding JSON RPC response", err)
}
}
return
}
// UnmarshalMeta unmarshals headers from a response for a JSON RPC service.
func UnmarshalMeta(req *request.Request) {
rest.UnmarshalMeta(req)
}
// UnmarshalError unmarshals an error response for a JSON RPC service.
func UnmarshalError(req *request.Request) {
defer req.HTTPResponse.Body.Close()
bodyBytes, err := ioutil.ReadAll(req.HTTPResponse.Body)
if err != nil {
req.Error = awserr.New("SerializationError", "failed reading JSON RPC error response", err)
return
}
if len(bodyBytes) == 0 {
req.Error = awserr.NewRequestFailure(
awserr.New("SerializationError", req.HTTPResponse.Status, nil),
req.HTTPResponse.StatusCode,
"",
)
return
}
var jsonErr jsonErrorResponse
if err := json.Unmarshal(bodyBytes, &jsonErr); err != nil {
req.Error = awserr.New("SerializationError", "failed decoding JSON RPC error response", err)
return
}
codes := strings.SplitN(jsonErr.Code, "#", 2)
req.Error = awserr.NewRequestFailure(
awserr.New(codes[len(codes)-1], jsonErr.Message, nil),
req.HTTPResponse.StatusCode,
req.RequestID,
)
}
type jsonErrorResponse struct {
Code string `json:"__type"`
Message string `json:"message"`
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,350 @@
// +build bench
package restjson_test
import (
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/private/protocol/restjson"
"github.com/aws/aws-sdk-go/service/elastictranscoder"
)
var (
elastictranscoderSvc *elastictranscoder.ElasticTranscoder
)
func TestMain(m *testing.M) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
sess := session.Must(session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials("Key", "Secret", "Token"),
Endpoint: aws.String(server.URL),
S3ForcePathStyle: aws.Bool(true),
DisableSSL: aws.Bool(true),
Region: aws.String(endpoints.UsWest2RegionID),
}))
elastictranscoderSvc = elastictranscoder.New(sess)
c := m.Run()
server.Close()
os.Exit(c)
}
func BenchmarkRESTJSONBuild_Complex_ETCCreateJob(b *testing.B) {
params := elastictranscoderCreateJobInput()
benchRESTJSONBuild(b, func() *request.Request {
req, _ := elastictranscoderSvc.CreateJobRequest(params)
return req
})
}
func BenchmarkRESTJSONBuild_Simple_ETCListJobsByPipeline(b *testing.B) {
params := elastictranscoderListJobsByPipeline()
benchRESTJSONBuild(b, func() *request.Request {
req, _ := elastictranscoderSvc.ListJobsByPipelineRequest(params)
return req
})
}
func BenchmarkRESTJSONRequest_Complex_CFCreateJob(b *testing.B) {
benchRESTJSONRequest(b, func() *request.Request {
req, _ := elastictranscoderSvc.CreateJobRequest(elastictranscoderCreateJobInput())
return req
})
}
func BenchmarkRESTJSONRequest_Simple_ETCListJobsByPipeline(b *testing.B) {
benchRESTJSONRequest(b, func() *request.Request {
req, _ := elastictranscoderSvc.ListJobsByPipelineRequest(elastictranscoderListJobsByPipeline())
return req
})
}
func benchRESTJSONBuild(b *testing.B, reqFn func() *request.Request) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
req := reqFn()
restjson.Build(req)
if req.Error != nil {
b.Fatal("Unexpected error", req.Error)
}
}
}
func benchRESTJSONRequest(b *testing.B, reqFn func() *request.Request) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
err := reqFn().Send()
if err != nil {
b.Fatal("Unexpected error", err)
}
}
}
func elastictranscoderListJobsByPipeline() *elastictranscoder.ListJobsByPipelineInput {
return &elastictranscoder.ListJobsByPipelineInput{
PipelineId: aws.String("Id"), // Required
Ascending: aws.String("Ascending"),
PageToken: aws.String("Id"),
}
}
func elastictranscoderCreateJobInput() *elastictranscoder.CreateJobInput {
return &elastictranscoder.CreateJobInput{
Input: &elastictranscoder.JobInput{ // Required
AspectRatio: aws.String("AspectRatio"),
Container: aws.String("JobContainer"),
DetectedProperties: &elastictranscoder.DetectedProperties{
DurationMillis: aws.Int64(1),
FileSize: aws.Int64(1),
FrameRate: aws.String("FloatString"),
Height: aws.Int64(1),
Width: aws.Int64(1),
},
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
FrameRate: aws.String("FrameRate"),
Interlaced: aws.String("Interlaced"),
Key: aws.String("Key"),
Resolution: aws.String("Resolution"),
},
PipelineId: aws.String("Id"), // Required
Output: &elastictranscoder.CreateJobOutput{
AlbumArt: &elastictranscoder.JobAlbumArt{
Artwork: []*elastictranscoder.Artwork{
{ // Required
AlbumArtFormat: aws.String("JpgOrPng"),
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
InputKey: aws.String("WatermarkKey"),
MaxHeight: aws.String("DigitsOrAuto"),
MaxWidth: aws.String("DigitsOrAuto"),
PaddingPolicy: aws.String("PaddingPolicy"),
SizingPolicy: aws.String("SizingPolicy"),
},
// More values...
},
MergePolicy: aws.String("MergePolicy"),
},
Captions: &elastictranscoder.Captions{
CaptionFormats: []*elastictranscoder.CaptionFormat{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Format: aws.String("CaptionFormatFormat"),
Pattern: aws.String("CaptionFormatPattern"),
},
// More values...
},
CaptionSources: []*elastictranscoder.CaptionSource{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Key: aws.String("Key"),
Label: aws.String("Name"),
Language: aws.String("Key"),
TimeOffset: aws.String("TimeOffset"),
},
// More values...
},
MergePolicy: aws.String("CaptionMergePolicy"),
},
Composition: []*elastictranscoder.Clip{
{ // Required
TimeSpan: &elastictranscoder.TimeSpan{
Duration: aws.String("Time"),
StartTime: aws.String("Time"),
},
},
// More values...
},
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Key: aws.String("Key"),
PresetId: aws.String("Id"),
Rotate: aws.String("Rotate"),
SegmentDuration: aws.String("FloatString"),
ThumbnailEncryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
ThumbnailPattern: aws.String("ThumbnailPattern"),
Watermarks: []*elastictranscoder.JobWatermark{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
InputKey: aws.String("WatermarkKey"),
PresetWatermarkId: aws.String("PresetWatermarkId"),
},
// More values...
},
},
OutputKeyPrefix: aws.String("Key"),
Outputs: []*elastictranscoder.CreateJobOutput{
{ // Required
AlbumArt: &elastictranscoder.JobAlbumArt{
Artwork: []*elastictranscoder.Artwork{
{ // Required
AlbumArtFormat: aws.String("JpgOrPng"),
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
InputKey: aws.String("WatermarkKey"),
MaxHeight: aws.String("DigitsOrAuto"),
MaxWidth: aws.String("DigitsOrAuto"),
PaddingPolicy: aws.String("PaddingPolicy"),
SizingPolicy: aws.String("SizingPolicy"),
},
// More values...
},
MergePolicy: aws.String("MergePolicy"),
},
Captions: &elastictranscoder.Captions{
CaptionFormats: []*elastictranscoder.CaptionFormat{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Format: aws.String("CaptionFormatFormat"),
Pattern: aws.String("CaptionFormatPattern"),
},
// More values...
},
CaptionSources: []*elastictranscoder.CaptionSource{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Key: aws.String("Key"),
Label: aws.String("Name"),
Language: aws.String("Key"),
TimeOffset: aws.String("TimeOffset"),
},
// More values...
},
MergePolicy: aws.String("CaptionMergePolicy"),
},
Composition: []*elastictranscoder.Clip{
{ // Required
TimeSpan: &elastictranscoder.TimeSpan{
Duration: aws.String("Time"),
StartTime: aws.String("Time"),
},
},
// More values...
},
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
Key: aws.String("Key"),
PresetId: aws.String("Id"),
Rotate: aws.String("Rotate"),
SegmentDuration: aws.String("FloatString"),
ThumbnailEncryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
ThumbnailPattern: aws.String("ThumbnailPattern"),
Watermarks: []*elastictranscoder.JobWatermark{
{ // Required
Encryption: &elastictranscoder.Encryption{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
Mode: aws.String("EncryptionMode"),
},
InputKey: aws.String("WatermarkKey"),
PresetWatermarkId: aws.String("PresetWatermarkId"),
},
// More values...
},
},
// More values...
},
Playlists: []*elastictranscoder.CreateJobPlaylist{
{ // Required
Format: aws.String("PlaylistFormat"),
HlsContentProtection: &elastictranscoder.HlsContentProtection{
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("Base64EncodedString"),
KeyMd5: aws.String("Base64EncodedString"),
KeyStoragePolicy: aws.String("KeyStoragePolicy"),
LicenseAcquisitionUrl: aws.String("ZeroTo512String"),
Method: aws.String("HlsContentProtectionMethod"),
},
Name: aws.String("Filename"),
OutputKeys: []*string{
aws.String("Key"), // Required
// More values...
},
PlayReadyDrm: &elastictranscoder.PlayReadyDrm{
Format: aws.String("PlayReadyDrmFormatString"),
InitializationVector: aws.String("ZeroTo255String"),
Key: aws.String("NonEmptyBase64EncodedString"),
KeyId: aws.String("KeyIdGuid"),
KeyMd5: aws.String("NonEmptyBase64EncodedString"),
LicenseAcquisitionUrl: aws.String("OneTo512String"),
},
},
// More values...
},
UserMetadata: map[string]*string{
"Key": aws.String("String"), // Required
// More values...
},
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,92 @@
// Package restjson provides RESTful JSON serialization of AWS
// requests and responses.
package restjson
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/input/rest-json.json build_test.go
//go:generate go run -tags codegen ../../../models/protocol_tests/generate.go ../../../models/protocol_tests/output/rest-json.json unmarshal_test.go
import (
"encoding/json"
"io/ioutil"
"strings"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/private/protocol/jsonrpc"
"github.com/aws/aws-sdk-go/private/protocol/rest"
)
// BuildHandler is a named request handler for building restjson protocol requests
var BuildHandler = request.NamedHandler{Name: "awssdk.restjson.Build", Fn: Build}
// UnmarshalHandler is a named request handler for unmarshaling restjson protocol requests
var UnmarshalHandler = request.NamedHandler{Name: "awssdk.restjson.Unmarshal", Fn: Unmarshal}
// UnmarshalMetaHandler is a named request handler for unmarshaling restjson protocol request metadata
var UnmarshalMetaHandler = request.NamedHandler{Name: "awssdk.restjson.UnmarshalMeta", Fn: UnmarshalMeta}
// UnmarshalErrorHandler is a named request handler for unmarshaling restjson protocol request errors
var UnmarshalErrorHandler = request.NamedHandler{Name: "awssdk.restjson.UnmarshalError", Fn: UnmarshalError}
// Build builds a request for the REST JSON protocol.
func Build(r *request.Request) {
rest.Build(r)
if t := rest.PayloadType(r.Params); t == "structure" || t == "" {
jsonrpc.Build(r)
}
}
// Unmarshal unmarshals a response body for the REST JSON protocol.
func Unmarshal(r *request.Request) {
if t := rest.PayloadType(r.Data); t == "structure" || t == "" {
jsonrpc.Unmarshal(r)
} else {
rest.Unmarshal(r)
}
}
// UnmarshalMeta unmarshals response headers for the REST JSON protocol.
func UnmarshalMeta(r *request.Request) {
rest.UnmarshalMeta(r)
}
// UnmarshalError unmarshals a response error for the REST JSON protocol.
func UnmarshalError(r *request.Request) {
defer r.HTTPResponse.Body.Close()
code := r.HTTPResponse.Header.Get("X-Amzn-Errortype")
bodyBytes, err := ioutil.ReadAll(r.HTTPResponse.Body)
if err != nil {
r.Error = awserr.New("SerializationError", "failed reading REST JSON error response", err)
return
}
if len(bodyBytes) == 0 {
r.Error = awserr.NewRequestFailure(
awserr.New("SerializationError", r.HTTPResponse.Status, nil),
r.HTTPResponse.StatusCode,
"",
)
return
}
var jsonErr jsonErrorResponse
if err := json.Unmarshal(bodyBytes, &jsonErr); err != nil {
r.Error = awserr.New("SerializationError", "failed decoding REST JSON error response", err)
return
}
if code == "" {
code = jsonErr.Code
}
code = strings.SplitN(code, ":", 2)[0]
r.Error = awserr.NewRequestFailure(
awserr.New(code, jsonErr.Message, nil),
r.HTTPResponse.StatusCode,
r.RequestID,
)
}
type jsonErrorResponse struct {
Code string `json:"code"`
Message string `json:"message"`
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,180 @@
package v2
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"sort"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
)
var (
errInvalidMethod = errors.New("v2 signer only handles HTTP POST")
)
const (
signatureVersion = "2"
signatureMethod = "HmacSHA256"
timeFormat = "2006-01-02T15:04:05Z"
)
type signer struct {
// Values that must be populated from the request
Request *http.Request
Time time.Time
Credentials *credentials.Credentials
Debug aws.LogLevelType
Logger aws.Logger
Query url.Values
stringToSign string
signature string
}
// SignRequestHandler is a named request handler the SDK will use to sign
// service client request with using the V4 signature.
var SignRequestHandler = request.NamedHandler{
Name: "v2.SignRequestHandler", Fn: SignSDKRequest,
}
// SignSDKRequest requests with signature version 2.
//
// Will sign the requests with the service config's Credentials object
// Signing is skipped if the credentials is the credentials.AnonymousCredentials
// object.
func SignSDKRequest(req *request.Request) {
// If the request does not need to be signed ignore the signing of the
// request if the AnonymousCredentials object is used.
if req.Config.Credentials == credentials.AnonymousCredentials {
return
}
if req.HTTPRequest.Method != "POST" && req.HTTPRequest.Method != "GET" {
// The V2 signer only supports GET and POST
req.Error = errInvalidMethod
return
}
v2 := signer{
Request: req.HTTPRequest,
Time: req.Time,
Credentials: req.Config.Credentials,
Debug: req.Config.LogLevel.Value(),
Logger: req.Config.Logger,
}
req.Error = v2.Sign()
if req.Error != nil {
return
}
if req.HTTPRequest.Method == "POST" {
// Set the body of the request based on the modified query parameters
req.SetStringBody(v2.Query.Encode())
// Now that the body has changed, remove any Content-Length header,
// because it will be incorrect
req.HTTPRequest.ContentLength = 0
req.HTTPRequest.Header.Del("Content-Length")
} else {
req.HTTPRequest.URL.RawQuery = v2.Query.Encode()
}
}
func (v2 *signer) Sign() error {
credValue, err := v2.Credentials.Get()
if err != nil {
return err
}
if v2.Request.Method == "POST" {
// Parse the HTTP request to obtain the query parameters that will
// be used to build the string to sign. Note that because the HTTP
// request will need to be modified, the PostForm and Form properties
// are reset to nil after parsing.
v2.Request.ParseForm()
v2.Query = v2.Request.PostForm
v2.Request.PostForm = nil
v2.Request.Form = nil
} else {
v2.Query = v2.Request.URL.Query()
}
// Set new query parameters
v2.Query.Set("AWSAccessKeyId", credValue.AccessKeyID)
v2.Query.Set("SignatureVersion", signatureVersion)
v2.Query.Set("SignatureMethod", signatureMethod)
v2.Query.Set("Timestamp", v2.Time.UTC().Format(timeFormat))
if credValue.SessionToken != "" {
v2.Query.Set("SecurityToken", credValue.SessionToken)
}
// in case this is a retry, ensure no signature present
v2.Query.Del("Signature")
method := v2.Request.Method
host := v2.Request.URL.Host
path := v2.Request.URL.Path
if path == "" {
path = "/"
}
// obtain all of the query keys and sort them
queryKeys := make([]string, 0, len(v2.Query))
for key := range v2.Query {
queryKeys = append(queryKeys, key)
}
sort.Strings(queryKeys)
// build URL-encoded query keys and values
queryKeysAndValues := make([]string, len(queryKeys))
for i, key := range queryKeys {
k := strings.Replace(url.QueryEscape(key), "+", "%20", -1)
v := strings.Replace(url.QueryEscape(v2.Query.Get(key)), "+", "%20", -1)
queryKeysAndValues[i] = k + "=" + v
}
// join into one query string
query := strings.Join(queryKeysAndValues, "&")
// build the canonical string for the V2 signature
v2.stringToSign = strings.Join([]string{
method,
host,
path,
query,
}, "\n")
hash := hmac.New(sha256.New, []byte(credValue.SecretAccessKey))
hash.Write([]byte(v2.stringToSign))
v2.signature = base64.StdEncoding.EncodeToString(hash.Sum(nil))
v2.Query.Set("Signature", v2.signature)
if v2.Debug.Matches(aws.LogDebugWithSigning) {
v2.logSigningInfo()
}
return nil
}
const logSignInfoMsg = `DEBUG: Request Signature:
---[ STRING TO SIGN ]--------------------------------
%s
---[ SIGNATURE ]-------------------------------------
%s
-----------------------------------------------------`
func (v2 *signer) logSigningInfo() {
msg := fmt.Sprintf(logSignInfoMsg, v2.stringToSign, v2.Query.Get("Signature"))
v2.Logger.Log(msg)
}

View file

@ -0,0 +1,237 @@
package v2
import (
"bytes"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/awstesting"
)
type signerBuilder struct {
ServiceName string
Region string
SignTime time.Time
Query url.Values
Method string
SessionToken string
}
func (sb signerBuilder) BuildSigner() signer {
endpoint := "https://" + sb.ServiceName + "." + sb.Region + ".amazonaws.com"
var req *http.Request
if sb.Method == "POST" {
body := []byte(sb.Query.Encode())
reader := bytes.NewReader(body)
req, _ = http.NewRequest(sb.Method, endpoint, reader)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", string(len(body)))
} else {
req, _ = http.NewRequest(sb.Method, endpoint, nil)
req.URL.RawQuery = sb.Query.Encode()
}
sig := signer{
Request: req,
Time: sb.SignTime,
Credentials: credentials.NewStaticCredentials(
"AKID",
"SECRET",
sb.SessionToken),
}
if os.Getenv("DEBUG") != "" {
sig.Debug = aws.LogDebug
sig.Logger = aws.NewDefaultLogger()
}
return sig
}
func TestSignRequestWithAndWithoutSession(t *testing.T) {
// have to create more than once, so use a function
newQuery := func() url.Values {
query := make(url.Values)
query.Add("Action", "CreateDomain")
query.Add("DomainName", "TestDomain-1437033376")
query.Add("Version", "2009-04-15")
return query
}
// create request without a SecurityToken (session) in the credentials
query := newQuery()
timestamp := time.Date(2015, 7, 16, 7, 56, 16, 0, time.UTC)
builder := signerBuilder{
Method: "POST",
ServiceName: "sdb",
Region: "ap-southeast-2",
SignTime: timestamp,
Query: query,
}
signer := builder.BuildSigner()
err := signer.Sign()
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := "tm4dX8Ks7pzFSVHz7qHdoJVXKRLuC4gWz9eti60d8ks=", signer.signature; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := 8, len(signer.Query); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "AKID", signer.Query.Get("AWSAccessKeyId"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "2015-07-16T07:56:16Z", signer.Query.Get("Timestamp"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "HmacSHA256", signer.Query.Get("SignatureMethod"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "2", signer.Query.Get("SignatureVersion"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "tm4dX8Ks7pzFSVHz7qHdoJVXKRLuC4gWz9eti60d8ks=", signer.Query.Get("Signature"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "CreateDomain", signer.Query.Get("Action"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "TestDomain-1437033376", signer.Query.Get("DomainName"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "2009-04-15", signer.Query.Get("Version"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
// should not have a SecurityToken parameter
_, ok := signer.Query["SecurityToken"]
if ok {
t.Errorf("expect SecurityToken found, was not")
}
// now sign again, this time with a security token (session)
query = newQuery()
builder.SessionToken = "SESSION"
signer = builder.BuildSigner()
err = signer.Sign()
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := "Ch6qv3rzXB1SLqY2vFhsgA1WQ9rnQIE2WJCigOvAJwI=", signer.signature; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := 9, len(signer.Query); e != a { // expect one more parameter
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "Ch6qv3rzXB1SLqY2vFhsgA1WQ9rnQIE2WJCigOvAJwI=", signer.Query.Get("Signature"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "SESSION", signer.Query.Get("SecurityToken"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
func TestMoreComplexSignRequest(t *testing.T) {
query := make(url.Values)
query.Add("Action", "PutAttributes")
query.Add("DomainName", "TestDomain-1437041569")
query.Add("Version", "2009-04-15")
query.Add("Attribute.2.Name", "Attr2")
query.Add("Attribute.2.Value", "Value2")
query.Add("Attribute.2.Replace", "true")
query.Add("Attribute.1.Name", "Attr1-%\\+ %")
query.Add("Attribute.1.Value", " \tValue1 +!@#$%^&*(){}[]\"';:?/.>,<\x12\x00")
query.Add("Attribute.1.Replace", "true")
query.Add("ItemName", "Item 1")
timestamp := time.Date(2015, 7, 16, 10, 12, 51, 0, time.UTC)
builder := signerBuilder{
Method: "POST",
ServiceName: "sdb",
Region: "ap-southeast-2",
SignTime: timestamp,
Query: query,
SessionToken: "SESSION",
}
signer := builder.BuildSigner()
err := signer.Sign()
if err != nil {
t.Fatalf("expect no error, got %v", err)
}
if e, a := "WNdE62UJKLKoA6XncVY/9RDbrKmcVMdQPQOTAs8SgwQ=", signer.signature; e != a {
t.Errorf("expect %v, got %v", e, a)
}
}
func TestGet(t *testing.T) {
svc := awstesting.NewClient(&aws.Config{
Credentials: credentials.NewStaticCredentials("AKID", "SECRET", "SESSION"),
Region: aws.String("ap-southeast-2"),
})
r := svc.NewRequest(
&request.Operation{
Name: "OpName",
HTTPMethod: "GET",
HTTPPath: "/",
},
nil,
nil,
)
r.Build()
if e, a := "GET", r.HTTPRequest.Method; e != a {
t.Errorf("expect %v, got %v", e, a)
}
if e, a := "", r.HTTPRequest.URL.Query().Get("Signature"); e != a {
t.Errorf("expect %v, got %v", e, a)
}
SignSDKRequest(r)
if r.Error != nil {
t.Fatalf("expect no error, got %v", r.Error)
}
t.Logf("Signature: %s", r.HTTPRequest.URL.Query().Get("Signature"))
if len(r.HTTPRequest.URL.Query().Get("Signature")) == 0 {
t.Errorf("expect signature to be set, was not")
}
}
func TestAnonymousCredentials(t *testing.T) {
svc := awstesting.NewClient(&aws.Config{
Credentials: credentials.AnonymousCredentials,
Region: aws.String("ap-southeast-2"),
})
r := svc.NewRequest(
&request.Operation{
Name: "PutAttributes",
HTTPMethod: "POST",
HTTPPath: "/",
},
nil,
nil,
)
r.Build()
SignSDKRequest(r)
req := r.HTTPRequest
req.ParseForm()
if a := req.PostForm.Get("Signature"); len(a) != 0 {
t.Errorf("expect no signature, got %v", a)
}
}

View file

@ -0,0 +1,14 @@
package util
import "sort"
// SortedKeys returns a sorted slice of keys of a map.
func SortedKeys(m map[string]interface{}) []string {
i, sorted := 0, make([]string, len(m))
for k := range m {
sorted[i] = k
i++
}
sort.Strings(sorted)
return sorted
}

109
vendor/github.com/aws/aws-sdk-go/private/util/util.go generated vendored Normal file
View file

@ -0,0 +1,109 @@
package util
import (
"bytes"
"encoding/xml"
"fmt"
"go/format"
"io"
"reflect"
"regexp"
"strings"
"github.com/aws/aws-sdk-go/private/protocol/xml/xmlutil"
)
// GoFmt returns the Go formated string of the input.
//
// Panics if the format fails.
func GoFmt(buf string) string {
formatted, err := format.Source([]byte(buf))
if err != nil {
panic(fmt.Errorf("%s\nOriginal code:\n%s", err.Error(), buf))
}
return string(formatted)
}
var reTrim = regexp.MustCompile(`\s{2,}`)
// Trim removes all leading and trailing white space.
//
// All consecutive spaces will be reduced to a single space.
func Trim(s string) string {
return strings.TrimSpace(reTrim.ReplaceAllString(s, " "))
}
// Capitalize capitalizes the first character of the string.
func Capitalize(s string) string {
if len(s) == 1 {
return strings.ToUpper(s)
}
return strings.ToUpper(s[0:1]) + s[1:]
}
// SortXML sorts the reader's XML elements
func SortXML(r io.Reader) string {
var buf bytes.Buffer
d := xml.NewDecoder(r)
root, _ := xmlutil.XMLToStruct(d, nil)
e := xml.NewEncoder(&buf)
xmlutil.StructToXML(e, root, true)
return buf.String()
}
// PrettyPrint generates a human readable representation of the value v.
// All values of v are recursively found and pretty printed also.
func PrettyPrint(v interface{}) string {
value := reflect.ValueOf(v)
switch value.Kind() {
case reflect.Struct:
str := fullName(value.Type()) + "{\n"
for i := 0; i < value.NumField(); i++ {
l := string(value.Type().Field(i).Name[0])
if strings.ToUpper(l) == l {
str += value.Type().Field(i).Name + ": "
str += PrettyPrint(value.Field(i).Interface())
str += ",\n"
}
}
str += "}"
return str
case reflect.Map:
str := "map[" + fullName(value.Type().Key()) + "]" + fullName(value.Type().Elem()) + "{\n"
for _, k := range value.MapKeys() {
str += "\"" + k.String() + "\": "
str += PrettyPrint(value.MapIndex(k).Interface())
str += ",\n"
}
str += "}"
return str
case reflect.Ptr:
if e := value.Elem(); e.IsValid() {
return "&" + PrettyPrint(e.Interface())
}
return "nil"
case reflect.Slice:
str := "[]" + fullName(value.Type().Elem()) + "{\n"
for i := 0; i < value.Len(); i++ {
str += PrettyPrint(value.Index(i).Interface())
str += ",\n"
}
str += "}"
return str
default:
return fmt.Sprintf("%#v", v)
}
}
func pkgName(t reflect.Type) string {
pkg := t.PkgPath()
c := strings.Split(pkg, "/")
return c[len(c)-1]
}
func fullName(t reflect.Type) string {
if pkg := pkgName(t); pkg != "" {
return pkg + "." + t.Name()
}
return t.Name()
}