Skip to content

Commit 8efa542

Browse files
authored
Merge pull request #40 from donutloop/refactoring
restructured packages
2 parents 3e89c75 + 46b46f7 commit 8efa542

File tree

6 files changed

+100
-96
lines changed

6 files changed

+100
-96
lines changed

cmd/xcode/main.go

Lines changed: 11 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
11
package main
22

33
import (
4-
"bytes"
54
"flag"
65
"fmt"
7-
"go/ast"
8-
"go/format"
9-
"go/parser"
10-
"go/token"
6+
"github.com/donutloop/toolkit/internal/ast"
117
"io/ioutil"
128
"log"
139
"os"
14-
"strings"
1510
"text/tabwriter"
16-
"github.com/fatih/astrewrite"
1711
)
1812

1913
func main() {
20-
log.SetFlags(log.Ldate | log.Lshortfile | log.Ltime)
14+
log.SetFlags(0)
2115

2216
fs := flag.NewFlagSet("xcode", flag.ExitOnError)
2317
var (
24-
in = fs.String("in", "", "input file")
25-
out = fs.String("out", "", "output file")
26-
pkg = fs.String("pkg", "", "package name")
27-
typ = fs.String("type", "", "type")
28-
mode = fs.String("mode", "", "activate mode")
18+
in = fs.String("in", "", "input file")
19+
out = fs.String("out", "", "output file")
20+
pkg = fs.String("pkg", "", "package name")
21+
typ = fs.String("type", "", "type")
22+
mode = fs.String("mode", "", "activate mode")
2923
)
3024
fs.Usage = usageFor(fs, "xcode [flags]")
3125
fs.Parse(os.Args[1:])
@@ -51,10 +45,10 @@ func main() {
5145
log.Fatalf("could not read file (%v)", err)
5246
}
5347

54-
rnFunc := RenamePackage(*pkg)
55-
ctFunc := ChangeType("GenericType", *typ, *mode)
48+
rnFunc := ast.RenamePackage(*pkg)
49+
ctFunc := ast.ChangeType("GenericType", *typ, *mode)
5650

57-
modifiedFile, err := modifyAst(inputFile, rnFunc, ctFunc)
51+
modifiedFile, err := ast.ModifyAst(inputFile, rnFunc, ctFunc)
5852
if err != nil {
5953
log.Fatalf("could not modify ast of file (%v)", err)
6054
}
@@ -64,73 +58,6 @@ func main() {
6458
}
6559
}
6660

67-
const (
68-
DebugMode string = "DEV"
69-
)
70-
71-
func RenamePackage(packageName string) func(file *ast.File) *ast.File {
72-
return func(file *ast.File) *ast.File {
73-
file.Name = &ast.Ident{Name: packageName}
74-
return file
75-
}
76-
}
77-
78-
func ChangeType(typeName string, newType string, debugMode string) func(file *ast.File) *ast.File {
79-
return func(file *ast.File) *ast.File {
80-
rewriteFunc := func(n ast.Node) (ast.Node, bool) {
81-
switch x := n.(type) {
82-
case *ast.Ident:
83-
if typeName == x.Name {
84-
x = &ast.Ident{Name: newType}
85-
}
86-
return x, true
87-
case *ast.CallExpr:
88-
for i := 0; i < len(x.Args); i++ {
89-
v, ok := x.Args[i].(*ast.Ident)
90-
if ok {
91-
if strings.ToLower(typeName) == strings.ToLower(v.Name) {
92-
x.Args[i] = &ast.Ident{Name: fmt.Sprintf("%s.(%s)", v.Name, newType)}
93-
}
94-
}
95-
}
96-
return x, true
97-
default:
98-
if debugMode == DebugMode {
99-
log.SetFlags(0)
100-
log.Println("ast node:")
101-
log.Println(fmt.Sprintf("verbose value: %#v", x))
102-
log.Println(fmt.Sprintf("type: %T", x))
103-
log.Println(fmt.Sprintf("value: %v", x))
104-
}
105-
}
106-
return n, true
107-
}
108-
109-
astrewrite.Walk(file, rewriteFunc)
110-
111-
return file
112-
}
113-
}
114-
115-
func modifyAst(dest []byte, fns ...func(*ast.File) *ast.File) ([]byte, error) {
116-
destFset := token.NewFileSet()
117-
destF, err := parser.ParseFile(destFset, "", dest, 0)
118-
if err != nil {
119-
return nil, err
120-
}
121-
122-
for _, fn := range fns {
123-
destF = fn(destF)
124-
}
125-
126-
var buf bytes.Buffer
127-
if err := format.Node(&buf, destFset, destF); err != nil {
128-
return nil, fmt.Errorf("couldn't format package code (%v)", err)
129-
}
130-
131-
return buf.Bytes(), nil
132-
}
133-
13461
func usageFor(fs *flag.FlagSet, short string) func() {
13562
return func() {
13663
fmt.Fprintf(os.Stdout, "USAGE\n")
@@ -139,7 +66,7 @@ func usageFor(fs *flag.FlagSet, short string) func() {
13966
fmt.Fprintf(os.Stdout, "FLAGS\n")
14067
tw := tabwriter.NewWriter(os.Stdout, 0, 2, 2, ' ', 0)
14168
fs.VisitAll(func(f *flag.Flag) {
142-
if f.Name == "mode" {
69+
if f.Name == "debug" {
14370
return
14471
}
14572
def := f.DefValue
@@ -151,4 +78,3 @@ func usageFor(fs *flag.FlagSet, short string) func() {
15178
tw.Flush()
15279
}
15380
}
154-

internal/ast/ast.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package ast
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"github.com/fatih/astrewrite"
7+
"go/ast"
8+
"go/format"
9+
"go/parser"
10+
"go/token"
11+
"log"
12+
"strings"
13+
)
14+
15+
const (
16+
DebugMode string = "DEV"
17+
)
18+
19+
func RenamePackage(packageName string) func(file *ast.File) *ast.File {
20+
return func(file *ast.File) *ast.File {
21+
file.Name = &ast.Ident{Name: packageName}
22+
return file
23+
}
24+
}
25+
26+
func ChangeType(typeName string, newType string, debugMode string) func(file *ast.File) *ast.File {
27+
return func(file *ast.File) *ast.File {
28+
rewriteFunc := func(n ast.Node) (ast.Node, bool) {
29+
switch x := n.(type) {
30+
case *ast.Ident:
31+
if typeName == x.Name {
32+
x = &ast.Ident{Name: newType}
33+
}
34+
return x, true
35+
case *ast.CallExpr:
36+
for i := 0; i < len(x.Args); i++ {
37+
v, ok := x.Args[i].(*ast.Ident)
38+
if ok {
39+
if strings.ToLower(typeName) == strings.ToLower(v.Name) {
40+
x.Args[i] = &ast.Ident{Name: fmt.Sprintf("%s.(%s)", v.Name, newType)}
41+
}
42+
}
43+
}
44+
return x, true
45+
default:
46+
if debugMode == DebugMode {
47+
log.Println("ast node:")
48+
log.Println(fmt.Sprintf("verbose value: %#v", x))
49+
log.Println(fmt.Sprintf("type: %T", x))
50+
log.Println(fmt.Sprintf("value: %v", x))
51+
}
52+
}
53+
return n, true
54+
}
55+
56+
astrewrite.Walk(file, rewriteFunc)
57+
58+
return file
59+
}
60+
}
61+
62+
func ModifyAst(dest []byte, fns ...func(*ast.File) *ast.File) ([]byte, error) {
63+
destFset := token.NewFileSet()
64+
destF, err := parser.ParseFile(destFset, "", dest, 0)
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
for _, fn := range fns {
70+
destF = fn(destF)
71+
}
72+
73+
var buf bytes.Buffer
74+
if err := format.Node(&buf, destFset, destF); err != nil {
75+
return nil, fmt.Errorf("couldn't format package code (%v)", err)
76+
}
77+
78+
return buf.Bytes(), nil
79+
}

worker/doc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func ExampleWorker() {
1515
queue := worker.New(2, workerHandler, 10)
1616

1717
queue <- "hello"
18-
<- time.After(time.Millisecond * 250)
18+
<-time.After(time.Millisecond * 250)
1919

2020
// Output: hello
2121
}

worker/worker.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import (
55
)
66

77
type worker struct {
8-
c chan interface{}
8+
c chan interface{}
99
done chan bool
1010
jobs chan interface{}
11-
fn func(n GenericType)
12-
buf *list.List
11+
fn func(n GenericType)
12+
buf *list.List
1313
}
1414

1515
// NewWorker starts n*Workers goroutines running func on incoming
1616
// parameters sent on the returned channel.
1717
func New(nWorkers uint, fn func(gt GenericType), buffer uint) chan<- interface{} {
1818
retc := make(chan interface{}, buffer)
1919
w := &worker{
20-
c: retc,
20+
c: retc,
2121
jobs: make(chan interface{}, buffer),
2222
done: make(chan bool),
23-
fn: fn,
24-
buf: list.New(),
23+
fn: fn,
24+
buf: list.New(),
2525
}
2626
go w.listener()
2727
for i := uint(0); i < nWorkers; i++ {

worker/worker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestWorker(t *testing.T) {
2626
return
2727
}
2828

29-
if !containes([]string{"hello", "golang", "world"}, v) {
29+
if !containes([]string{"hello", "golang", "world"}, v) {
3030
t.Errorf("value is bad got=%v", parameter)
3131
}
3232

@@ -40,7 +40,7 @@ func TestWorker(t *testing.T) {
4040
queue <- "golang"
4141
queue <- "world"
4242

43-
<- time.After(500 * time.Millisecond)
43+
<-time.After(500 * time.Millisecond)
4444

4545
if atomic.LoadInt32(&counter) != 3 {
4646
t.Errorf("counter is bad (want=3, got=%v)", atomic.LoadInt32(&counter))

worker/worker_type.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package worker
22

33
// used for code generation replacing
4-
type GenericType interface {}
5-
4+
type GenericType interface{}

0 commit comments

Comments
 (0)