Code Monkey home page Code Monkey logo

pbts's Introduction

pbts

pbts is a simple pb.go to .ts converter. It converts the protoc generated Golang definitions for protobuf messages into a Typescript abstract class.

The motivation for pbts is to be able to easily -

  1. define APIs using protobufs,
  2. implement the API server using Golang, and
  3. implement frontend client in Typescript.

The types generated by protoc and pbts respectively will ensure that (<pedantic>some</pedantic>) breaking API changes will cause compile time errors.

Notes

  • a copy function is generated to copy data between instances.
  • following protobuf conventions, int64 types are serialized as strings.
  • the json field name in a proto field tag is preferred to a json field tag
  • proto maps and oneofs are supported

Example Usage

# main.go

writer, _ := os.OpenFile("api.ts", ...)
g := pbts.NewGenerator(writer)
g.RegisterMany(
  MyFirstStruct{},
  MySecondStruct{},
)
g.Write()

Example Output

Import the generated classes into your Typescript project for fun and profit.

type TestStruct struct {
	Field      *string           `protobuf:"bytes,1,opt,name=field" json:"field,omitempty"`
	FieldInt   *int64            `protobuf:"varint,6,opt,name=field_int,json=fieldInt" json:"field_int,omitempty"`
	OtherField string            `json:"other_field"`
	Metadata   map[string]int32  `protobuf:"bytes,11,rep,name=metadata" json:"metadata,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"`
	Tags       []string          `protobuf:"bytes,12,rep,name=tags" json:"tags,omitempty"`
}
# api.ts

export abstract class MyFirstStruct {
  field?: string;
  fieldInt?: string;
  other_field?: string;
  metadata?: { [key: string]: number; };
  tags?: string[];
  static copy(from: MyFirstStruct, to?: MyFirstStruct): MyFirstStruct {
    to = to || {};
    to.field = from.field;
    to.fieldInt = from.fieldInt;
    to.other_field = from.other_field;
    to.metadata = from.metadata;
    to.tags = from.tags;
    return to;
  }
}

...

pbts's People

Contributors

octavore avatar

Watchers

Jason Lee Ertle avatar  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.