Skip to content

Commit

Permalink
templates, first iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 1, 2024
1 parent e3d2502 commit fd18a9e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/messaging/clients/client_template.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Code generated by '{{GENERATOR}}'. DO NOT EDIT.
// template: {{TEMPLATE}}

package clients

import (
"context"
"fmt"

"{{GRPC_INC}}"
{{TYPE_PACKAGE}} "{{PROTO_INC}}"
"github.com/chain4travel/camino-messenger-bot/internal/messaging/types"

"google.golang.org/grpc"
"google.golang.org/protobuf/reflect/protoreflect"
)

const (
{{SERVICE}}V{{VERSION}} = {{FQPN}}
{{SERVICE}}V{{VERSION}}Request types.MessageType = types.MessageType({{SERVICE}}V{{VERSION}} + ".Request")
{{SERVICE}}V{{VERSION}}Response types.MessageType = types.MessageType({{SERVICE}}V{{VERSION}} + ".Response")
)

var _ Client = (*{{SERVICE}}Client)(nil)

func New{{SERVICE}}V{{VERSION}}(grpcCon *grpc.ClientConn) *{{SERVICE}}Client {
client := {{GRPC_PACKAGE}}.New{{SERVICE}}Client(grpcCon)
return &{{SERVICE}}Client{client: &client}
}

type {{SERVICE}}Client struct {
client *{{GRPC_PACKAGE}}.{{SERVICE}}Client
}

func (s {{SERVICE}}Client) Call(ctx context.Context, requestIntf protoreflect.ProtoMessage, opts ...grpc.CallOption) (protoreflect.ProtoMessage, types.MessageType, error) {
request, ok := requestIntf.(*{{TYPE_PACKAGE}}.{{REQUEST}}))
if !ok {
return nil, {{SERVICE}}V{{VERSION}}Response, fmt.Errorf("invalid request type")
}
response, err := (*s.client).{{METHOD}})(ctx, request, opts...)
return response, {{SERVICE}}V{{VERSION}}Response, err
}
42 changes: 42 additions & 0 deletions internal/rpc/server/server_template.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Code generated by '{{GENERATOR}}'. DO NOT EDIT.
// template: {{TEMPLATE}}

package server

import (
"context"
"fmt"

"{{GRPC_INC}}"
{{TYPE_PACKAGE}} "{{PROTO_INC}}"

"github.com/chain4travel/camino-messenger-bot/internal/messaging/clients"
"google.golang.org/grpc"
)

var _ {{GRPC_PACKAGE}}.{{SERVICE}}Server = (*{{TYPE_PACKAGE}}_srv1)(nil)

type {{TYPE_PACKAGE}}_srv struct {
reqProcessor externalRequestProcessor
}

func New{{SERVICE}}Server(
grpcServer *grpc.Server,
reqProcess externalRequestProcessor,
) {{GRPC_PACKAGE}}.{{SERVICE}}Server {
{{TYPE_PACKAGE}}_srv := &{{TYPE_PACKAGE}}_srv1{reqProcessor: reqProcess}
{{GRPC_PACKAGE}}.Register{{SERVICE}}Server(grpcServer, {{TYPE_PACKAGE}}_srv)
return {{TYPE_PACKAGE}}_srv
}

func (s *{{TYPE_PACKAGE}}_srv1) {{METHOD}}(ctx context.Context, request *{{TYPE_PACKAGE}}.{{REQUEST}}) (*{{TYPE_PACKAGE}}.{{RESPONSE}}, error) {
response, err := s.reqProcessor.processExternalRequest(ctx, clients.{{SERVICE}}V{{VERSION}}Request, request)
if err != nil {
return nil, fmt.Errorf("failed to process %s request: %w", clients.{{SERVICE}}V{{VERSION}}Request, err)
}
resp, ok := response.(*{{TYPE_PACKAGE}}.{{RESPONSE}})
if !ok {
return nil, fmt.Errorf("invalid response type: expected %s, got %T", clients.{{SERVICE}}V{{VERSION}}Response, response)
}
return resp, nil
}

0 comments on commit fd18a9e

Please sign in to comment.