diff --git a/gen.go b/gen.go index 5244235..f8d7360 100644 --- a/gen.go +++ b/gen.go @@ -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 @@ -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 @@ -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 @@ -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 } diff --git a/gen_test.go b/gen_test.go index b9efbeb..adff2c2 100644 --- a/gen_test.go +++ b/gen_test.go @@ -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()) + } +}