Skip to content

Commit 9cd3b37

Browse files
committed
Allow multiple interface invocation
1 parent 8af989b commit 9cd3b37

File tree

2 files changed

+77
-58
lines changed

2 files changed

+77
-58
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Add `./vendor/` to package path if the dependency is vendored; when using Go mod
4242

4343
Set the `circuit-major-version` flag if using Go modules and major version 3 or later. This makes the wrappers import the same version as the rest of your code.
4444

45+
Note you can also pass multiple --name, or multiple names separated by comma in order to generate multiple interfaces at once. Note that while doing this
46+
the --out parameter has to be a directory and you cannot use an alias.
47+
4548
## Example
4649

4750
Generating the DynamoDB client into the wrappers directory with circuits aliased as "DynamoDB"

circuit.go

+74-58
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ func (t *circuitWrapperTemplateContext) IsInterface() bool {
197197

198198
type circuitCmd struct {
199199
pkg string
200-
name string
200+
name []string
201201
out string
202202
alias string
203203
majorVersion int
@@ -220,10 +220,10 @@ func (c *circuitCmd) Cobra() *cobra.Command {
220220
pf.StringVar(&c.pkg, "pkg", "", "(Required) The path to the package. Add ./vendor if the dependency is vendored")
221221
markFlagRequired(pf, "pkg")
222222

223-
pf.StringVar(&c.name, "name", "", "(Required) The name of the type (interface or struct) in the package path")
223+
pf.StringSliceVar(&c.name, "name", []string{}, "(Required) The name of the type (interface or struct) in the package path")
224224
markFlagRequired(pf, "name")
225225

226-
pf.StringVar(&c.out, "out", "", "(Required) The output path. A default filename is given if the path looks like a directory. The path is lazily created (equivalent to mkdir -p)")
226+
pf.StringVar(&c.out, "out", "", "(Required) The output path. A default filename is given if the path looks like a directory. The path is lazily created (equivalent to mkdir -p). Must be a directory of passing multiple names")
227227
markFlagRequired(pf, "out")
228228

229229
pf.StringVar(&c.alias, "alias", "", "(Optional) The name used for the generated wrapper in the struct, constructor, and default circuit prefix. Defaults to name")
@@ -243,12 +243,15 @@ func markFlagRequired(pf *pflag.FlagSet, name string) {
243243
}
244244

245245
func (c *circuitCmd) Execute() error {
246-
if c.alias == "" {
247-
c.alias = c.name
248-
}
249-
250-
if !strings.HasSuffix(c.out, ".go") {
251-
c.out = filepath.Join(c.out, strings.ToLower(c.alias)+".gen.go")
246+
if len(c.name) > 1 {
247+
if c.alias != "" {
248+
return errors.New("unable to use alias with multiple interface invocation")
249+
}
250+
if strings.HasSuffix(c.out, ".go") {
251+
return errors.New("must specify directory as filename if generating multiple interfaces")
252+
}
253+
} else if c.alias == "" && len(c.name) > 0 {
254+
c.alias = c.name[0]
252255
}
253256

254257
if err := c.gen(); err != nil {
@@ -273,62 +276,75 @@ func (c *circuitCmd) gen() error {
273276

274277
pkg := pkgs[0]
275278

276-
obj := pkg.Types.Scope().Lookup(c.name)
277-
if obj == nil {
278-
return errors.New("could not lookup name")
279-
}
279+
for _, name := range(c.name) {
280+
alias := c.alias
281+
if alias == "" {
282+
alias = name
283+
}
284+
out := c.out
285+
if !strings.HasSuffix(out, ".go") {
286+
out = filepath.Join(out, strings.ToLower(alias)+".gen.go")
287+
}
288+
if len(c.name) > 1 {
289+
c.log("generating %s as %s => %s", name, alias, out)
290+
}
291+
obj := pkg.Types.Scope().Lookup(name)
292+
if obj == nil {
293+
return errors.New("could not lookup name")
294+
}
280295

281-
typ := obj.Type()
282-
if typ == nil {
283-
return errors.New("object is not a type")
284-
}
296+
typ := obj.Type()
297+
if typ == nil {
298+
return errors.New("object is not a type")
299+
}
285300

286-
s = time.Now()
287-
outPkgPath, err := resolvePackagePath(c.out)
288-
if err != nil {
289-
return err
290-
}
291-
c.log("resolvePackagePath took %v", time.Since(s))
301+
s = time.Now()
302+
outPkgPath, err := resolvePackagePath(out)
303+
if err != nil {
304+
return err
305+
}
306+
c.log("resolvePackagePath took %v", time.Since(s))
292307

293-
outPkgName := filepath.Base(outPkgPath)
308+
outPkgName := filepath.Base(outPkgPath)
294309

295-
s = time.Now()
296-
typeMeta, err := parseType(typ, outPkgPath)
297-
if err != nil {
298-
return err
299-
}
300-
c.log("parseType took %v", time.Since(s))
310+
s = time.Now()
311+
typeMeta, err := parseType(typ, outPkgPath)
312+
if err != nil {
313+
return err
314+
}
315+
c.log("parseType took %v", time.Since(s))
301316

302-
templateCtx := circuitWrapperTemplateContext{
303-
PackageName: outPkgName,
304-
VersionSuffix: circuitVersionSuffix(c.majorVersion),
305-
TypeMetadata: typeMeta,
306-
Alias: c.alias,
307-
}
317+
templateCtx := circuitWrapperTemplateContext{
318+
PackageName: outPkgName,
319+
VersionSuffix: circuitVersionSuffix(c.majorVersion),
320+
TypeMetadata: typeMeta,
321+
Alias: alias,
322+
}
308323

309-
s = time.Now()
310-
var b bytes.Buffer
311-
err = circuitWrapperTemplate.Execute(&b, &templateCtx)
312-
if err != nil {
313-
return fmt.Errorf("rendering circuit wrapper: %v", err)
314-
}
315-
c.log("executing circuit wrapper template took %v", time.Since(s))
316-
317-
s = time.Now()
318-
var src []byte
319-
if c.goimports {
320-
src, err = imports.Process("<gen>", b.Bytes(), nil)
321-
} else {
322-
src, err = format.Source(b.Bytes())
323-
}
324-
if err != nil {
325-
return fmt.Errorf("formatting rendered circuit wrapper: %v", err)
326-
}
327-
c.log("formatting code took %v", time.Since(s))
324+
s = time.Now()
325+
var b bytes.Buffer
326+
err = circuitWrapperTemplate.Execute(&b, &templateCtx)
327+
if err != nil {
328+
return fmt.Errorf("rendering circuit wrapper: %v", err)
329+
}
330+
c.log("executing circuit wrapper template took %v", time.Since(s))
331+
332+
s = time.Now()
333+
var src []byte
334+
if c.goimports {
335+
src, err = imports.Process("<gen>", b.Bytes(), nil)
336+
} else {
337+
src, err = format.Source(b.Bytes())
338+
}
339+
if err != nil {
340+
return fmt.Errorf("formatting rendered circuit wrapper: %v", err)
341+
}
342+
c.log("formatting code took %v", time.Since(s))
328343

329-
err = writeFile(c.out, src)
330-
if err != nil {
331-
return fmt.Errorf("writing circuit wrapper file: %v", err)
344+
err = writeFile(out, src)
345+
if err != nil {
346+
return fmt.Errorf("writing circuit wrapper file: %v", err)
347+
}
332348
}
333349

334350
return nil

0 commit comments

Comments
 (0)