Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw when unsupported fields are detected #80

Merged
merged 4 commits into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/protons-benchmark/src/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ new Benchmark.Suite()
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
})
// run async
.run({ async: true })
2 changes: 1 addition & 1 deletion packages/protons-benchmark/src/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ new Benchmark.Suite()
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
})
// run async
.run({ async: true })
2 changes: 1 addition & 1 deletion packages/protons-benchmark/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ new Benchmark.Suite()
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
})
// run async
.run({ async: true })
2 changes: 1 addition & 1 deletion packages/protons-benchmark/src/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ new Benchmark.Suite()
})
.on('complete', function () {
// @ts-expect-error types are wrong
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
})
// run async
.run({ async: true })
3 changes: 1 addition & 2 deletions packages/protons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@
"pbjs": "^0.0.14",
"protobufjs": "^7.0.0",
"protons-runtime": "^4.0.0",
"uint8arraylist": "^2.3.2",
"uint8arrays": "^4.0.2"
"uint8arraylist": "^2.3.2"
}
}
4 changes: 4 additions & 0 deletions packages/protons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ function defineModule (def: ClassDef): ModuleDef {
fieldDef.repeated = fieldDef.rule === 'repeated'
fieldDef.optional = !fieldDef.repeated && fieldDef.options?.proto3_optional === true
fieldDef.map = fieldDef.keyType != null

if (fieldDef.rule === 'required') {
throw new Error('"required" fields are not allowed in proto3 - please convert your proto2 definitions to proto3')
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/protons/test/fixtures/proto2.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto2";

message Message {
required string requiredField = 1;
}
1 change: 0 additions & 1 deletion packages/protons/test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env mocha */
/* eslint-disable @typescript-eslint/restrict-template-expressions */

import { expect } from 'aegir/chai'
import pbjs from 'pbjs'
Expand Down
1 change: 0 additions & 1 deletion packages/protons/test/maps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-env mocha */
/* eslint-disable @typescript-eslint/restrict-template-expressions */

import { expect } from 'aegir/chai'
import { MapTypes, SubMessage } from './fixtures/maps.js'
Expand Down
11 changes: 11 additions & 0 deletions packages/protons/test/unsupported.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* eslint-env mocha */

import { expect } from 'aegir/chai'
import { generate } from '../src/index.js'

describe('unsupported', () => {
it('should refuse to generate source from proto2 definition', async () => {
await expect(generate('test/fixtures/proto2.proto', {})).to.eventually.be.rejected
.with.property('message').that.contain('"required" fields are not allowed in proto3')
})
})