protocol-buffers-schema
Advanced tools
Comparing version 1.5.1 to 2.0.0
{ | ||
"name": "protocol-buffers-schema", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "No nonsense protocol buffers schema parser written in Javascript", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"standard": "^2.6.5", | ||
"tape": "^2.13.4" | ||
"standard": "^3.7.0", | ||
"tape": "^4.0.0" | ||
}, | ||
@@ -10,0 +10,0 @@ "scripts": { |
66
parse.js
@@ -36,2 +36,4 @@ var tokenize = require('./tokenize') | ||
tag: 0, | ||
map: null, | ||
oneof: null, | ||
required: false, | ||
@@ -49,2 +51,17 @@ repeated: false, | ||
case 'map': | ||
field.type = 'map' | ||
field.map = {from: null, to: null} | ||
tokens.shift() | ||
if (tokens[0] !== '<') throw new Error('Unexpected token in map type: ' + tokens[0]) | ||
tokens.shift() | ||
field.map.from = tokens.shift() | ||
if (tokens[0] !== ',') throw new Error('Unexpected token in map type: ' + tokens[0]) | ||
tokens.shift() | ||
field.map.to = tokens.shift() | ||
if (tokens[0] !== '>') throw new Error('Unexpected token in map type: ' + tokens[0]) | ||
tokens.shift() | ||
field.name = tokens.shift() | ||
break | ||
case 'repeated': | ||
@@ -81,3 +98,2 @@ case 'required': | ||
fields: [], | ||
oneof: [], | ||
extensions: null | ||
@@ -88,2 +104,3 @@ } | ||
switch (tokens[0]) { | ||
case 'map': | ||
case 'repeated': | ||
@@ -108,3 +125,13 @@ case 'optional': | ||
case 'oneof': | ||
body.oneof.push(ononeof(tokens)) | ||
tokens.shift() | ||
var name = tokens.shift() | ||
if (tokens[0] !== '{') throw new Error('Unexpected token in oneof: ' + tokens[0]) | ||
tokens.shift() | ||
while (tokens[0] !== '}') { | ||
tokens.unshift('optional') | ||
var field = onfield(tokens) | ||
field.oneof = name | ||
body.fields.push(field) | ||
} | ||
tokens.shift() | ||
break | ||
@@ -152,3 +179,2 @@ | ||
enums: [], | ||
oneof: [], | ||
messages: [], | ||
@@ -169,3 +195,2 @@ fields: [] | ||
msg.enums = body.enums | ||
msg.oneof = body.oneof | ||
msg.messages = body.messages | ||
@@ -239,35 +264,2 @@ msg.fields = body.fields | ||
var ononeof = function (tokens) { | ||
tokens.shift() | ||
var oneof = { | ||
name: tokens.shift(), | ||
fields: [] | ||
} | ||
if (tokens[0] !== '{') throw new Error('Expected { but found ' + tokens[0]) | ||
tokens.shift() | ||
while (tokens.length) { | ||
if (tokens[0] === '}') { | ||
tokens.shift() | ||
return oneof | ||
} | ||
var field = {} | ||
field.type = tokens.shift() | ||
field.name = tokens.shift() | ||
if (tokens[0] !== '=') throw new Error('Expected = but found ' + tokens[0]) | ||
tokens.shift() | ||
field.tag = Number(tokens.shift()) | ||
oneof.fields.push(field) | ||
if (tokens[0] !== ';') throw new Error('Expected ; but found ' + tokens[0]) | ||
tokens.shift() | ||
} | ||
throw new Error('No closing tag for oneof') | ||
} | ||
var onoption = function (tokens) { | ||
@@ -274,0 +266,0 @@ var name = null |
@@ -33,3 +33,3 @@ # protocol-buffers-schema | ||
var fs = require('fs') | ||
var schema = require('protobuf-schema') | ||
var schema = require('protocol-buffers-schema') | ||
@@ -36,0 +36,0 @@ // pass a buffer or string to schema.parse |
var onfield = function (f, result) { | ||
var prefix = f.repeated ? 'repeated' : f.required ? 'required' : 'optional' | ||
if (f.map) prefix = 'map<' + f.map.from + ',' + f.map.to + '>' | ||
if (f.oneof) prefix = '' | ||
@@ -10,3 +12,3 @@ var opts = Object.keys(f.options || {}).map(function (key) { | ||
result.push(prefix + ' ' + f.type + ' ' + f.name + ' = ' + f.tag + opts + ';') | ||
result.push((prefix ? prefix + ' ' : '') + (f.type === 'map' ? '' : f.type + ' ') + f.name + ' = ' + f.tag + opts + ';') | ||
return result | ||
@@ -28,7 +30,20 @@ } | ||
var oneofs = {} | ||
if (!m.fields) m.fields = [] | ||
m.fields.forEach(function (f) { | ||
result.push(onfield(f, [])) | ||
if (f.oneof) { | ||
if (!oneofs[f.oneof]) oneofs[f.oneof] = [] | ||
oneofs[f.oneof].push(onfield(f, [])) | ||
} else { | ||
result.push(onfield(f, [])) | ||
} | ||
}) | ||
Object.keys(oneofs).forEach(function (n) { | ||
oneofs[n].unshift('oneof ' + n + ' {') | ||
oneofs[n].push('}') | ||
result.push(oneofs[n]) | ||
}) | ||
result.push('}', '') | ||
@@ -35,0 +50,0 @@ return result |
@@ -10,3 +10,2 @@ { | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -19,2 +18,4 @@ "extensions": null, | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -28,2 +29,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -37,2 +40,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -47,3 +52,2 @@ "repeated": false, | ||
"enums": [], | ||
"oneof": [], | ||
"extensions": null, | ||
@@ -56,2 +60,4 @@ "messages": [], | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -65,2 +71,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -74,2 +82,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -76,0 +86,0 @@ "repeated": false, |
@@ -10,3 +10,2 @@ { | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -19,2 +18,4 @@ "extensions": null, | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -28,2 +29,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -37,2 +40,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -47,3 +52,2 @@ "repeated": false, | ||
"enums": [], | ||
"oneof": [], | ||
"extensions": null, | ||
@@ -56,2 +60,4 @@ "messages": [], | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -65,2 +71,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -74,2 +82,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -76,0 +86,0 @@ "repeated": false, |
@@ -20,3 +20,2 @@ { | ||
], | ||
"oneof": [], | ||
"messages": [ | ||
@@ -26,3 +25,2 @@ { | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -35,2 +33,4 @@ "extensions": null, | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -44,2 +44,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -59,2 +61,4 @@ "repeated": false, | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -68,2 +72,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -77,2 +83,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -86,2 +94,4 @@ "repeated": false, | ||
"tag": 4, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -96,3 +106,2 @@ "repeated": true, | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -105,2 +114,4 @@ "extensions": null, | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -107,0 +118,0 @@ "repeated": true, |
@@ -9,3 +9,2 @@ { | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -17,2 +16,4 @@ "fields": [ | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -26,2 +27,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -35,2 +38,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -44,2 +49,4 @@ "repeated": false, | ||
"tag": 101, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -53,2 +60,4 @@ "repeated": false, | ||
"tag": 102, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -64,3 +73,2 @@ "repeated": false, | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -72,2 +80,4 @@ "fields": [ | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -81,2 +91,4 @@ "repeated": false, | ||
"tag": 2, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -90,2 +102,4 @@ "repeated": false, | ||
"tag": 3, | ||
"map": null, | ||
"oneof": null, | ||
"required": true, | ||
@@ -99,2 +113,4 @@ "repeated": false, | ||
"tag": 101, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -108,2 +124,4 @@ "repeated": false, | ||
"tag": 102, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -127,3 +145,2 @@ "repeated": false, | ||
"enums": [], | ||
"oneof": [], | ||
"messages": [], | ||
@@ -135,2 +152,4 @@ "fields": [ | ||
"tag": 101, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -144,2 +163,4 @@ "repeated": false, | ||
"tag": 102, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
@@ -146,0 +167,0 @@ "repeated": false, |
@@ -7,36 +7,27 @@ { | ||
{ | ||
"name": "Data", | ||
"name": "SampleMessage", | ||
"enums": [], | ||
"oneof": [ | ||
{ | ||
"name": "type", | ||
"fields": [ | ||
{ | ||
"type": "string", | ||
"name": "string", | ||
"tag": 1 | ||
}, | ||
{ | ||
"type": "float", | ||
"name": "float", | ||
"tag": 2 | ||
}, | ||
{ | ||
"type": "bool", | ||
"name": "null", | ||
"tag": 3 | ||
} | ||
] | ||
} | ||
], | ||
"messages": [], | ||
"fields": [ | ||
{ | ||
"name": "value", | ||
"type": "type", | ||
"tag": 1, | ||
"name": "name", | ||
"type": "string", | ||
"tag": 4, | ||
"map": null, | ||
"oneof": "test_oneof", | ||
"required": false, | ||
"repeated": false, | ||
"options": {} | ||
}, | ||
{ | ||
"name": "sub_message", | ||
"type": "SubMessage", | ||
"tag": 9, | ||
"map": null, | ||
"oneof": "test_oneof", | ||
"required": false, | ||
"repeated": false, | ||
"options": {} | ||
} | ||
], | ||
@@ -43,0 +34,0 @@ "extensions": null |
{ | ||
"package":null, | ||
"package": null, | ||
"imports": ["./result.proto"], | ||
"enums":[], | ||
"enums": [], | ||
"extends": [], | ||
"messages":[{ | ||
"name":"SearchResponse", | ||
"extensions": null, | ||
"enums":[], | ||
"oneof": [], | ||
"messages":[], | ||
"fields":[ | ||
{ | ||
"name":"result","type":"Result","tag":1,"required":false,"repeated":true,"options":{} | ||
} | ||
] | ||
}], | ||
"messages": [ | ||
{ | ||
"name": "SearchResponse", | ||
"extensions": null, | ||
"enums": [], | ||
"messages": [], | ||
"fields": [ | ||
{ | ||
"name": "result", | ||
"type": "Result", | ||
"tag": 1, | ||
"map": null, | ||
"oneof": null, | ||
"required": false, | ||
"repeated": true, | ||
"options": {} | ||
} | ||
] | ||
} | ||
], | ||
"options":{} | ||
} |
@@ -74,1 +74,6 @@ var tape = require('tape') | ||
}) | ||
tape('schema with map', function (t) { | ||
t.same(schema.parse(fixture('map.proto')), require('./fixtures/map.json')) | ||
t.end() | ||
}) |
@@ -27,3 +27,3 @@ module.exports = function (sch) { | ||
return sch | ||
.replace(/([;,{}=\[\]]|\/\*|\*\/)/g, ' $1 ') | ||
.replace(/([;,{}=\[\]<>]|\/\*|\*\/)/g, ' $1 ') | ||
.split(/\n/) | ||
@@ -30,0 +30,0 @@ .map(trim) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
31236
26
1037