Skip to content

Commit

Permalink
Additional fixes for Heroku schema generation (#40)
Browse files Browse the repository at this point in the history
* Generates only links with unique titles

* Always uses named types

* Avoids creating pointer types for required response attributes
  • Loading branch information
alindeman authored and cyberdelia committed Feb 28, 2017
1 parent 30bb00e commit 6b5e959
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
24 changes: 21 additions & 3 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,29 @@ func (s *Schema) Values(name string, l *Link) []string {
} else if s.ReturnsCustomType(l) {
values = append(values, fmt.Sprintf("*%s", name), "error")
} else {
values = append(values, s.ReturnedGoType(l), "error")
values = append(values, name, "error")
}
return values
}

// UniqueLinks returns a list of links, unique by title.
//
// If more than one link in a given schema has the same title, only the one
// appearing last will appear in this list. This matches the behavior of the
// heroics Ruby gem and avoids generating structs and funcs with duplicate
// names.
func (s *Schema) UniqueLinks() []*Link {
titles := map[string]bool{}
var uniqueLinks []*Link
for _, link := range s.Links {
if _, ok := titles[link.Title]; !ok {
uniqueLinks = append(uniqueLinks, link)
}
titles[link.Title] = true
}
return uniqueLinks
}

// URL returns schema base URL.
func (s *Schema) URL() string {
for _, l := range s.Links {
Expand All @@ -260,9 +278,9 @@ func (s *Schema) ReturnsCustomType(l *Link) bool {
// ReturnedGoType returns Go type returned by the given link as a string.
func (s *Schema) ReturnedGoType(l *Link) string {
if l.TargetSchema != nil {
return l.TargetSchema.goType(true, false)
return l.TargetSchema.goType(true, true)
}
return s.goType(true, false)
return s.goType(true, true)
}

// EmptyResult retursn true if the link result should be empty.
Expand Down
27 changes: 22 additions & 5 deletions gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ var valuesTests = []struct {
Link: &Link{
Rel: "self",
},
Values: []string{"map[string]string", "error"},
Values: []string{"ConfigVar", "error"},
},
{
Schema: &Schema{},
Expand Down Expand Up @@ -500,6 +500,23 @@ var valuesTests = []struct {
},
Values: []string{"*ResultInfoResult", "error"},
},
{
Schema: &Schema{},
Name: "ConfigVar",
Link: &Link{
Rel: "self",
Title: "Info",
TargetSchema: &Schema{
Type: "object",
Properties: map[string]*Schema{
"value": {
Type: "integer",
},
},
},
},
Values: []string{"*ConfigVarInfoResult", "error"},
},
{
Schema: &Schema{
Type: "object",
Expand Down Expand Up @@ -535,7 +552,7 @@ var valuesTests = []struct {
Type: "string",
},
},
Values: []string{"string", "error"},
Values: []string{"ResultListResult", "error"},
},
{
Schema: &Schema{},
Expand All @@ -547,7 +564,7 @@ var valuesTests = []struct {
Type: []interface{}{"string", "null"},
},
},
Values: []string{"*string", "error"},
Values: []string{"ResultListResult", "error"},
},
{
Schema: &Schema{
Expand All @@ -567,7 +584,7 @@ var valuesTests = []struct {
Type: "string",
},
},
Values: []string{"string", "error"},
Values: []string{"ResultInfoResult", "error"},
},
{
Schema: &Schema{
Expand All @@ -587,7 +604,7 @@ var valuesTests = []struct {
Type: "string",
},
},
Values: []string{"string", "error"},
Values: []string{"ConfigVarInfoResult", "error"},
},
}

Expand Down
2 changes: 1 addition & 1 deletion helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,5 @@ func paramType(name string, l *Link) string {
}

func defineCustomType(s *Schema, l *Link) bool {
return l.TargetSchema != nil && s.ReturnsCustomType(l)
return l.TargetSchema != nil
}
2 changes: 1 addition & 1 deletion templates/funcs.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{$Name := .Name}}
{{$Def := .Definition}}
{{range .Definition.Links}}
{{range .Definition.UniqueLinks}}
{{if .AcceptsCustomType}}
type {{paramType $Name .}} {{linkGoType .}}
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var templates = map[string]string{"field.tmpl": `{{initialCap .Name}} {{.Type}}
`,
"funcs.tmpl": `{{$Name := .Name}}
{{$Def := .Definition}}
{{range .Definition.Links}}
{{range .Definition.UniqueLinks}}
{{if .AcceptsCustomType}}
type {{paramType $Name .}} {{linkGoType .}}
{{end}}
Expand Down

0 comments on commit 6b5e959

Please sign in to comment.