Skip to content

Commit

Permalink
Exclude methods (#58)
Browse files Browse the repository at this point in the history
* add original method name to maker.Method

* exclude methods in maker.Make()

* add repeated `-e`/`--exclude-method` flag to cli
  • Loading branch information
kazhuravlev authored Dec 8, 2022
1 parent 126f872 commit 187c382
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
32 changes: 17 additions & 15 deletions ifacemaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import (
)

type cmdlineArgs struct {
Files []string `short:"f" long:"file" description:"Go source file to read, either filename or glob" required:"true"`
StructType string `short:"s" long:"struct" description:"Generate an interface for this structure name" required:"true"`
IfaceName string `short:"i" long:"iface" description:"Name of the generated interface" required:"true"`
PkgName string `short:"p" long:"pkg" description:"Package name for the generated interface" required:"true"`
IfaceComment string `short:"y" long:"iface-comment" description:"Comment for the interface, default is '// <iface> ...'"`
ImportModule string `short:"m" long:"import-module" description:"Fully qualified module import for packages with a different target package '// <iface> ...'"`
Files []string `short:"f" long:"file" description:"Go source file to read, either filename or glob" required:"true"`
StructType string `short:"s" long:"struct" description:"Generate an interface for this structure name" required:"true"`
IfaceName string `short:"i" long:"iface" description:"Name of the generated interface" required:"true"`
PkgName string `short:"p" long:"pkg" description:"Package name for the generated interface" required:"true"`
IfaceComment string `short:"y" long:"iface-comment" description:"Comment for the interface, default is '// <iface> ...'"`
ImportModule string `short:"m" long:"import-module" description:"Fully qualified module import for packages with a different target package '// <iface> ...'"`
ExcludeMethods []string `short:"e" long:"exclude-method" description:"Name of method that will be excluded from output interface"`

// jessevdk/go-flags doesn't support default values for boolean flags,
// so we use a string for backwards-compatibility and then convert it to a bool later.
Expand Down Expand Up @@ -61,15 +62,16 @@ func main() {
files = append(files, matches...)
}
result, err := maker.Make(maker.MakeOptions{
Files: files,
StructType: args.StructType,
Comment: args.Comment,
PkgName: args.PkgName,
IfaceName: args.IfaceName,
IfaceComment: args.IfaceComment,
CopyDocs: args.copyDocs,
CopyTypeDoc: args.CopyTypeDoc,
ImportModule: args.ImportModule,
Files: files,
StructType: args.StructType,
Comment: args.Comment,
PkgName: args.PkgName,
IfaceName: args.IfaceName,
IfaceComment: args.IfaceComment,
CopyDocs: args.copyDocs,
CopyTypeDoc: args.CopyTypeDoc,
ImportModule: args.ImportModule,
ExcludeMethods: args.ExcludeMethods,
})
if err != nil {
log.Fatal(err.Error())
Expand Down
33 changes: 23 additions & 10 deletions maker/maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// Method describes the code and documentation
// tied into a method
type Method struct {
Name string
Code string
Docs []string
}
Expand Down Expand Up @@ -266,14 +267,16 @@ func ParseStruct(src []byte, structName string, copyDocs bool, copyTypeDocs bool
}
params := FormatFieldList(src, fd.Type.Params, pkgName, declaredTypes)
ret := FormatFieldList(src, fd.Type.Results, pkgName, declaredTypes)
method := fmt.Sprintf("%s(%s) (%s)", fd.Name.String(), strings.Join(params, ", "), strings.Join(ret, ", "))
mName := fd.Name.String()
method := fmt.Sprintf("%s(%s) (%s)", mName, strings.Join(params, ", "), strings.Join(ret, ", "))
var docs []string
if fd.Doc != nil && copyDocs {
for _, d := range fd.Doc.List {
docs = append(docs, string(src[d.Pos()-1:d.End()-1]))
}
}
methods = append(methods, Method{
Name: mName,
Code: method,
Docs: docs,
})
Expand All @@ -295,15 +298,16 @@ func ParseStruct(src []byte, structName string, copyDocs bool, copyTypeDocs bool

// MakeOptions contains options for the Make function.
type MakeOptions struct {
Files []string
StructType string
Comment string
PkgName string
IfaceName string
IfaceComment string
ImportModule string
CopyDocs bool
CopyTypeDoc bool
Files []string
StructType string
Comment string
PkgName string
IfaceName string
IfaceComment string
ImportModule string
CopyDocs bool
CopyTypeDoc bool
ExcludeMethods []string
}

func Make(options MakeOptions) ([]byte, error) {
Expand Down Expand Up @@ -334,6 +338,11 @@ func Make(options MakeOptions) ([]byte, error) {
}
}

excludedMethods := make(map[string]struct{}, len(options.ExcludeMethods))
for _, mName := range options.ExcludeMethods {
excludedMethods[mName] = struct{}{}
}

// Second pass to build up the interface
for _, f := range options.Files {
src, err := ioutil.ReadFile(f)
Expand All @@ -342,6 +351,10 @@ func Make(options MakeOptions) ([]byte, error) {
}
methods, imports, parsedTypeDoc := ParseStruct(src, options.StructType, options.CopyDocs, options.CopyTypeDoc, options.PkgName, allDeclaredTypes, options.ImportModule)
for _, m := range methods {
if _, ok := excludedMethods[m.Name]; ok {
continue
}

if _, ok := mset[m.Code]; !ok {
allMethods = append(allMethods, m.Lines()...)
mset[m.Code] = struct{}{}
Expand Down

0 comments on commit 187c382

Please sign in to comment.