| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"errors" |
| 7 |
"flag" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
"log" |
| 11 |
"os" |
| 12 |
"strings" |
| 13 |
"text/template" |
| 14 |
) |
| 15 |
|
| 16 |
type ( |
| 17 |
Type struct { |
| 18 |
Notation string |
| 19 |
Spec string |
| 20 |
Prototype string |
| 21 |
} |
| 22 |
|
| 23 |
TypeMap map[string]Type |
| 24 |
|
| 25 |
Imports []string |
| 26 |
|
| 27 |
Config struct { |
| 28 |
Package string |
| 29 |
Imports Imports |
| 30 |
Args string |
| 31 |
Types TypeMap |
| 32 |
} |
| 33 |
) |
| 34 |
|
| 35 |
func (i *Imports) String() string { |
| 36 |
if i == nil { |
| 37 |
return "" |
| 38 |
} |
| 39 |
return fmt.Sprintf("%#v", ([]string)(*i)) |
| 40 |
} |
| 41 |
|
| 42 |
func (i *Imports) Set(s string) error { |
| 43 |
*i = append(*i, s) |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func (tm *TypeMap) Set(s string) error { |
| 48 |
tok := strings.SplitN(s, ":", 3) |
| 49 |
|
| 50 |
if len(tok) < 2 { |
| 51 |
return errors.New("expected {notation}:{type-spec} syntax, instead of " + s) |
| 52 |
} |
| 53 |
|
| 54 |
if *tm == nil { |
| 55 |
*tm = make(TypeMap) |
| 56 |
} |
| 57 |
|
| 58 |
t := (*tm)[tok[0]] |
| 59 |
t.Notation, t.Spec = tok[0], tok[1] |
| 60 |
|
| 61 |
if t.Notation == "" { |
| 62 |
return fmt.Errorf("type notation in %q may not be an empty string", s) |
| 63 |
} |
| 64 |
|
| 65 |
if t.Spec == "" { |
| 66 |
return fmt.Errorf("type specification in %q may not be an empty string", s) |
| 67 |
} |
| 68 |
|
| 69 |
if len(tok) == 3 { |
| 70 |
t.Prototype = tok[2] |
| 71 |
|
| 72 |
if t.Prototype == "" { |
| 73 |
return fmt.Errorf("prototype specification in %q may not be an empty string", s) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
(*tm)[tok[0]] = t |
| 78 |
return nil |
| 79 |
} |
| 80 |
|
| 81 |
func (tm *TypeMap) String() string { |
| 82 |
if tm == nil { |
| 83 |
return "" |
| 84 |
} |
| 85 |
return fmt.Sprintf("%#v", *tm) |
| 86 |
} |
| 87 |
|
| 88 |
func (c *Config) Var(notation string, names ...string) string { |
| 89 |
t := c.Type(notation) |
| 90 |
if t == "" || len(names) == 0 { |
| 91 |
return "" |
| 92 |
} |
| 93 |
return "var " + strings.Join(names, ",") + " " + t |
| 94 |
} |
| 95 |
|
| 96 |
func (c *Config) Arg(notation, name string) string { |
| 97 |
t := c.Type(notation) |
| 98 |
if t == "" { |
| 99 |
return "" |
| 100 |
} |
| 101 |
if name == "" { |
| 102 |
return t |
| 103 |
} |
| 104 |
if strings.HasSuffix(name, ",") { |
| 105 |
return strings.TrimSpace(name[:len(name)-1]+" "+t) + ", " |
| 106 |
} |
| 107 |
return name + " " + t |
| 108 |
} |
| 109 |
|
| 110 |
func (c *Config) Ref(notation, name string) (string, error) { |
| 111 |
t := c.Type(notation) |
| 112 |
if t == "" || name == "" { |
| 113 |
return "", nil |
| 114 |
} |
| 115 |
if strings.HasSuffix(name, ",") { |
| 116 |
if len(name) < 2 { |
| 117 |
return "", errors.New("expected ref name before comma") |
| 118 |
} |
| 119 |
return name[:len(name)-1] + ", ", nil |
| 120 |
} |
| 121 |
return name, nil |
| 122 |
} |
| 123 |
|
| 124 |
func (c *Config) RequireType(notation string) (string, error) { |
| 125 |
_, ok := c.Types[notation] |
| 126 |
if !ok { |
| 127 |
return "", fmt.Errorf("type %q is required but not specified", notation) |
| 128 |
} |
| 129 |
return "", nil |
| 130 |
} |
| 131 |
|
| 132 |
func (c *Config) RequirePrototype(notation string) (string, error) { |
| 133 |
t, ok := c.Types[notation] |
| 134 |
if !ok { |
| 135 |
|
| 136 |
return "", nil |
| 137 |
} |
| 138 |
if t.Prototype == "" { |
| 139 |
return "", fmt.Errorf("prototype for type %q is required but not specified", notation) |
| 140 |
} |
| 141 |
return "", nil |
| 142 |
} |
| 143 |
|
| 144 |
func (c *Config) Type(notation string) string { |
| 145 |
t, ok := c.Types[notation] |
| 146 |
if !ok { |
| 147 |
return "" |
| 148 |
} |
| 149 |
return t.Spec |
| 150 |
} |
| 151 |
|
| 152 |
func (c *Config) Prototype(notation string) string { |
| 153 |
t, ok := c.Types[notation] |
| 154 |
if !ok { |
| 155 |
return "" |
| 156 |
} |
| 157 |
return t.Prototype |
| 158 |
} |
| 159 |
|
| 160 |
func (c *Config) AddFlags(fs *flag.FlagSet) { |
| 161 |
fs.StringVar(&c.Package, "package", c.Package, "destination package") |
| 162 |
fs.Var(&c.Imports, "import", "packages to import") |
| 163 |
fs.Var(&c.Types, "type", "auxilliary type mappings in {notation}:{type-spec}:{prototype-expr} format") |
| 164 |
} |
| 165 |
|
| 166 |
func NewConfig() *Config { |
| 167 |
var ( |
| 168 |
c = Config{ |
| 169 |
Package: os.Getenv("GOPACKAGE"), |
| 170 |
} |
| 171 |
) |
| 172 |
return &c |
| 173 |
} |
| 174 |
|
| 175 |
func Run(src, test *template.Template, args ...string) { |
| 176 |
if len(args) < 1 { |
| 177 |
panic(errors.New("expected at least one arg")) |
| 178 |
} |
| 179 |
var ( |
| 180 |
c = NewConfig() |
| 181 |
defaultOutput = "foo.go" |
| 182 |
output string |
| 183 |
) |
| 184 |
if c.Package != "" { |
| 185 |
defaultOutput = c.Package + "_generated.go" |
| 186 |
} |
| 187 |
|
| 188 |
fs := flag.NewFlagSet(args[0], flag.PanicOnError) |
| 189 |
fs.StringVar(&output, "output", output, "path of the to-be-generated file") |
| 190 |
c.AddFlags(fs) |
| 191 |
|
| 192 |
if err := fs.Parse(args[1:]); err != nil { |
| 193 |
if err == flag.ErrHelp { |
| 194 |
fs.PrintDefaults() |
| 195 |
} |
| 196 |
panic(err) |
| 197 |
} |
| 198 |
|
| 199 |
c.Args = strings.Join(args[1:], " ") |
| 200 |
|
| 201 |
if c.Package == "" { |
| 202 |
c.Package = "foo" |
| 203 |
} |
| 204 |
|
| 205 |
if output == "" { |
| 206 |
output = defaultOutput |
| 207 |
} |
| 208 |
|
| 209 |
genmap := make(map[string]*template.Template) |
| 210 |
if src != nil { |
| 211 |
genmap[output] = src |
| 212 |
} |
| 213 |
if test != nil { |
| 214 |
testOutput := output + "_test" |
| 215 |
if strings.HasSuffix(output, ".go") { |
| 216 |
testOutput = output[:len(output)-3] + "_test.go" |
| 217 |
} |
| 218 |
genmap[testOutput] = test |
| 219 |
} |
| 220 |
if len(genmap) == 0 { |
| 221 |
panic(errors.New("neither src or test templates were provided")) |
| 222 |
} |
| 223 |
|
| 224 |
Generate(genmap, c, func(err error) { panic(err) }) |
| 225 |
} |
| 226 |
|
| 227 |
func Generate(items map[string]*template.Template, data interface{}, eh func(error)) { |
| 228 |
for filename, t := range items { |
| 229 |
func() { |
| 230 |
f, err := os.Create(filename) |
| 231 |
if err != nil { |
| 232 |
eh(err) |
| 233 |
return |
| 234 |
} |
| 235 |
closer := safeClose(f) |
| 236 |
defer closer() |
| 237 |
|
| 238 |
log.Println("generating file", filename) |
| 239 |
err = t.Execute(f, data) |
| 240 |
if err != nil { |
| 241 |
eh(err) |
| 242 |
} |
| 243 |
err = f.Sync() |
| 244 |
if err != nil { |
| 245 |
eh(err) |
| 246 |
} |
| 247 |
err = closer() |
| 248 |
if err != nil { |
| 249 |
eh(err) |
| 250 |
} |
| 251 |
}() |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
func safeClose(c io.Closer) func() error { |
| 256 |
var s bool |
| 257 |
return func() error { |
| 258 |
if s { |
| 259 |
return nil |
| 260 |
} |
| 261 |
s = true |
| 262 |
return c.Close() |
| 263 |
} |
| 264 |
} |