Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #36 #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,31 @@ func (s *Schema) Generate() ([]byte, error) {
}

name := strings.ToLower(strings.Split(s.Title, " ")[0])
templates.ExecuteTemplate(&buf, "package.tmpl", name)

if s.Title == "" {
name = "main"
}

err := templates.ExecuteTemplate(&buf, "package.tmpl", name)

if err != nil {
return nil, fmt.Errorf("failed to execute the package.tmpl with error " + err.Error())
}

// TODO: Check if we need time.
templates.ExecuteTemplate(&buf, "imports.tmpl", []string{
err = templates.ExecuteTemplate(&buf, "imports.tmpl", []string{
"encoding/json", "fmt", "io", "reflect",
"net/http", "runtime", "time", "bytes",
// TODO: Change for google/go-querystring if pull request #5 gets merged
// https://github.com/google/go-querystring/pull/5
"github.com/ernesto-jimenez/go-querystring/query",
})
templates.ExecuteTemplate(&buf, "service.tmpl", struct {

if err != nil {
return nil, fmt.Errorf("failed to execute the imports.tmpl with error %s", err.Error())
}

err = templates.ExecuteTemplate(&buf, "service.tmpl", struct {
Name string
URL string
Version string
Expand All @@ -47,8 +61,14 @@ func (s *Schema) Generate() ([]byte, error) {
Version: s.Version,
})

if err != nil {
return nil, fmt.Errorf("failed to execute the service.tmpl with error %s", err.Error())
}

for _, name := range sortedKeys(s.Properties) {

schema := s.Properties[name]

// Skipping definitions because there is no links, nor properties.
if schema.Links == nil && schema.Properties == nil {
continue
Expand All @@ -62,8 +82,17 @@ func (s *Schema) Generate() ([]byte, error) {
Definition: schema,
}

templates.ExecuteTemplate(&buf, "struct.tmpl", context)
err = templates.ExecuteTemplate(&buf, "struct.tmpl", context)

if err != nil {
return nil, fmt.Errorf("failed to execute the struct.tmpl for property %s with error %s", name, err.Error())
}

templates.ExecuteTemplate(&buf, "funcs.tmpl", context)

if err != nil {
return nil, fmt.Errorf("failed to execute the funcs.tmpl for property %s with error %s", name, err.Error())
}
}

// Remove blank lines added by text/template
Expand All @@ -72,7 +101,7 @@ func (s *Schema) Generate() ([]byte, error) {
// Format sources
clean, err := format.Source(bytes)
if err != nil {
return buf.Bytes(), err
return buf.Bytes(), fmt.Errorf("failed to format the output source code with error %s", err.Error())
}
return clean, nil
}
Expand Down
20 changes: 20 additions & 0 deletions gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,23 @@ func TestValues(t *testing.T) {
}
}
}

func TestThatMissingNamesAreAllowed(t *testing.T) {
s := &Schema{
Properties: map[string]*Schema{
"name": &Schema{
Type: "string",
},
},
}
bytes, err := s.Generate()

code := string(bytes)
if !strings.Contains(code, "package main") {
t.Error("if the schema title is omitted, then the package should default to being called \"main\".")
}

if err != nil {
t.Fatal("if a schema title is omitted, the schema should still generate without failure, but the following error was found: " + err.Error())
}
}