Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor server and planner expectation #56

Merged
merged 4 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MODULE_NAME=grpcmock
MODULE_NAME = grpcmock

VENDOR_DIR = vendor

Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ignore:
- "features/**/*"
- "internal/**/*"
- "request/*"
- "options.go"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
google.golang.org/genproto v0.0.0-20230119192704-9d59e20e5cd1 // indirect
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ google.golang.org/genproto v0.0.0-20221114212237-e4508ebdbee1/go.mod h1:rZS5c/ZV
google.golang.org/genproto v0.0.0-20221117204609-8f9c96812029/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20221118155620-16455021b5e6/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20221201164419-0e50fba7f41c/go.mod h1:rZS5c/ZVYMaOGBfO68GWtjOw/eLaZM1X6iVtgjZ+EWg=
google.golang.org/genproto v0.0.0-20230119192704-9d59e20e5cd1 h1:wSjSSQW7LuPdv3m1IrSN33nVxH/kID6OIKy+FMwGB2k=
google.golang.org/genproto v0.0.0-20230119192704-9d59e20e5cd1/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57 h1:vArvWooPH749rNHpBGgVl+U9B9dATjiEhJzcWGlovNs=
google.golang.org/genproto v0.0.0-20230202175211-008b39050e57/go.mod h1:RGgjbofJ8xD9Sq1VVhDM1Vok1vRONV+rg+CjzG4SZKM=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
Expand Down
81 changes: 81 additions & 0 deletions matcher/payload.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package matcher

import (
"encoding/json"
"regexp"

"go.nhat.io/matcher/v2"

"go.nhat.io/grpcmock/must"
"go.nhat.io/grpcmock/streamer"
"go.nhat.io/grpcmock/value"
)

Expand Down Expand Up @@ -69,3 +74,79 @@ func Payload(m matcher.Matcher, decode PayloadDecoder) *PayloadMatcher {
decode: decode,
}
}

// UnaryPayload initiates a new payload matcher for a unary request.
func UnaryPayload(in interface{}) *PayloadMatcher {
switch v := in.(type) {
case []byte, string:
return Payload(matcher.JSON(value.String(in)), decodeUnaryPayload)

case matcher.Matcher,
func() matcher.Matcher,
*regexp.Regexp:
return Payload(matcher.Match(v), decodeUnaryPayload)

case MatchFn:
return Payload(Fn("", v), nil)
}

return Payload(matcher.JSON(in), decodeUnaryPayload)
}

// ServerStreamPayload initiates a new payload matcher for a server stream request.
func ServerStreamPayload(in interface{}) *PayloadMatcher {
return UnaryPayload(in)
}

// ClientStreamPayload initiates a new payload matcher for a client stream request.
func ClientStreamPayload(in interface{}) *PayloadMatcher {
switch v := in.(type) {
case []byte, string:
return Payload(matcher.JSON(value.String(v)), decodeClientStreamPayload)

case matcher.Matcher,
func() matcher.Matcher,
*regexp.Regexp:
return Payload(matcher.Match(v), decodeClientStreamPayload)

case MatchFn:
return matchClientStreamPayloadWithCustomMatcher("", v)

case func() (string, MatchFn):
return matchClientStreamPayloadWithCustomMatcher(v())
}

return Payload(matcher.JSON(in), decodeClientStreamPayload)
}

func matchClientStreamPayloadWithCustomMatcher(expected string, match MatchFn) *PayloadMatcher {
return Payload(Fn(expected, func(actual interface{}) (bool, error) {
in, err := streamer.ClientStreamerPayload(actual.(*streamer.ClientStreamer))
// This should never happen because the PayloadMatcher will read the stream first.
// If there is an error while reading the stream, it is caught inside the PayloadMatcher.
must.NotFail(err)

return match(in)
}), nil)
}

func decodeUnaryPayload(in interface{}) (string, error) {
switch v := in.(type) {
case []byte:
return string(v), nil

case string:
return v, nil
}

data, err := json.Marshal(in)
if err != nil {
return "", err
}

return string(data), nil
}

func decodeClientStreamPayload(in interface{}) (string, error) {
return value.Marshal(in)
}
Loading