Coder Social home page Coder Social logo

hubertbiyo / mysql-to-proto Goto Github PK

View Code? Open in Web Editor NEW

This project forked from guyan0319/mysql-to-proto

0.0 1.0 0.0 34 KB

Generating proto files required by grpc from MySQL database,这是一个从mysql数据库生成grpc所需的proto文件的工具

License: MIT License

Go 94.95% Smarty 5.05%

mysql-to-proto's Introduction

mysql-to-proto

使用过grpc的同学都知道,写proto文件比较繁琐,尤其是写message,对应很多字段,为此写了一个简单的从mysql直接读取表结构,生成proto文件的工具。

工具的使用很简单,需要简单的配置,即可运行生成proto文件。

准备

使用前需先安装依赖包go-sql-driver/mysql

$ go get -u github.com/go-sql-driver/mysql

使用说明:

func main() {
	//模板文件存放路径
	tpl := "d:/gopath/src/mysql-to-proto/template/proto.go.tpl"
	//生成proto文件路径
	file := "d:/gopath/src/mysql-to-proto/sso.proto"
	//数据库名,这里填你自己的数据库名
	dbName := "user"
	//配置连接数据库信息
	db, err := Connect("mysql", "root:123456@tcp(127.0.0.1:3306)/"+dbName+"?charset=utf8mb4&parseTime=true")
	//Table names to be excluded
	//需要排除表,这里的表不会生成对应的proto文件
	exclude := map[string]int{"user_log": 1}
	if err != nil {
		fmt.Println(err)
	}
	if IsFile(file) {
		fmt.Fprintf(os.Stderr, "Fatal error: ", "proto file already exist")
		return
	}
	t := Table{}
	//配置message,Cat 
	t.Message = map[string]Detail{
		"Filter": {
			Name: "Filter",
			Cat:  "custom",
			Attr: []AttrDetail{{
				TypeName: "uint64", //类型
				AttrName: "id",//字段
			}},
		},
		"Request": {
			Name: "Request",
			Cat:  "all",
		},
		"Response": {
			Name: "Response",
			Cat:  "custom",
			Attr: []AttrDetail{
				{
					TypeName: "uint64",
					AttrName: "id",
				},
				{
					TypeName: "bool",
					AttrName: "success",
				},
			},
		},
	}
	//pachage名称
	t.PackageModels = "sso"
	//service名称
	t.ServiceName = "Sso"
	//配置services里面的rpc
	t.Method = map[string]MethodDetail{
		"Get":    {Request: t.Message["Filter"], Response: t.Message["Request"]},
		"Create": {Request: t.Message["Request"], Response: t.Message["Response"]},
		"Update": {Request: t.Message["Request"], Response: t.Message["Response"]},
	}
	//处理数据库表字段属性
	t.TableColumn(db, dbName, exclude)
	//生成proto
	t.Generate(file, tpl)
}

LICENSE

BSD License http://creativecommons.org/licenses/BSD/

Book License: CC BY-SA 3.0 License

mysql-to-proto's People

Watchers

James Cloos 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.