Skip to content

Latest commit

 

History

History
158 lines (115 loc) · 2.6 KB

README.md

File metadata and controls

158 lines (115 loc) · 2.6 KB

pb-util

Utilities for working with common protobuf types.

Installing

$ npm i --save pb-util

API

value.encode(val)

Encodes a JSON value into a google.protobuf.Value.

val

Type: string number boolean null object array

const {value} = require('pb-util');

const stringValue = value.encode('hello!');
// => {
//   kind: 'stringValue',
//   stringValue: 'hello!'
// }

value.decode(protoValue)

Decodes a google.protobuf.Value into a JSON value.

protoValue

Type: google.protobuf.Value

const {value} = require('pb-util');

const str = value.decode({
  kind: 'stringValue',
  stringValue: 'beep boop'
});
// => 'beep boop'

struct.encode(json)

Encodes a JSON object into a google.protobuf.Struct.

json

Type: object

const {struct} = require('pb-util');

const structValue = struct.encode({foo: 'bar'});
// => {
//   fields: {
//     foo: {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   }
// }

struct.decode(structValue)

Decodes a google.protobuf.Struct into a JSON object.

structValue

Type: google.protobuf.ListValue

const {struct} = require('pb-util');

const obj = struct.decode({
  fields: {
    foo: {
      kind: 'stringValue',
      stringValue: 'bar'
    },
    yes: {
      kind: 'boolValue',
      boolValue: true
    }
  }
});
// => {
//   foo: 'bar',
//   yes: true
// }

list.encode(array)

Encodes an array of JSON values into a google.protobuf.ListValue.

array

Type: array

const {list} = require('pb-util');

const listValue = list.encode(['foo', 'bar']);
// => {
//   values: [
//     {
//       kind: 'stringValue',
//       stringValue: 'foo'
//     },
//     {
//       kind: 'stringValue',
//       stringValue: 'bar'
//     }
//   ]
// }

list.decode(listValue)

Decodes a google.protobuf.ListValue into an array of JSON values.

listValue

Type: google.protobuf.ListValue

const {list} = require('pb-util');

const arr = list.decode({
  values: [
    {
      kind: 'stringValue',
      stringValue: 'foo'
    },
    {
      kind: 'numberValue',
      numberValue: 10
    }
  ]
});
// => ['foo', 10]

License

ISC