Skip to content
This repository has been archived by the owner on Apr 30, 2021. It is now read-only.

Commit

Permalink
Fix the failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlin Ralchev committed Dec 13, 2019
1 parent 392bbce commit dc93046
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 96 deletions.
13 changes: 10 additions & 3 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/phogolabs/stride/codedom"
"github.com/phogolabs/stride/service"
"github.com/phogolabs/stride/syntax/golang"
"github.com/phogolabs/stride/syntax/markdown"
)

// OpenAPIGenerator provides a subcommands to generate source code from OpenAPI specification
Expand Down Expand Up @@ -62,9 +63,15 @@ func (m *OpenAPIGenerator) generate(ctx *cli.Context) error {
Reporter: reporter(ctx),
Cache: codedom.TypeDescriptorMap{},
},
Generator: &golang.Generator{
Reporter: reporter(ctx),
Path: dir,
Generator: service.CompositeGenerator{
&golang.Generator{
Reporter: reporter(ctx),
Path: dir,
},
&markdown.Generator{
Reporter: reporter(ctx),
Path: dir,
},
},
}

Expand Down
20 changes: 10 additions & 10 deletions fake/code_generator.go → fake/syntax_generator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions service/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,34 @@ type SpecResolver interface {
Resolve(spec *openapi3.Swagger) (*codedom.SpecDescriptor, error)
}

//go:generate counterfeiter -fake-name CodeGenerator -o ../fake/code_generator.go . CodeGenerator
//go:generate counterfeiter -fake-name SyntaxGenerator -o ../fake/syntax_generator.go . SyntaxGenerator

// CodeGenerator generates the code
type CodeGenerator interface {
// SyntaxGenerator generates the code
type SyntaxGenerator interface {
// Generate generates a source code from spec
Generate(spec *codedom.SpecDescriptor) error
}

var _ SyntaxGenerator = CompositeGenerator{}

// CompositeGenerator represents a composite generator
type CompositeGenerator []SyntaxGenerator

// Generate generates the source code
func (items CompositeGenerator) Generate(spec *codedom.SpecDescriptor) error {
for _, generator := range items {
if err := generator.Generate(spec); err != nil {
return err
}
}

return nil
}

// Generator generates the code
type Generator struct {
Path string
Generator CodeGenerator
Generator SyntaxGenerator
Resolver SpecResolver
}

Expand Down
4 changes: 2 additions & 2 deletions service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ var _ = Describe("Generator", func() {
var (
generator *service.Generator
resolver *fake.SpecResolver
coder *fake.CodeGenerator
coder *fake.SyntaxGenerator
)

BeforeEach(func() {
resolver = &fake.SpecResolver{}
resolver.ResolveReturns(&codedom.SpecDescriptor{}, nil)

coder = &fake.CodeGenerator{}
coder = &fake.SyntaxGenerator{}

generator = &service.Generator{
Path: path("../fixture/spec/schemas-array.yaml"),
Expand Down
11 changes: 0 additions & 11 deletions syntax/golang/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,6 @@ func (g *Generator) Generate(spec *codedom.SpecDescriptor) error {
return err
}

markdown := &MarkdownGenerator{
Path: g.Path,
Reporter: g.Reporter,
Info: spec.Info,
}

if err := markdown.Generate(); err != nil {
reporter.Error(" Generating spec fail")
return err
}

reporter.Success(" Generating spec complete!")
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion syntax/golang/generator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/phogolabs/stride/codedom"
"github.com/phogolabs/stride/contract"
"github.com/phogolabs/stride/inflect"
"github.com/phogolabs/stride/syntax"
)

// ControllerGeneratorMode determines the mode of this generator
Expand Down Expand Up @@ -314,7 +315,7 @@ func (g *ControllerGenerator) function(root *File, name string, ctx map[string]i
)

// mount method
writer := &TemplateWriter{
writer := &syntax.TemplateWriter{
Path: fmt.Sprintf("syntax/golang/%s.go.tpl", name),
Context: ctx,
}
Expand Down
3 changes: 2 additions & 1 deletion syntax/golang/generator_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/phogolabs/stride/contract"
"github.com/phogolabs/stride/syntax"
)

// MainGenerator builds the main
Expand All @@ -23,7 +24,7 @@ func (g *MainGenerator) Generate() *File {
reporter := g.Reporter.With(contract.SeverityHigh)
reporter.Notice(" Generating main file: %s...", filename)

writer := &TemplateWriter{
writer := &syntax.TemplateWriter{
Path: "syntax/golang/main.go.tpl",
Context: map[string]interface{}{
"command": command,
Expand Down
62 changes: 0 additions & 62 deletions syntax/golang/generator_markdown.go

This file was deleted.

3 changes: 2 additions & 1 deletion syntax/golang/generator_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/phogolabs/stride/codedom"
"github.com/phogolabs/stride/contract"
"github.com/phogolabs/stride/syntax"
)

// ServerGenerator builds a server
Expand All @@ -22,7 +23,7 @@ func (g *ServerGenerator) Generate() *File {
reporter := g.Reporter.With(contract.SeverityHigh)
reporter.Notice(" Generating server file: %s...", filename)

writer := &TemplateWriter{
writer := &syntax.TemplateWriter{
Path: "syntax/golang/server.go.tpl",
Context: map[string]interface{}{
"controllers": g.Controllers,
Expand Down
84 changes: 84 additions & 0 deletions syntax/markdown/generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package markdown

import (
"fmt"
"go/build"
"os"
"path/filepath"
"strings"

"github.com/phogolabs/stride/codedom"
"github.com/phogolabs/stride/contract"
"github.com/phogolabs/stride/inflect"
"github.com/phogolabs/stride/syntax"
)

// Generator builds the main
type Generator struct {
Path string
Reporter contract.Reporter
}

// Generate generates the source code
func (g *Generator) Generate(spec *codedom.SpecDescriptor) error {
reporter := g.Reporter.With(contract.SeverityVeryHigh)

if spec.Info == nil {
return nil
}

reporter.Notice(" Generating markdown documentation...")

project, err := filepath.Rel(filepath.Join(build.Default.GOPATH, "src"), g.Path)

if err != nil {
reporter.Error(" Generating markdown documentation fail: ", err)
return err
}

ctx := map[string]interface{}{
"command": filepath.Base(g.Path),
"project": project,
"title": strings.TrimSpace(spec.Info.Title),
"description": strings.TrimSpace(spec.Info.Description),
"version": strings.TrimSpace(spec.Info.Version),
}

// generate README.md
if err := g.sync(filepath.Join(g.Path, "README.md"), ctx); err != nil {
reporter.Error(" Generating markdown documentation fail: ", err)
return err
}

reporter.Success(" Generating markdown documentation successful!")
return nil
}

func (g *Generator) sync(path string, ctx map[string]interface{}) error {
reporter := g.Reporter.With(contract.SeverityHigh)

var (
name = inflect.LowerCase(filepath.Base(path))
writer = &syntax.TemplateWriter{
Path: fmt.Sprintf("syntax/golang/%s.tpl", name),
Context: ctx,
}
)

file, err := os.Create(path)
if err != nil {
reporter.Error(" Generating markdown file: %s fail: %v", path, err)
return err
}

defer file.Close()

if _, err := writer.WriteTo(file); err != nil {
reporter.Error(" Generating markdown file: %s fail: %v", path, err)
return err
}

reporter.Notice(" Generating markdown file: %s successful", path)
return nil

}
2 changes: 1 addition & 1 deletion syntax/golang/generator_template.go → syntax/writer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package golang
package syntax

import (
"bytes"
Expand Down

0 comments on commit dc93046

Please sign in to comment.