Skip to content

Commit

Permalink
Support sharing of shapes across models
Browse files Browse the repository at this point in the history
  • Loading branch information
lsegal committed Jul 14, 2015
1 parent 7a7223b commit cc85221
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion internal/model/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type API struct {
imports map[string]bool
name string
unrecognizedNames map[string]string
path string
}

// A Metadata is the metadata about an API's definition.
Expand Down Expand Up @@ -192,7 +193,7 @@ var tplAPI = template.Must(template.New("api").Parse(`
{{ end }}
{{ range $_, $s := .ShapeList }}
{{ if eq $s.Type "structure" }}{{ $s.GoCode }}{{ end }}
{{ if and $s.IsInternal (eq $s.Type "structure") }}{{ $s.GoCode }}{{ end }}
{{ end }}
`))
Expand Down
30 changes: 26 additions & 4 deletions internal/model/api/customization_passes.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package api

var svcCustomizations = map[string]func(*API){
"s3": s3Customizations,
"cloudfront": cloudfrontCustomizations,
}
import (
"path/filepath"
"strings"
)

// customizationPasses Executes customization logic for the API by package name.
func (a *API) customizationPasses() {
var svcCustomizations = map[string]func(*API){
"s3": s3Customizations,
"cloudfront": cloudfrontCustomizations,
"dynamodbstreams": dynamodbstreamsCustomizations,
}

if fn := svcCustomizations[a.PackageName()]; fn != nil {
fn(a)
}
Expand Down Expand Up @@ -38,3 +44,19 @@ func cloudfrontCustomizations(a *API) {
}
}
}

// dynamodbstreamsCustomizations references any duplicate shapes from DynamoDB
func dynamodbstreamsCustomizations(a *API) {
p := strings.Replace(a.path, "streams.dynamodb", "dynamodb", -1)
file := filepath.Join(p, "api-2.json")

dbAPI := API{}
dbAPI.Attach(file)
dbAPI.Setup()

for n := range a.Shapes {
if _, ok := dbAPI.Shapes[n]; ok {
a.Shapes[n].resolvePkg = "github.com/aws/aws-sdk-go/service/dynamodb"
}
}
}
2 changes: 2 additions & 0 deletions internal/model/api/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"path/filepath"
)

// Load takes a set of files for each filetype and returns an API pointer.
Expand All @@ -24,6 +25,7 @@ func Load(api, docs, paginators, waiters string) *API {
// 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 {
Expand Down
22 changes: 18 additions & 4 deletions internal/model/api/shape.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"fmt"
"path"
"sort"
"strings"
"text/template"
Expand Down Expand Up @@ -52,7 +53,8 @@ type Shape struct {
LocationName string
XMLNamespace XMLInfo

refs []*ShapeRef // References to this shape
refs []*ShapeRef // References to this shape
resolvePkg string // use this package in the goType() if present
}

// Rename changes the name of the Shape to newName. Also updates
Expand Down Expand Up @@ -113,8 +115,15 @@ func (ref *ShapeRef) GoTypeWithPkgName() string {
func goType(s *Shape, withPkgName bool) string {
switch s.Type {
case "structure":
if withPkgName {
return fmt.Sprintf("*%s.%s", s.API.PackageName(), s.ShapeName)
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":
Expand Down Expand Up @@ -299,7 +308,7 @@ func (s *Shape) GoCode() string {
code += "SDKShapeTraits bool " + ref.GoTags(true, false)
code += "}"

if (!s.API.NoStringerMethods) {
if !s.API.NoStringerMethods {
code += s.goCodeStringers()
}
default:
Expand All @@ -318,3 +327,8 @@ func (s *Shape) IsRequired(member string) bool {
}
return false
}

// IsInternal returns whether the shape was defined in this package
func (s *Shape) IsInternal() bool {
return s.resolvePkg == ""
}

0 comments on commit cc85221

Please sign in to comment.