Giter VIP home page Giter VIP logo

protobuf2flowtype's Introduction

npm version CircleCI codecov

protobuf2flowtype

The aim of this project is to allow flowtype checking of protobuf messages created using protobufjs

This is achieved by generating modules that match the namespaces within your protobuf deinition which export the protobufjs builder for that namespace but also include all the nessisary flowtype annotations for the messages and enums declared in that namespace.

The generated code has a runtime dependency on this module for flowtype checking only. The generated code includes the flowtype annotations inside special comment blocks so the code is fully ES5 compatible and doesn't require transpiling to run.

Usage

CLI

npm install --save protobuf2flowtype
node_modules/.bin/protobuf2flowtype --outDir src/proto --protoFile main.proto --protoRoot proto

API

const generate = require('protobuf2flowtype/build/generate'); 

generate('src/proto', 'main.proto', 'proto');

Example

###main.proto

package foo;

message Greeting {

    required string text = 1;

};

message Chat {

    required Greeting greeting = 1;
    optional string body = 2;
};

###generated code

.
├── foo
│   ├── Chat
│   │   └── index.js
│   ├── Greeting
│   │   └── index.js
│   └── index.js
└── index.js

###index.js

// @flow
Object.defineProperty(exports, "__esModule", {
  value: true
});

const _package = require('./package.json');

const jsonDescriptor = {
  "package": "foo",
  "messages": [{
    "name": "Greeting",
    "fields": [{
      "rule": "required",
      "type": "string",
      "name": "text",
      "id": 1
    }]
  }, {
    "name": "Chat",
    "fields": [{
      "rule": "required",
      "type": "Greeting",
      "name": "greeting",
      "id": 1
    }, {
      "rule": "optional",
      "type": "string",
      "name": "body",
      "id": 2
    }]
  }]
};

const ProtoBuf = require('protobufjs');

const builder = exports.builder = ProtoBuf.loadJson(jsonDescriptor);
exports.default = builder;

###foo/index.js

// @flow
Object.defineProperty(exports, "__esModule", {
  value: true
});

const _ = require('../index');

exports.builder = _.builder; /*::
import { Protobuf$Message } from 'protobuf2flowtype';
import type { ByteBuffer, ProtoBuf$Builder, ProtoBuf$MessageLiteral, Protobuf$RefectType } from 'protobuf2flowtype';
interface GreetingInterface extends ProtoBuf$MessageLiteral {
  text: string
}
;
type GreetingReflect = Protobuf$RefectType<GreetingInterface>;
declare class GreetingBuilder extends Protobuf$Message<GreetingInterface, GreetingReflect> {
  constructor: (values: GreetingInterface) => GreetingBuilder;
  text: string;
  getText: () => string;
  setText: (text: string) => void;
}
;
interface ChatInterface extends ProtoBuf$MessageLiteral {
  greeting: $Subtype<GreetingInterface>;
  body?: ?string;
}
;
type ChatReflect = Protobuf$RefectType<ChatInterface>;
declare class ChatBuilder extends Protobuf$Message<ChatInterface, ChatReflect> {
  constructor: (values: ChatInterface) => ChatBuilder;
  greeting: GreetingBuilder;
  getGreeting: () => GreetingBuilder;
  setGreeting: (greeting: GreetingBuilder) => void;
  body?: ?string;
  getBody: () => ?string;
  setBody: (body: ?string) => void;
}
;
type foo = {
  Greeting: Class<GreetingBuilder>;
  Chat: Class<ChatBuilder>;
};
export type { GreetingInterface, GreetingBuilder, GreetingReflect, ChatInterface, ChatBuilder, ChatReflect }; */

const namespace /*: foo */ = _.builder.build('foo');

exports.Greeting = namespace.Greeting; /*::
export const Greeting = namespace.Greeting; */
exports.Chat = namespace.Chat; /*::
export const Chat = namespace.Chat; */
exports.default = namespace; /*::
export default namespace; */

###Usage

/* @flow */
import {Chat, Greating} from './proto/foo';

const chat: Chat = new Chat({
    greeting: new Greating({ text: 'hello'}),
    body: 'some text'
});

##Missing features Currently doesn't support

  • Passing constructor arguments to protobufjs builders
  • service definitions
  • map fields

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.