Giter Site home page Giter Site logo

protoc-gen-go-event's Introduction

基于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)
}

protoc-gen-go-event's People

Contributors

guihouchang avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.