GithubHelp home page GithubHelp logo

protoc-gen-gorm's Introduction

protoc-gen-gorm

protoc-gen-gorm is a protoc plugin for injecting [gorm](https//gorm.io) tag to protobuf message。

中文介绍

How it works

Inspired by protoc-go-inject-tag, protoc-gen-gorm runs after the plugin [protoc-gen-go](https://golang/protobuf/protoc-gen-go) and injects gorm tag to protobuf message.

Install

go install github.com/defool/protoc-gen-gorm

Example

./example/foo/v1/db.proto:

syntax = "proto3";
package foo.v1;
option go_package="foo/v1";

// reference of proto file in `./buf`
import "gorm/v1/gorm.proto";

message User {
    uint64 id = 1;
    string name = 2 [(gorm)="size:32;column:uname;"];
    string user_email = 3 [(gorm)="size:32;"];
    uint64 company_id = 4;
    Company company = 5;
    repeated Group groups = 6 [(gorm)="many2many:user_languages;"]; 
}

message Company {
    uint64 id = 1;
    string name = 2;
}

message Group {
    uint64 id = 1;
    string name = 2;
}

generate stub file in two step by protoc:

protoc  -I . -I ./buf  --go_out="./example/generated"  ./example/foo/v1/db.proto
protoc  -I . -I ./buf  --gorm_out="outdir=./example/generated:."  ./example/foo/v1/db.proto
# Note: The out argment is replace by outdir option

OR buf use case:

buf.gen.yaml:

version: v1
plugins:
  - name: go
    out: generated
    opt: paths=source_relative

buf.gen.gorm.yaml:

version: v1
plugins:  
  - name: gorm
    out: .
    opt:
    - paths=source_relative
    - outdir=./generated
    - replace_keyword=true

generate stub file in two step:

buf generate
buf generate --template buf.gen.gorm.yaml

Other features

  • When use the option replace_keyword=true,the column name will be replace by table+column if it's keyword in MySQL.
  • Generate the database column name as variable value to avoid using column name in code directly.

protoc-gen-gorm's People

Contributors

defool 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.