Skip to content

Commit

Permalink
support exclude option (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysugimoto authored Apr 18, 2020
1 parent 26c2f4e commit 51b3e34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
14 changes: 3 additions & 11 deletions example/greeter/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
.PHONY: build

build:
cd ../../ && make
protoc \
-I. \
-I./grpc-gateway \
--plugin=../../dist/protoc-gen-graphql \
--go_out=plugins=grpc:./gateway \
--graphql_out=verbose:./gateway \
./grpc-gateway/google/api/annotations.proto
#protoc \
# -I. \
# --plugin=../../dist/protoc-gen-graphql \
# --go_out=plugins=grpc:./greeter \
# --graphql_out=verbose:./greeter \
# greeter.proto
--go_out=plugins=grpc:./greeter \
--graphql_out=verbose:./greeter \
greeter.proto
1 change: 1 addition & 0 deletions protoc-gen-graphql/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (g *Generator) Generate(tmpl string, fs []string) ([]*plugin.CodeGeneratorR
if f.Filename() != v {
continue
}

s, ok := services[f.Package()]
if !ok {
continue
Expand Down
18 changes: 13 additions & 5 deletions protoc-gen-graphql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,18 @@ func main() {
}

g := generator.New(files, args)
genFiles, err := g.Generate(goTemplate, req.GetFileToGenerate())
if err != nil {
genError = err
return
var ftg []string
for _, f := range req.GetFileToGenerate() {
if !args.IsExclude(f) {
ftg = append(ftg, f)
}
}
if len(ftg) > 0 {
genFiles, err := g.Generate(goTemplate, ftg)
if err != nil {
genError = err
return
}
resp.File = append(resp.File, genFiles...)
}
resp.File = append(resp.File, genFiles...)
}
24 changes: 23 additions & 1 deletion protoc-gen-graphql/spec/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ package spec

import (
"errors"
"regexp"
"strings"
)

// Params spec have plugin parameters
type Params struct {
QueryOut string
Excludes []*regexp.Regexp
Verbose bool
}

func NewParams(p string) (*Params, error) {
params := &Params{}
params := &Params{
Excludes: []*regexp.Regexp{},
}
if p == "" {
return params, nil
}
Expand All @@ -27,9 +31,27 @@ func NewParams(p string) (*Params, error) {
return nil, errors.New("argument " + kv[0] + " must have value")
}
params.QueryOut = kv[1]
case "exclude":
if len(kv) == 1 {
return nil, errors.New("argument " + kv[0] + " must have value")
}
regex, err := regexp.Compile(kv[1])
if err != nil {
return nil, errors.New("failed to compile regex for exclude argument " + kv[1])
}
params.Excludes = append(params.Excludes, regex)
default:
return nil, errors.New("Unacceptable argument " + kv[0] + " provided")
}
}
return params, nil
}

func (p *Params) IsExclude(pkg string) bool {
for _, r := range p.Excludes {
if r.MatchString(pkg) {
return true
}
}
return false
}

0 comments on commit 51b3e34

Please sign in to comment.