/protoc-gen-go-event

protoc-gen-go-event

Primary LanguageGo

基于protobuff生成事件api接口与客户端

定义事件

syntax = "proto3";

package test;

import "event/options/event.proto";


option go_package = "codeup.aliyun.com/7799520/b/protoc-gen-go-event/test/pb";

service TestService {
  rpc TestMethod1 (TestRequest) returns (TestResponse) {
  }

  rpc TestMethod (TestRequest) returns (TestResponse) {
    option (event.event) = {
      name: "/test/test111",
      delay: 100,
    };
  }
}

message TestRequest {

}

message TestResponse {

}

安装

go install github.com:guihouchang/protoc-gen-go-event@latest

生成接口

   protoc  --proto_path=./ \
		   --proto_path=./pb \
	       --proto_path=./test/pb \
	       --go_out=paths=source_relative:./ \
	       --go-event_out=paths=source_relative:./ \
	       test/pb/test.proto

生成接口文件

// Code generated by protoc-gen-go-event. DO NOT EDIT.
// versions:
// protoc-gen-go-event v2.5.3

package pb

import (
	context "context"
	"fmt"

	watermill "github.com/ThreeDotsLabs/watermill"
	amqp1 "github.com/ThreeDotsLabs/watermill-amqp/pkg/amqp"
	message "github.com/ThreeDotsLabs/watermill/message"
	amqp "github.com/streadway/amqp"
	protojson "google.golang.org/protobuf/encoding/protojson"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(amqp.Table)
var _ = new(amqp1.Config)
var _ = new(message.Message)
var _ = watermill.NewUUID
var _ = protojson.Marshal

type TestServiceEventServer interface {
	TestMethod(context.Context, *TestRequest) error
}

func RegisterTestServiceEventServer(r *message.Router, sg func(topic string) message.Subscriber, srv TestServiceEventServer) {
	r.AddNoPublisherHandler(
		"/test/test111",
		"/test/test111",
		sg("/test/test111"),
		_TestService_TestMethod0_Event_Handler(srv),
	)
}

func _TestService_TestMethod0_Event_Handler(srv TestServiceEventServer) func(msg *message.Message) error {
	return func(msg *message.Message) error {
		var req TestRequest
		err := protojson.Unmarshal(msg.Payload, &req)
		if err != nil {
			return err
		}
		return srv.TestMethod(msg.Context(), &req)
	}
}

type TestServiceEventClient interface {
	TestMethod(ctx context.Context, req *TestRequest) error

	TestMethodWithDelay(ctx context.Context, req *TestRequest, delay int32) error
}

type TestServiceEventClientImpl struct {
	publisher message.Publisher
}

func NewTestServiceEventClient(publisher message.Publisher) TestServiceEventClient {
	return &TestServiceEventClientImpl{publisher}
}

func (c *TestServiceEventClientImpl) TestMethod(ctx context.Context, req *TestRequest) error {
	topic := "/test/test111"
	byteData, err := protojson.Marshal(req)
	if err != nil {
		return err
	}

	msg := message.NewMessage(watermill.NewUUID(), byteData)
	msg.SetContext(ctx)

	// 设置延迟队列时间,单位为100ms
	msg.Metadata.Set("x-delay", "100")

	return c.publisher.Publish(topic, msg)
}

func (c *TestServiceEventClientImpl) TestMethodWithDelay(ctx context.Context, req *TestRequest, delay int32) error {
	topic := "/test/test111"
	byteData, err := protojson.Marshal(req)
	if err != nil {
		return err
	}

	msg := message.NewMessage(watermill.NewUUID(), byteData)
	msg.SetContext(ctx)
	msg.Metadata.Set("x-delay", fmt.Sprintf("%d", delay))
	return c.publisher.Publish(topic, msg)
}