Skip to content

Commit

Permalink
Regenerating services
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Jun 1, 2017
1 parent 9c6e483 commit 6e45f8d
Show file tree
Hide file tree
Showing 104 changed files with 19,371 additions and 79,711 deletions.
120 changes: 111 additions & 9 deletions private/model/api/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -141,6 +142,8 @@ func generateTypes(ex Example) string {

// 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 buildShape(ref *ShapeRef, shapes map[string]interface{}, isMap bool) string {
order := make([]string, len(shapes))
for k := range shapes {
Expand All @@ -161,45 +164,142 @@ func buildShape(ref *ShapeRef, shapes map[string]interface{}, isMap bool) string
memName := name
if isMap {
memName = fmt.Sprintf("%q", memName)
} else if ref != nil {
}

switch v := shape.(type) {
case map[string]interface{}:
ret += buildComplex(name, memName, ref, v)
case []interface{}:
ret += buildList(ref, name, memName, v)
default:
ret += buildScalar(name, memName, ref, v)
}
}
return ret
}

func buildList(ref *ShapeRef, name, memName string, v []interface{}) string {
ret := ""

if len(v) == 0 || ref == nil {
return ""
}

t := ""
format := ""
isComplex := false
passRef := ref

switch v[0].(type) {
case string:
t = "string"
format = "%s"
case bool:
t = "bool"
format = "%t"
case int:
t = "int64"
format = "%d"
case float64:
t = "float64"
format = "%f"
default:
pkgPrefix := ref.API.PackageName() + "."
if ref.Shape.MemberRefs[name] != nil {
t = pkgPrefix + ref.Shape.MemberRefs[name].Shape.ShapeName
passRef = ref.Shape.MemberRefs[name]
} else {
t = pkgPrefix + ref.Shape.MemberRef.Shape.ShapeName
passRef = &ref.Shape.MemberRef
}
isComplex = true
}
ret += fmt.Sprintf("%s: []*%s {\n", memName, t)
for _, elem := range v {
if isComplex {
ret += fmt.Sprintf("{\n%s\n},\n", buildShape(passRef, elem.(map[string]interface{}), false))
} else {
ret += fmt.Sprintf("%s,\n", getValue(t, fmt.Sprintf(format, elem)))
}
}
ret += "},\n"
return ret
}

func buildScalar(name, memName string, ref *ShapeRef, shape interface{}) string {
if ref == nil || ref.Shape == nil || ref.Shape.MemberRefs[name] == nil {
return correctType(memName, shape)
}

switch v := shape.(type) {
case bool:
return fmt.Sprintf("%s: aws.Bool(%t),\n", memName, v)
return convertToCorrectType(memName, ref.Shape.MemberRefs[name].Shape.Type, fmt.Sprintf("%t", v))
case int:
if ref.Shape.MemberRefs[name].Shape.Type == "timestamp" {
return parseTimeString(ref, memName, fmt.Sprintf("%d", v))
} else {
return fmt.Sprintf("%s: aws.Int64(%d),\n", memName, v)
return convertToCorrectType(memName, ref.Shape.MemberRefs[name].Shape.Type, fmt.Sprintf("%d", v))
}
case float64:
return convertToCorrectType(memName, ref.Shape.MemberRefs[name].Shape.Type, fmt.Sprintf("%f", v))
case string:
if ref != nil && ref.Shape.MemberRefs[name] != nil && ref.Shape.MemberRefs[name].Shape.Type == "timestamp" {
t := ref.Shape.MemberRefs[name].Shape.Type
switch t {
case "timestamp":
return parseTimeString(ref, memName, fmt.Sprintf("%s", v))
} else if ref != nil && ref.Shape.MemberRefs[name] != nil && ref.Shape.MemberRefs[name].Shape.Type == "blob" {
case "blob":
if (ref.Shape.MemberRefs[name].Streaming || ref.Shape.MemberRefs[name].Shape.Streaming) && ref.Shape.Payload == name {
return fmt.Sprintf("%s: aws.ReadSeekCloser(bytes.NewBuffer([]byte(%q))),\n", memName, v)
} else {
return fmt.Sprintf("%s: []byte(%q),\n", memName, v)
}
} else {
return fmt.Sprintf("%s: aws.String(%q),\n", memName, v)
default:
return convertToCorrectType(memName, t, v)
}
default:
panic(fmt.Errorf("Unsupported scalar type: %v", reflect.TypeOf(v)))
}
return ""
}

func correctType(memName string, value interface{}) string {
if value == nil {
return ""
}

switch v := value.(type) {
case string:
return convertToCorrectType(memName, "string", v)
case int:
return convertToCorrectType(memName, "integer", fmt.Sprintf("%d", v))
case float64:
return convertToCorrectType(memName, "float", fmt.Sprintf("%f", v))
case bool:
return convertToCorrectType(memName, "boolean", fmt.Sprintf("%t", v))
default:
panic(fmt.Sprintf("Unsupported type: %v", v))
}
}

func convertToCorrectType(memName, t, v string) string {
return fmt.Sprintf("%s: %s,\n", memName, getValue(t, v))
}

func getValue(t, v string) string {
switch t {
case "string":
return fmt.Sprintf("aws.String(%q)", v)
case "integer", "long":
return fmt.Sprintf("aws.Int64(%s)", v)
case "float", "float64":
return fmt.Sprintf("aws.Float64(%s)", v)
case "boolean":
return fmt.Sprintf("aws.Bool(%s)", v)
default:
panic("Unsupported type: " + t)
}
}

func buildComplex(name, memName string, ref *ShapeRef, v map[string]interface{}) string {
shapeName, t := "", ""
member := ref.Shape.MemberRefs[name]
Expand Down Expand Up @@ -232,6 +332,8 @@ func buildComplex(name, memName string, ref *ShapeRef, v map[string]interface{})
// AttachExamples will create a new ExamplesDefinition from the examples file
// and reference the API object.
func (a *API) AttachExamples(filename string) {
a.Setup()
a.customizationPasses()
p := ExamplesDefinition{API: a}

f, err := os.Open(filename)
Expand Down Expand Up @@ -274,8 +376,8 @@ import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/{{ .PackageName }}"
)
Expand Down Expand Up @@ -332,7 +434,7 @@ func getNestedType(shape *Shape) string {
case "structure":
return fmt.Sprintf("*%s.%s", shape.API.PackageName(), shape.ShapeName)
default:
panic("Unsupported shape " + shape.ValueRef.Shape.Type)
panic("Unsupported shape " + shape.Type)
}
}

Expand Down
48 changes: 34 additions & 14 deletions private/model/api/example_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build codegen
// +build 1.6,codegen

package api

Expand All @@ -13,6 +13,7 @@ func buildAPI() *API {
stringShape := &Shape{
API: a,
ShapeName: "string",
Type: "string",
}
stringShapeRef := &ShapeRef{
API: a,
Expand All @@ -23,6 +24,7 @@ func buildAPI() *API {
intShape := &Shape{
API: a,
ShapeName: "int",
Type: "int",
}
intShapeRef := &ShapeRef{
API: a,
Expand Down Expand Up @@ -114,24 +116,40 @@ func TestExampleGeneration(t *testing.T) {
def.API = a

def.setup()
expected := `import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/fooservice"
expected := `
import (
"fmt"
"bytes"
"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 _ bytes.Buffer
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() {
func ExampleFooService_Foo_shared00() {
svc := fooservice.New(session.New())
input := FooInput{
BarShape: "Hello world",
input := &fooservice.FooInput{
BarShape: aws.String("Hello world"),
}
result, err := svc.Foo(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
Expand All @@ -150,7 +168,9 @@ func ExampleFooService_Foo() {
}
`
if expected != a.ExamplesGoCode() {
t.Errorf("Expected:\n%s\n\nReceived:\n%s\n", expected, a.ExamplesGoCode())
t.Log([]byte(expected))
t.Log([]byte(a.ExamplesGoCode()))
t.Errorf("Expected:\n%s\nReceived:\n%s\n", expected, a.ExamplesGoCode())
}
}

Expand All @@ -164,13 +184,13 @@ func TestBuildShape(t *testing.T) {
defs: map[string]interface{}{
"barShape": "Hello World",
},
expected: "BarShape: \"Hello World\",\n",
expected: "BarShape: aws.String(\"Hello World\"),\n",
},
{
defs: map[string]interface{}{
"BarShape": "Hello World",
},
expected: "BarShape: \"Hello World\",\n",
expected: "BarShape: aws.String(\"Hello World\"),\n",
},
}

Expand Down
Loading

0 comments on commit 6e45f8d

Please sign in to comment.