plugin/shared/interface.go

79 lines
2.3 KiB
Go

package shared
import (
"context"
"gitea.cdlsxd.cn/sdk/plugin/proto"
"github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
)
// PluginService .
type PluginService interface {
Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error)
Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error)
Notify(ctx context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error)
}
// GRPCPlugin implement plugin.GRPCPlugin
type GRPCPlugin struct {
plugin.Plugin
Impl PluginService
}
func (p GRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, server *grpc.Server) error {
proto.RegisterPluginServer(server, GPRCPluginServerWrapper{impl: p.Impl})
return nil
}
func (p GRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, conn *grpc.ClientConn) (interface{}, error) {
return GRPCPluginClientWrapper{
client: proto.NewPluginClient(conn),
}, nil
}
type GPRCPluginServerWrapper struct {
impl PluginService
proto.UnimplementedPluginServer
}
func (_this GPRCPluginServerWrapper) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) {
return _this.impl.Order(ctx, request)
}
func (_this GPRCPluginServerWrapper) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
return _this.impl.Query(ctx, request)
}
func (_this GPRCPluginServerWrapper) Notify(ctx context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
return _this.impl.Notify(ctx, request)
}
// GRPCPluginClientWrapper 作为 server 调用插件接口的包装器,
type GRPCPluginClientWrapper struct {
client proto.PluginClient
}
func (_this GRPCPluginClientWrapper) Order(ctx context.Context, request *proto.OrderRequest) (*proto.OrderResponse, error) {
resp, err := _this.client.Order(ctx, request)
if err != nil {
return nil, err
}
return resp, nil
}
func (_this GRPCPluginClientWrapper) Query(ctx context.Context, request *proto.QueryRequest) (*proto.QueryResponse, error) {
resp, err := _this.client.Query(ctx, request)
if err != nil {
return nil, err
}
return resp, nil
}
func (_this GRPCPluginClientWrapper) Notify(ctx context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) {
resp, err := _this.client.Notify(ctx, request)
if err != nil {
return nil, err
}
return resp, nil
}