Skip to content

Commit

Permalink
added ability to ignore interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
matryer committed Feb 22, 2020
1 parent 1ac0822 commit 4c7213b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ type GreetResponse struct {
Use the `oto` tool to generate a client and server:

```bash
oto -template ./otohttp/templates/server.go.plush -out ./api/oto.gen.go ./api/definitions
oto -template ./otohttp/templates/server.go.plush \
-out ./api/oto.gen.go \
-ignore Ignorer \
./api/definitions
gofmt -w ./api/oto.gen.go ./api/oto.gen.go
```

Expand Down
15 changes: 10 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ flags:`)
flags.PrintDefaults()
}
var (
template = flags.String("template", "", "plush template to render")
outfile = flags.String("out", "", "output file (default: stdout)")
pkg = flags.String("pkg", "", "explicit package name (default: inferred)")
v = flags.Bool("v", false, "verbose output")
paramsStr = flags.String("params", "", "list of parameters in the format: \"key:value,key:value\"")
template = flags.String("template", "", "plush template to render")
outfile = flags.String("out", "", "output file (default: stdout)")
pkg = flags.String("pkg", "", "explicit package name (default: inferred)")
v = flags.Bool("v", false, "verbose output")
paramsStr = flags.String("params", "", "list of parameters in the format: \"key:value,key:value\"")
ignoreList = flags.String("ignore", "", "comma separated list of interfaces to ignore")
)
if err := flags.Parse(args[1:]); err != nil {
return err
Expand All @@ -46,6 +47,10 @@ flags:`)
return errors.Wrap(err, "params")
}
parser := newParser(flags.Args()...)
ignoreItems := strings.Split(*ignoreList, ",")
if ignoreItems[0] != "" {
parser.ExcludeInterfaces = ignoreItems
}
parser.Verbose = *v
if parser.Verbose {
fmt.Println("oto - github.com/pacedotdev/oto")
Expand Down
38 changes: 37 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (f fieldType) JSType() (string, error) {
type parser struct {
Verbose bool

ExcludeInterfaces []string

patterns []string
def definition

Expand All @@ -112,6 +114,7 @@ func (p *parser) parse() (definition, error) {
}
p.outputObjects = make(map[string]struct{})
p.objects = make(map[string]struct{})
var excludedObjectsTypeIDs []string
pkgs, err := packages.Load(cfg, p.patterns...)
if err != nil {
return p.def, err
Expand All @@ -127,12 +130,35 @@ func (p *parser) parse() (definition, error) {
if err != nil {
return p.def, err
}
if isInSlice(p.ExcludeInterfaces, name) {
for _, method := range s.Methods {
excludedObjectsTypeIDs = append(excludedObjectsTypeIDs, method.InputObject.TypeID)
excludedObjectsTypeIDs = append(excludedObjectsTypeIDs, method.OutputObject.TypeID)
}
continue
}
p.def.Services = append(p.def.Services, s)
case *types.Struct:
p.parseObject(pkg, obj, item)
}
}
}
// remove any excluded objects
nonExcludedObjects := make([]object, 0, len(p.def.Objects))
for _, object := range p.def.Objects {
excluded := false
for _, excludedTypeID := range excludedObjectsTypeIDs {
if object.TypeID == excludedTypeID {
excluded = true
break
}
}
if excluded {
continue
}
nonExcludedObjects = append(nonExcludedObjects, object)
}
p.def.Objects = nonExcludedObjects
sort.Slice(p.def.Services, func(i, j int) bool {
return p.def.Services[i].Name < p.def.Services[j].Name
})
Expand Down Expand Up @@ -275,7 +301,8 @@ func (p *parser) addOutputFields() error {
for typeName := range p.outputObjects {
obj, err := p.def.Object(typeName)
if err != nil {
return errors.Wrapf(err, "missing output object: %s", typeName)
// skip if we can't find it - it must be excluded
continue
}
obj.Fields = append(obj.Fields, errorField)
}
Expand All @@ -286,3 +313,12 @@ func (p *parser) wrapErr(err error, pkg *packages.Package, pos token.Pos) error
position := pkg.Fset.Position(pos)
return errors.Wrap(err, position.String())
}

func isInSlice(slice []string, s string) bool {
for i := range slice {
if slice[i] == s {
return true
}
}
return false
}
6 changes: 4 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
func TestParse(t *testing.T) {
is := is.New(t)
patterns := []string{"./testdata/services/pleasantries"}
def, err := newParser(patterns...).parse()
parser := newParser(patterns...)
parser.ExcludeInterfaces = []string{"Ignorer"}
def, err := parser.parse()
is.NoErr(err)

is.Equal(def.PackageName, "pleasantries")
is.Equal(len(def.Services), 2)
is.Equal(len(def.Services), 2) // should be 2 services
is.Equal(def.Services[0].Name, "GreeterService")
is.Equal(len(def.Services[0].Methods), 2)
is.Equal(def.Services[0].Methods[0].Name, "GetGreetings")
Expand Down
9 changes: 9 additions & 0 deletions testdata/services/pleasantries/ignorer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pleasantries

type Ignorer interface {
Ignore(IgnoreRequest) IgnoreResponse
}

type IgnoreRequest struct{}

type IgnoreResponse struct{}

0 comments on commit 4c7213b

Please sign in to comment.