webrpc Golang client/server generator

  • By null
  • Last update: Dec 30, 2022
  • Comments: 6

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/[email protected] -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.

CLI option flag Description Default value
-Pkg=pkgname package name "proto"
-Client generate client code unset (false)
-Server generate server code unset (false)

Example:

webrpc-gen -schema=./proto.json -target=golang -out openapi.gen.yaml -Pkg=main -Client -Server

Set custom Go field meta tags in your RIDL file

CLI option flag Description
+ go.field.name = ID Set custom field name
+ go.tag.json = id Set json:"id" struct tag
+ go.tag.db = id Set db:"id" struct tag

Example:

message User
  - id: uint64
    + go.field.name = ID
    + go.tag.db = id
    + go.tag.json = id

will result in

type User struct {
	ID uint64 `json:"id" db:"id"`
}

Examples

See _examples

LICENSE

LICENSE

Download

gen-golang.zip

Comments(6)

  • 1

    Interoperability tests

    Interoperability tests

    This PR implements webrpc interoperability tests.

    Self-test

    The basic interoperability unit test is ensuring that the client and server code generated from this repository can talk to each other.

    1. Generate code
    2. Test generated client/server code against each other
    go generate ./...
    go test ./...
    

    Test matrix against webrpc-test reference binary

    These tests are ensuring that

    • client code generated from this repository can talk to reference webrpc-test@version server
    • server code generated from this repository responds to reference webrpc-test@version client tests
    1. Generate code
    2. Test generated client and server code against multiple versions of webrpc-test reference binaries via test.sh script
    go generate ./...
    
    for webrpcVersion in v0.10.0 v0.11.0; do
        ./test.sh $webrpcVersion
    done
    
  • 2

    Fix duplicated json struct tag

    Fixes bug with go.tag.json, which rendered duplicated json tags:

    -   CreatedAt *time.Time `json:"createdAt" db:"created_at,omitempty" json:"createdAt,omitempty"`
    +   UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at,omitempty"`
    
  • 3

    Support custom struct tags (+ go.tag.TAGNAME)

    This RIDL

    message User
      - id: uint64
        + go.tag.db = id,omitempty
        + go.tag.cbor = id,omitempty
    

    should generate

    type User struct {
      ID  uint64  `json:"id" db:"id,omitempty" cbor:"id,omitempty"`
    }
    
  • 4

    Fix more optional field regressions

    Fixed more edge cases against a proprietary RIDL file to make sure the golang target is fully backward compatible with v0.6.0. These edge cases were not really covered by ./_examples or tests.

  • 5

    Support go.field.type meta

    Seems like I might have missed support for go.field.type during the webrpc 0.7.0 development

    type tokenID: string
      + go.tag.db = id
      + go.field.type = prototyp.BigInt
    

    See the original code at https://github.com/webrpc/gen-golang/commit/afb0cde87948eaf5656f212ba7f9579cb0fb9d2c

    • [ ] Add go.field.type to examples
    • [ ] Support custom type, ie. prototyp.BigInt
  • 6

    Custom type in method argument fails to render

    message User
      - id: uint64
        + json = id
        + go.field.name = ID
        + go.field.type = int64 ### This doesn't work on server
        + go.tag.db = id
    
    - GetUser(header: map<string,string>, userID: uint64) => (code: uint32, user: User)
    

    generates wrong server method signature