Home

Awesome

webrpc-gen Golang templates

This repo contains the templates used by the webrpc-gen cli to code-generate webrpc Go server and client code.

Usage

webrpc-gen -schema=example.ridl -target=golang -out=./example.gen.go -pkg=main -server -client

# or 
webrpc-gen -schema=example.ridl -target=github.com/webrpc/gen-golang@v0.6.0 -out=./example.gen.go -pkg=main -server -client

# or
webrpc-gen -schema=example.ridl -target=./local-go-templates-on-disk -out=./example.gen.go -pkg=main -server -client

As you can see, the -target supports default golang, any git URI, or a local folder :)

Set custom template variables

Change any of the following values by passing -option="Value" CLI flag to webrpc-gen.

webrpc-gen -optionDefaultDescriptionAdded in
-pkg=<name>"proto"package namev0.5.0
-clientfalsegenerate client codev0.5.0
-serverfalsegenerate server codev0.5.0
-types=falsetruedon't generate typesv0.13.0
-json=jsoniter"stdlib"use alternative json encoding packagev0.12.0
-fixEmptyArraysfalseforce empty array [] instead of null in JSON (see Go #27589)v0.13.0
-errorStackTracefalseenables error stack tracesv0.14.0
-webrpcHeadertrueenable client send webrpc version in http headersv0.16.0

Example:

webrpc-gen -schema=./proto.json -target=golang -out server.gen.go -pkg=main -server

Set custom Go field meta tags in your RIDL file

CLI option flagDescription
+ go.field.name = IDSet custom field name
+ go.field.type = uuid.UUIDSet custom field type (must be able to JSON unmarshal the value)
+ go.type.import = github.com/google/uuidSet custom field type's import path
+ go.tag.json = idSet json:"id" struct tag
+ go.tag.db = idSet db:"id" struct tag

Example:

struct User
  - ID: int64
    + go.tag.db = id
    + go.tag.json = id
  - UUID: string
    + go.field.type = uuid.UUID
    + go.type.import = github.com/google/uuid
    + go.tag.json = uuid
    + go.tag.db = uuid
  - Age: int
    + go.tag.db: age
  - Name: string
    + go.tag.db = name
  - PasswordHash: string
    + go.tag.db = passwd_hash

will result in

import "github.com/google/uuid"

type User struct {
	ID           int64     `json:"id" db:"id"`
	UUID         uuid.UUID `json:"uuid" db:"uuid"`
	Name         string    `db:"name"`
	Age          int       `db:"age"`
	PasswordHash string    `json:"-" db:"passwd_hash"`
}

Examples

See _examples

LICENSE

MIT LICENSE