forked from cheekybits/genny
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_example_test.go
79 lines (71 loc) · 2.02 KB
/
_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Given this generic Go code (github.com/cheekybits/genny/tree/master/examples/queue)
// which compiles and is tested:
package main_test
import (
"fmt"
"os"
"os/exec"
"github.com/cheekybits/genny/generic"
)
// NOTE: this is how easy it is to define a generic type
type Something generic.Type
// SomethingQueue is a queue of Somethings.
type SomethingQueue struct {
items []Something
}
func NewSomethingQueue() *SomethingQueue {
return &SomethingQueue{items: make([]Something, 0)}
}
func (q *SomethingQueue) Push(item Something) {
q.items = append(q.items, item)
}
func (q *SomethingQueue) Pop() Something {
item := q.items[0]
q.items = q.items[1:]
return item
}
func Example() {
// When 'genny gen' is invoked like this:
//
// cat source.go | genny gen "Something=string"
//
// Excluding this function you should get the output shown below.
// You can write scripts in GO that generate files for you but using
// 'go generate' is still the recommended way.
thisFile := "${GOPATH}/src/github.com/cheekybits/genny/example_queue_test.go"
thisFile = os.ExpandEnv(thisFile)
err := exec.Command("genny", "-in="+thisFile, "gen",
"Something=string").Run()
if err != nil {
fmt.Println(err)
if exitErr, ok := err.(*exec.ExitError); ok {
fmt.Println(string(exitErr.Stderr))
}
}
// Similar Output:
// // Code generated by genny - Generics for Go; DO NOT EDIT.
// // More information at github.com/cheekybits/genny
// // or using 'go doc github.com/cheekybits/genny'
//
// // Given this generic Go code (github.com/cheekybits/genny/tree/master/examples/queue)
// // which compiles and is tested:
//
// package main_test
//
// // StringQueue is a queue of Strings.
// type StringQueue struct {
// items []string
// }
//
// func NewStringQueue() *StringQueue {
// return &StringQueue{items: make([]string, 0)}
// }
// func (q *StringQueue) Push(item string) {
// q.items = append(q.items, item)
// }
// func (q *StringQueue) Pop() string {
// item := q.items[0]
// q.items = q.items[1:]
// return item
// }
}