Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
tidy up name regarding walk project to get annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
imantung committed Oct 8, 2019
1 parent 0e9a63c commit ecedf03
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions EXPERIMENTAL/typicmd/internal/prebuilder/prebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ func PreBuild(ctx *typictx.Context) (err error) {
root := typienv.AppName
projPkgs, filenames, _ := projectFiles(root)
configuration := createConfiguration(ctx)
report, err := walker.Walk(filenames)
projFiles, err := walker.WalkProject(filenames)
if err != nil {
return
}
return runn.Execute(
typienv.WriteEnvIfNotExist(ctx),
prepareTestTargets(projPkgs),
generateAnnotated(report),
generateAnnotated(projFiles),
generateConfiguration(configuration),
)
}
Expand All @@ -56,7 +56,7 @@ func generateTestTargets(name string, testTargets []string) error {
)
}

func generateAnnotated(files *walker.Files) error {
func generateAnnotated(files *walker.ProjectFiles) error {
defer elapsed("Generate Annotated")()
pkg := typienv.Dependency.Package
name := "annotateds.go"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package walker

// File of walk analysis
type File struct {
// ProjectFile of walk analysis
type ProjectFile struct {
Name string
Mock bool
Constructors []string
}

// IsEmpty is true if empty truct
func (f *File) IsEmpty() bool {
func (f *ProjectFile) IsEmpty() bool {
return !f.Mock && len(f.Constructors) < 1
}

// AddConstructor to add contructor to file
func (f *File) AddConstructor(constructor string) *File {
func (f *ProjectFile) AddConstructor(constructor string) *ProjectFile {
f.Constructors = append(f.Constructors, constructor)
return f
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
package walker

// Files information
type Files []File
// ProjectFiles information
type ProjectFiles []ProjectFile

// Add item to files
func (f *Files) Add(item File) {
func (f *ProjectFiles) Add(item ProjectFile) {
*f = append(*f, item)
}

// Autowires return autowired constructors
func (f *Files) Autowires() (constructors []string) {
func (f *ProjectFiles) Autowires() (constructors []string) {
for _, file := range *f {
constructors = append(constructors, file.Constructors...)
}
return
}

// Automocks return automocked filenames
func (f *Files) Automocks() (filenames []string) {
func (f *ProjectFiles) Automocks() (filenames []string) {
for _, file := range *f {
if file.Mock {
filenames = append(filenames, file.Name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ import (
"strings"
)

// Walk the source code to get autowire and automock
func Walk(filenames []string) (f *Files, err error) {
f = &Files{}
// WalkProject the source code to get autowire and automock
func WalkProject(filenames []string) (files *ProjectFiles, err error) {
files = &ProjectFiles{}
fset := token.NewFileSet() // positions are relative to fset
for _, filename := range filenames {
if walkTarget(filename) {
var file File
if isWalkTarget(filename) {
var file ProjectFile
file, err = parse(fset, filename)
if err != nil {
return
}
if !file.IsEmpty() {
f.Add(file)
files.Add(file)
}
}
}
return
}

func walkTarget(filename string) bool {
func isWalkTarget(filename string) bool {
return strings.HasSuffix(filename, ".go") &&
!strings.HasSuffix(filename, "_test.go")
}

func parse(fset *token.FileSet, filename string) (file File, err error) {
func parse(fset *token.FileSet, filename string) (projFile ProjectFile, err error) {
f, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return
}
file.Name = filename
projFile.Name = filename
for objName, obj := range f.Scope.Objects {
switch obj.Decl.(type) {
case *ast.FuncDecl:
Expand All @@ -47,7 +47,7 @@ func parse(fset *token.FileSet, filename string) (file File, err error) {
godoc = funcDecl.Doc.Text()
}
if isAutoWire(objName, godoc) {
file.AddConstructor(fmt.Sprintf("%s.%s", f.Name, objName))
projFile.AddConstructor(fmt.Sprintf("%s.%s", f.Name, objName))
}
case *ast.TypeSpec:
typeSpec := obj.Decl.(*ast.TypeSpec)
Expand All @@ -58,7 +58,7 @@ func parse(fset *token.FileSet, filename string) (file File, err error) {
if typeSpec.Doc != nil {
doc = typeSpec.Doc.Text()
}
file.Mock = isAutoMock(doc)
projFile.Mock = isAutoMock(doc)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ func TestWalkTarget(t *testing.T) {
{"file", false},
}
for _, tt := range testcases {
require.Equal(t, tt.result, walkTarget(tt.filename))
require.Equal(t, tt.result, isWalkTarget(tt.filename))
}
}
6 changes: 3 additions & 3 deletions typical/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package typical
import (
"github.com/typical-go/typical-rest-server/EXPERIMENTAL/typictx"
"github.com/typical-go/typical-rest-server/app"
"github.com/typical-go/typical-rest-server/app/config"
cfg "github.com/typical-go/typical-rest-server/app/config"
"github.com/typical-go/typical-rest-server/pkg/module/typpostgres"
"github.com/typical-go/typical-rest-server/pkg/module/typredis"
"github.com/typical-go/typical-rest-server/pkg/module/typserver"
)

// Context instance of Context
var Context = &typictx.Context{
Root: "github.com/typical-go/typical-rest-server",
Name: "Typical-RESTful-Server",
Description: "Example of typical and scalable RESTful API Server for Go",
Root: "github.com/typical-go/typical-rest-server",
Application: typictx.Application{
StartFunc: app.Start,
Config: typictx.Config{
Prefix: "APP",
Spec: &config.Config{},
Spec: &cfg.Config{},
},
Initiations: []interface{}{
app.Middlewares,
Expand Down

0 comments on commit ecedf03

Please sign in to comment.