Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-go
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Package Registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-go
Commits
aba784d3
Commit
aba784d3
authored
5 months ago
by
Anton Sattarov
Browse files
Options
Downloads
Patches
Plain Diff
add client template and proto decoder interface
parent
7456cb0f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
assets/templates/transport/grpc_client.tmpl
+87
-0
87 additions, 0 deletions
assets/templates/transport/grpc_client.tmpl
pkg/invitations/client.go
+82
-0
82 additions, 0 deletions
pkg/invitations/client.go
with
169 additions
and
0 deletions
assets/templates/transport/grpc_client.tmpl
0 → 100644
+
87
−
0
View file @
aba784d3
import (
"context"
"git.perx.ru/perxis/perxis-go/pkg/options"
pb "git.perx.ru/perxis/perxis-go/proto/{{lower .Interface.Name}}"
"google.golang.org/grpc"
)
type Client struct {
client pb.{{.Interface.Name}}Client
decoder ProtoDecoder
}
var _ {{.Interface.Name}} = &Client{}
func NewClient(conn *grpc.ClientConn, decoder ProtoDecoder) *Client {
return &Client{
client: pb.New{{.Interface.Name}}Client(conn),
decoder: decoder,
}
}
type ProtoDecoder interface {
{{range $method := .Interface.Methods}}
{{- if $method.ReturnsError}}
{{ $resList := list }}
{{ $resListLen := add 0 }}
{{- range $param := $method.Results -}}
{{- if not ( and $method.ReturnsError (eq $param.Name "err")) -}}
{{- $resList = append $resList (printf "%s %s" $param.Name $param.Type) -}}
{{- $resListLen = add $resListLen 1 -}}
{{- end -}}
{{- end -}}
{{- $paramList := list -}}
{{- range $param := $method.Params -}}
{{- if not ( and $method.AcceptsContext (eq $param.Name "ctx")) -}}
{{- $paramList = append $paramList (printf "%s %s" $param.Name $param.Type)
-}}
{{- end -}}
{{- end -}}
{{ $method.Name }}Request({{ $paramList | join `, ` }}) *pb.{{ $method.Name }}Request
{{- if gt $resListLen 0 }}
{{ $method.Name }}Response(res *pb.{{ $method.Name }}Response) ({{ $resList | join `, ` }})
{{- end }}
{{- end -}}
{{end}}
}
{{range $method := .Interface.Methods}}
{{- if $method.ReturnsError}}
{{ $resList := list }}
{{ $resListLen := add 0 }}
{{- range $param := $method.Results -}}
{{- if not ( and $method.ReturnsError (eq $param.Name "err")) -}}
{{- $resList = append $resList $param.Name -}}
{{- $resListLen = add $resListLen 1 -}}
{{- end -}}
{{- end -}}
{{- $paramList := list -}}
{{- range $param := $method.Params -}}
{{- if not ( and $method.AcceptsContext (eq $param.Name "ctx")) -}}
{{- $paramList = append $paramList $param.Name -}}
{{- end -}}
{{- end -}}
// {{$method.Name}} implements {{$.Interface.Name}}
func (c *Client) {{$method.Declaration}} {
request := c.decoder.{{$method.Name}}Request({{ $paramList | join `, ` }})
{{- if gt $resListLen 0 }}
res, err := c.client.{{ $method.Name }}(ctx, request)
{{- else }}
_, err = c.client.{{ $method.Name }}(ctx, request)
{{- end }}
{{- if gt $resListLen 0 }}
{{ $resList | join `,` }} = c.decoder.{{ $method.Name }}Response(res)
{{- end }}
return
}
{{ end }}
{{ end }}
This diff is collapsed.
Click to expand it.
pkg/invitations/client.go
0 → 100644
+
82
−
0
View file @
aba784d3
// Code generated by gowrap. DO NOT EDIT.
// template: ../../assets/templates/transport/grpc_client.tmpl
// gowrap: http://github.com/hexdigest/gowrap
package
invitations
//go:generate gowrap gen -p git.perx.ru/perxis/perxis-go/pkg/invitations -i Invitations -t ../../assets/templates/transport/grpc_client.tmpl -o client.go -l ""
import
(
"context"
"git.perx.ru/perxis/perxis-go/pkg/options"
pb
"git.perx.ru/perxis/perxis-go/proto/invitations"
"google.golang.org/grpc"
)
type
Client
struct
{
client
pb
.
InvitationsClient
decoder
ProtoDecoder
}
var
_
Invitations
=
&
Client
{}
func
NewClient
(
conn
*
grpc
.
ClientConn
,
decoder
ProtoDecoder
)
*
Client
{
return
&
Client
{
client
:
pb
.
NewInvitationsClient
(
conn
),
decoder
:
decoder
,
}
}
type
ProtoDecoder
interface
{
AcceptRequest
(
invitationId
string
,
userId
string
)
*
pb
.
AcceptRequest
CreateRequest
(
invitation
*
Invitation
)
*
pb
.
CreateRequest
CreateResponse
(
res
*
pb
.
CreateResponse
)
(
created
*
Invitation
)
DeleteRequest
(
invitationId
string
)
*
pb
.
DeleteRequest
FindRequest
(
filter
*
Filter
,
opts
*
options
.
FindOptions
)
*
pb
.
FindRequest
FindResponse
(
res
*
pb
.
FindResponse
)
(
invitations
[]
*
Invitation
,
total
int
)
GetRequest
(
invitationId
string
)
*
pb
.
GetRequest
GetResponse
(
res
*
pb
.
GetResponse
)
(
invitation
*
Invitation
)
}
// Accept implements Invitations
func
(
c
*
Client
)
Accept
(
ctx
context
.
Context
,
invitationId
string
,
userId
string
)
(
err
error
)
{
request
:=
c
.
decoder
.
AcceptRequest
(
invitationId
,
userId
)
_
,
err
=
c
.
client
.
Accept
(
ctx
,
request
)
return
}
// Create implements Invitations
func
(
c
*
Client
)
Create
(
ctx
context
.
Context
,
invitation
*
Invitation
)
(
created
*
Invitation
,
err
error
)
{
request
:=
c
.
decoder
.
CreateRequest
(
invitation
)
res
,
err
:=
c
.
client
.
Create
(
ctx
,
request
)
created
=
c
.
decoder
.
CreateResponse
(
res
)
return
}
// Delete implements Invitations
func
(
c
*
Client
)
Delete
(
ctx
context
.
Context
,
invitationId
string
)
(
err
error
)
{
request
:=
c
.
decoder
.
DeleteRequest
(
invitationId
)
_
,
err
=
c
.
client
.
Delete
(
ctx
,
request
)
return
}
// Find implements Invitations
func
(
c
*
Client
)
Find
(
ctx
context
.
Context
,
filter
*
Filter
,
opts
*
options
.
FindOptions
)
(
invitations
[]
*
Invitation
,
total
int
,
err
error
)
{
request
:=
c
.
decoder
.
FindRequest
(
filter
,
opts
)
res
,
err
:=
c
.
client
.
Find
(
ctx
,
request
)
invitations
,
total
=
c
.
decoder
.
FindResponse
(
res
)
return
}
// Get implements Invitations
func
(
c
*
Client
)
Get
(
ctx
context
.
Context
,
invitationId
string
)
(
invitation
*
Invitation
,
err
error
)
{
request
:=
c
.
decoder
.
GetRequest
(
invitationId
)
res
,
err
:=
c
.
client
.
Get
(
ctx
,
request
)
invitation
=
c
.
decoder
.
GetResponse
(
res
)
return
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment