Skip to content

Commit

Permalink
fix: nested pattern test
Browse files Browse the repository at this point in the history
  • Loading branch information
0daryo committed Jun 24, 2021
1 parent 42e2a39 commit 900b70e
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 29 deletions.
6 changes: 4 additions & 2 deletions example/example.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions example/example_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion example/proto/user.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ import "google/api/annotations.proto";
package gateway;

service UserService {
rpc GetStatus(UserRequest) returns (UserResponse) {
rpc GetUser(UserRequest) returns (UserResponse) {
option (google.api.http) = {
get: "/users/{userId}"
body: "*"
};
}
rpc GetNestedUser(UserRequest) returns (UserResponse) {
option (google.api.http) = {
get: "/users/{userId}/nesteds"
body: "*"
};
}
rpc GetNestedNestedUser(UserRequest) returns (UserResponse) {
option (google.api.http) = {
get: "/users/{userId}/nesteds/{nestedId}"
body: "*"
};
}
}

message UserRequest {
Expand Down
128 changes: 109 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"path/filepath"
"regexp"
"sort"
"strings"

"github.com/emicklei/proto"
)
Expand All @@ -19,9 +21,14 @@ var (
protoDir = flag.String("d", "proto/service", "proto directory name")
)

const (
testPathVar = "aA1_-"
)

type patternRegexp struct {
Pattern string
RegexPattern string
TestPath string
}

func main() {
Expand Down Expand Up @@ -58,13 +65,7 @@ func main() {
proto.WithRPC(handleRPC),
)
}
if err := deleteIfExist(*output); err != nil {
panic(err)
}
file, err := os.Create(*output)
if err != nil {
panic(err)
}

tp := struct {
Pkg string
PatternRegexp []patternRegexp
Expand All @@ -76,41 +77,82 @@ func main() {
ret = append(ret, patternRegexp{
Pattern: k,
RegexPattern: v,
TestPath: r.ReplaceAllString(k, testPathVar),
})
}
sort.SliceStable(ret, func(i, j int) bool {
return ret[i].Pattern < ret[j].Pattern
})
return ret
}(),
}
b := new(bytes.Buffer)
if err := tpl.Execute(b, tp); err != nil {
panic(err)

// generate code
{
if err := deleteIfExist(*output); err != nil {
panic(err)
}
file, err := os.Create(*output)
if err != nil {
panic(err)
}
b := new(bytes.Buffer)
if err := tpl.Execute(b, tp); err != nil {
panic(err)
}
formatted, err := format.Source(b.Bytes())
if err != nil {
panic(err)
}
file.Write(formatted)
}
formatted, err := format.Source(b.Bytes())
if err != nil {
panic(err)
// generate test code
{
testPath := strings.TrimSuffix(*output, filepath.Ext(*output)) + "_test.go"
if err := deleteIfExist(testPath); err != nil {
panic(err)
}
file, err := os.Create(testPath)
if err != nil {
panic(err)
}

b := new(bytes.Buffer)
if err := testTpl.Execute(b, tp); err != nil {
panic(err)
}
formatted, err := format.Source(b.Bytes())
if err != nil {
panic(err)
}
file.Write(formatted)
}
file.Write(formatted)
}

var r = regexp.MustCompile("{.*}")
var r = regexp.MustCompile("{[a-zA-Z0-9_]*}")

func handleRPC(s *proto.RPC) {
for _, o := range s.Options {
get, ok := o.Constant.Map["get"]
if ok {
pattern := "^" + r.ReplaceAllString(get.Source, "[a-zA-Z0-9]*") + "$"
pattern := "^" + r.ReplaceAllString(get.Source, "[-a-zA-Z0-9_]*") + "$"
patternToRegexp[get.Source] = pattern
}
post, ok := o.Constant.Map["post"]
if ok {
pattern := "^" + r.ReplaceAllString(post.Source, "[a-zA-Z0-9]*") + "$"
pattern := "^" + r.ReplaceAllString(post.Source, "[-a-zA-Z0-9_]*") + "$"
patternToRegexp[post.Source] = pattern
}
put, ok := o.Constant.Map["put"]
if ok {
pattern := "^" + r.ReplaceAllString(put.Source, "[a-zA-Z0-9]*") + "$"
pattern := "^" + r.ReplaceAllString(put.Source, "[-a-zA-Z0-9_]*") + "$"
patternToRegexp[put.Source] = pattern
}
delete, ok := o.Constant.Map["delete"]
if ok {
pattern := "^" + r.ReplaceAllString(delete.Source, "[-a-zA-Z0-9_]*") + "$"
patternToRegexp[delete.Source] = pattern
}
}
}

Expand All @@ -127,7 +169,8 @@ func isDir(name string) (bool, error) {
}
}

var tpl = template.Must(template.New("maps").Parse(text))
var tpl = template.Must(template.New("code").Parse(text))
var testTpl = template.Must(template.New("test").Parse(testText))

const text = `
// Code generated by pogen. DO NOT EDIT.
Expand All @@ -152,6 +195,53 @@ func Pattern(path string)(pattern string, found bool){
}
`

const testText = `
// Code generated by pogen. DO NOT EDIT.
package {{ .Pkg }}
import "testing"
func TestPattern(t *testing.T) {
type args struct {
path string
}
tests := []struct {
name string
args args
wantPattern string
wantFound bool
}{
{{range $pr := .PatternRegexp}}{
name: "{{$pr.Pattern}}",
args: args{
path: "{{$pr.TestPath}}",
},
wantPattern: "{{$pr.Pattern}}",
wantFound: true,
},
{{ end }}{
name: "nopass",
args: args{
path: "nopassnopass",
},
wantPattern: "",
wantFound: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotPattern, gotFound := Pattern(tt.args.path)
if gotPattern != tt.wantPattern {
t.Errorf("Pattern() gotPattern = %v, want %v", gotPattern, tt.wantPattern)
}
if gotFound != tt.wantFound {
t.Errorf("Pattern() gotFound = %v, want %v", gotFound, tt.wantFound)
}
})
}
}
`

func deleteIfExist(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
Expand Down

0 comments on commit 900b70e

Please sign in to comment.