Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented template for wrapping GRPC services with request validation
- Loading branch information
Showing
5 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import ( | ||
grpc_codes "google.golang.org/grpc/codes" | ||
grpc_status "google.golang.org/grpc/status" | ||
) | ||
|
||
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithGRPCValidation" .Interface.Name)) }} | ||
|
||
// {{$decorator}} implements {{.Interface.Type}} interface instrumented with GRPC request validation | ||
type {{$decorator}} struct { | ||
{{.Interface.Type}} | ||
} | ||
|
||
// New{{$decorator}} returns {{$decorator}} | ||
func New{{$decorator}} (base {{.Interface.Type}}) {{$decorator}} { | ||
return {{$decorator}} { | ||
{{.Interface.Name}}: base, | ||
} | ||
} | ||
|
||
{{range $method := .Interface.Methods}} | ||
{{- if $method.ReturnsError}} | ||
// {{$method.Name}} implements {{$.Interface.Type}} | ||
func (_d {{$decorator}}) {{$method.Declaration}} { | ||
{{- range $param := $method.Params}} | ||
{{- if not ( and $method.AcceptsContext (eq $param.Name "ctx")) -}} | ||
if _v, _ok := interface{}({{$param.Name}}).(interface{ Validate() error}); _ok { | ||
if err = _v.Validate(); err != nil { | ||
err = grpc_status.Error(grpc_codes.InvalidArgument, err.Error()) | ||
return | ||
} | ||
} | ||
{{end}} | ||
{{end}} | ||
{{$method.Pass (printf "_d.%s." $.Interface.Name) }} | ||
} | ||
{{end}} | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package templatestests | ||
|
||
// DO NOT EDIT! | ||
// This code is generated with http://github.com/hexdigest/gowrap tool | ||
// using ../templates/grpc_validate template | ||
|
||
//go:generate gowrap gen -p github.com/hexdigest/gowrap/templates_tests -i InterfaceWithValidtableArg -t ../templates/grpc_validate -o interface_with_grpc_validation.go | ||
|
||
import ( | ||
"context" | ||
|
||
grpc_codes "google.golang.org/grpc/codes" | ||
grpc_status "google.golang.org/grpc/status" | ||
) | ||
|
||
// InterfaceWithValidtableArgWithGRPCValidation implements InterfaceWithValidtableArg interface instrumented with GRPC request validation | ||
type InterfaceWithValidtableArgWithGRPCValidation struct { | ||
InterfaceWithValidtableArg | ||
} | ||
|
||
// NewInterfaceWithValidtableArgWithGRPCValidation returns InterfaceWithValidtableArgWithGRPCValidation | ||
func NewInterfaceWithValidtableArgWithGRPCValidation(base InterfaceWithValidtableArg) InterfaceWithValidtableArgWithGRPCValidation { | ||
return InterfaceWithValidtableArgWithGRPCValidation{ | ||
InterfaceWithValidtableArg: base, | ||
} | ||
} | ||
|
||
// Method implements InterfaceWithValidtableArg | ||
func (_d InterfaceWithValidtableArgWithGRPCValidation) Method(ctx context.Context, r *ValidatableRequest) (err error) { | ||
if _v, _ok := interface{}(r).(interface{ Validate() error }); _ok { | ||
if err = _v.Validate(); err != nil { | ||
err = grpc_status.Error(grpc_codes.InvalidArgument, err.Error()) | ||
return | ||
} | ||
} | ||
|
||
return _d.InterfaceWithValidtableArg.Method(ctx, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package templatestests | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestInterfaceWithValidatableArgWithGRPC_Method(t *testing.T) { | ||
t.Run("success", func(t *testing.T) { | ||
r := ValidatableRequest{} | ||
wrapped := NewInterfaceWithValidtableArgWithGRPCValidation(&r) | ||
err := wrapped.Method(context.Background(), &r) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("error", func(t *testing.T) { | ||
r := ValidatableRequest{ | ||
err: errors.New("unexpected error"), | ||
} | ||
wrapped := NewInterfaceWithValidtableArgWithGRPCValidation(&r) | ||
err := wrapped.Method(context.Background(), &r) | ||
require.Error(t, err) | ||
s, ok := status.FromError(err) | ||
require.True(t, ok) | ||
assert.Equal(t, codes.InvalidArgument, s.Code()) | ||
}) | ||
} |