Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
Move all the business logic of parsing and generating code
into maker subpackage.

Instead of functions, the maker package exports a Maker struct,
which has two exported methods: ParseSource to add a file,
and MakeInterface to create the interface.

The struct now handles the data that was previously handled
by the main package between the invocation of the two functions.

Don't use token.Pos to access source file byte slice,
this doesn't work when there are multiple files in the fileset.
Use go/printer or directly get the parsed strings from the ast nodes.

Ignore dot imports, assume they will not be needed by the
generated struct.

Throw errors when aliases conflict or a package is imported
with different aliases (vburenin#9).

Add a "Code generated by" comment per
https://golang.org/s/generatedcode

Add tests + travis.
  • Loading branch information
nkovacs committed Jun 29, 2017
1 parent d8a0157 commit 6f0f281
Show file tree
Hide file tree
Showing 5 changed files with 746 additions and 107 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: go
go:
- 1.4
- 1.5
- 1.6
- 1.7
- 1.8
- tip
sudo: false
go_import_path: github.com/vburenin/ifacemaker # required for forks to build properly
install: scripts/deps.sh
26 changes: 9 additions & 17 deletions ifacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"log"
"path/filepath"

"github.com/mkideal/cli"
"github.com/vburenin/ifacemaker/maker"
Expand All @@ -20,31 +21,22 @@ type cmdlineArgs struct {
}

func run(args *cmdlineArgs) {
allMethods := []string{}
allImports := []string{}
mset := make(map[string]struct{})
iset := make(map[string]struct{})
maker := &maker.Maker{
StructName: args.StructType,
CopyDocs: args.CopyDocs,
}
for _, f := range args.Files {
src, err := ioutil.ReadFile(f)
if err != nil {
log.Fatal(err.Error())
}
methods, imports := maker.ParseStruct(src, args.StructType, args.CopyDocs)
for _, m := range methods {
if _, ok := mset[m.Code]; !ok {
allMethods = append(allMethods, m.Lines()...)
mset[m.Code] = struct{}{}
}
}
for _, i := range imports {
if _, ok := iset[i]; !ok {
allImports = append(allImports, i)
iset[i] = struct{}{}
}
err = maker.ParseSource(src, filepath.Base(f))
if err != nil {
log.Fatal(err.Error())
}
}

result, err := maker.MakeInterface(args.PkgName, args.IfaceName, allMethods, allImports)
result, err := maker.MakeInterface(args.PkgName, args.IfaceName)
if err != nil {
log.Fatal(err.Error())
}
Expand Down
Loading

0 comments on commit 6f0f281

Please sign in to comment.