xprotogen

module
v0.0.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2021 License: MIT

README

xprotogen

A simple and easy-to-use protobuf plugin generation tool for golang

xprotogen是一个简单易用的golang protobuf plugin生成工具包

examples

  1. 最简单的hello
  2. micro protobuf生成
package main

import (
	"log"

	"github.com/pubgo/xerror"
	"github.com/pubgo/xprotogen/gen"
)

func main() {
	defer xerror.RespDebug()

	m := gen.New("micro")
	m.Parameter(func(key, value string) {
		log.Println("params:", key, "=", value)
	})

	xerror.Panic(m.GenWithTpl(tpl))
}

const tpl = `
// Code generated by protoc-gen-micro. DO NOT EDIT.
{%- if !fd.GetOptions().GetDeprecated() %}
// source: {{fd.GetName()}}
{%- else %}
// {{fd.GetName()}} is a deprecated file.
{%- endif %}

package {{pkg}}

import (
	fmt "fmt"
	math "math"
	context "context"

	_ "github.com/gogo/protobuf/gogoproto"
	proto "github.com/golang/protobuf/proto"
	_ "google.golang.org/genproto/googleapis/api/annotations"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package

// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context

{%- macro clientMethod(m) %}
	{% if !m.CS %}
		{% set new_in = ",in *"|add:m.InType %}
	{% endif %}

	{% set new_out = "*"|add:m.OutType %}
	{% if m.SS || m.CS %}
		{% set new_out = m.Srv|add:"_"|add:m.Name|add:"Service" %}
	{% endif %}

	{{- m.Name}}(ctx context.Context {{ new_in }})({{- new_out }},error)
{%- endmacro %}


{%- macro serverMethod(m) %}
	{% if !m.CS %}
		{% set new_in = ", *"|add:m.InType %}
	{% endif %}

	{% if m.CS || m.SS %}
		{% set new_in1 = m.Srv|add:"_"|add:m.Name|add:"Stream" %}
	{% endif %}

	{% if !m.SS && !m.CS %}
		{% set new_out = "*"|add:m.OutType|add:"," %}
	{% endif %}

	{{- m.Name}}Server(context.Context{{ new_in }}{{ new_in1 }})({{- new_out }}error)
{%- endmacro %}

{% for s in fd.GetService() %}

type {{s.GetName()}}Service interface {
	{%- for m in s.GetMethod() %}
		{{- clientMethod(m) -}}
		{{- serverMethod(m) -}}
	{%- endfor %}
}

{% endfor %}
`

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL