Giter Club home page Giter Club logo

Comments (11)

taoso avatar taoso commented on July 27, 2024

cmd/protoc-gen-twirp 已经跟 cmd/sniper 脚手架合并了。这样做可以避免安装多个工具,降低使用门槛。

from sniper.

busyfree avatar busyfree commented on July 27, 2024

OK,我看Makefile 文件里面 没有涉及到 build protoc-gen-twirp 这个插件,直接使用protoc --plugin=protoc-gen-twirp 这个插件。

from sniper.

busyfree avatar busyfree commented on July 27, 2024

rpc/common/common.proto

syntax = "proto3";

package common;

option go_package = "./common";


message Empty {

}

rpc/mp/v1/public/service.proto

syntax = "proto3";

package mp.v1.public;

option go_package = "./mp/v1/public";

import "common/common.proto";

service Server {
   rpc Test (common.Empty) returns (TestResp);
}

rpc 下面定义了一个service pb和一个 common pb,sniper下 pb 生成支持这样的结构吗?

from sniper.

taoso avatar taoso commented on July 27, 2024

OK,我看Makefile 文件里面 没有涉及到 build protoc-gen-twirp 这个插件,直接使用protoc --plugin=protoc-gen-twirp 这个插件。

这里面有一个小细节

protoc --plugin=protoc-gen-twirp=$(shell which sniper) \

protoc 约定所有的插件名必须是 protoc-gen-XXX,这里的 XXX 对应 --out_XXX。早期 sniper 需要安装 protoc-gen-twirp 和 sniper 两个工具。后面我把 protoc-gen-twirp 的功能整合到 sniper 命令里。但 sniper 的命令不符合 protoc-gen-XXX 约定,所以在调用 protoc 的时候需要通过--plugin=protoc-gen-twirp=$(shell which sniper)来指定使用 sniper 生成 twirp 代码。

from sniper.

busyfree avatar busyfree commented on July 27, 2024

嗯,看到了,感谢解答。

from sniper.

taoso avatar taoso commented on July 27, 2024

rpc 下面定义了一个service pb和一个 common pb,sniper下 pb 生成支持这样的结构吗?

可以的。不过导入 proto 的路径需要从 rpc 目录开始。而且被导入的 proto 文件需要使用 go_package 声明自己的完整包名。如果项目的包名是 helloworld,那么 common.proto 的 go_package 应该是helloworld/rpc/common。而 service.proto 导包的时候应该写成import rpc/common/common.proto

from sniper.

busyfree avatar busyfree commented on July 27, 2024

好的,感谢。

from sniper.

busyfree avatar busyfree commented on July 27, 2024

测试了一下rpc输出,go_out的包名有错误,twrip的 package 输出也是和下面的 go out 一样。
代码里临时修改,只能解决twrip out 的包名问题。

func (t *twirp) generateFileHeader(file *protogen.File) {
	t.P("// Package ", string(file.GoPackageName), " is generated by sniper ", Version, ", DO NOT EDIT.")
	t.P("// source: ", file.Desc.Path())
	pkgName := string(file.GoPackageName)
	if strings.Contains(pkgName, "/") {
		pkgName = strings.ReplaceAll(pkgName, "/", "_")
	}
	t.P(`package `, pkgName)
	t.P()
}
--go_out: rpc/mp/v1/public/service.pb.go: unparsable Go source: 7:14: expected ';', found '/'
    1	// Code generated by protoc-gen-go. DO NOT EDIT.
    2	// versions:
    3	// 	protoc-gen-go v1.27.1
    4	// 	protoc        v3.19.4
    5	// source: rpc/mp/v1/public/service.proto
    6	
    7	package mp_v1/public
    8	
// common.proto
syntax = "proto3";

package rpc.common;

option go_package = "sniper/rpc/common";

message Empty {

}

// service.proto
syntax = "proto3";

package rpc.mp.v1.public;

option go_package = "sniper/rpc/mp/v1/public";

import "rpc/common/common.proto";

service Server {
  rpc Test (common.Empty) returns (TestResp);
}


message TestResp {
  int32 code = 1;
  string msg = 2;
}

from sniper.

busyfree avatar busyfree commented on July 27, 2024

Makefile 里面的 $m 变量输出,不是期望的 rpc/util/v0/kv.proto => rpc/util/v0;util_v0


rpc/common/common.proto=rpc/common;common

protoc --plugin=protoc-gen-twirp=/Users/tdbackend/Devland/codes/go/libs/bin/sniper
--twirp_out=Mrpc/common/common.proto=rpc/common;common:.
--go_out=Mrpc/common/common.proto=rpc/common;common:.
rpc/common/common.proto

rpc/mp/v1/public/service.proto=rpc/mp/v1/public;mp_v1/public

protoc --plugin=protoc-gen-twirp=/Users/tdbackend/Devland/codes/go/libs/bin/sniper
--twirp_out=Mrpc/mp/v1/public/service.proto=rpc/mp/v1/public;mp_v1/public:.
--go_out=Mrpc/mp/v1/public/service.proto=rpc/mp/v1/public;mp_v1/public:.
rpc/mp/v1/public/service.proto

from sniper.

busyfree avatar busyfree commented on July 27, 2024

$m 这个脚本看样是只能支持3级目录,3级以上处理就出问题了。

from sniper.

busyfree avatar busyfree commented on July 27, 2024

还有个问题,能把这个 protoc-gen-markdown 插件一起引入到protoc 脚本里吗?

from sniper.

Related Issues (20)

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.