Skip to content

Commit 8108875

Browse files
authored
fix: throw when unsupported fields are detected (#80)
`required` is a field in proto2 but not in proto3 so throw if it is encountered while generating `.ts` from `.proto`. It would be better to detect `proto2` from the `syntax` directive but it's not in the output of the `pbjs -t json` command. Fixes #34
1 parent 3ac2c56 commit 8108875

File tree

10 files changed

+25
-8
lines changed

10 files changed

+25
-8
lines changed

packages/protons-benchmark/src/decode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ new Benchmark.Suite()
4444
})
4545
.on('complete', function () {
4646
// @ts-expect-error types are wrong
47-
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
47+
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
4848
})
4949
// run async
5050
.run({ async: true })

packages/protons-benchmark/src/encode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ new Benchmark.Suite()
4242
})
4343
.on('complete', function () {
4444
// @ts-expect-error types are wrong
45-
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
45+
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
4646
})
4747
// run async
4848
.run({ async: true })

packages/protons-benchmark/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ new Benchmark.Suite()
6767
})
6868
.on('complete', function () {
6969
// @ts-expect-error types are wrong
70-
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
70+
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
7171
})
7272
// run async
7373
.run({ async: true })

packages/protons-benchmark/src/rpc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ new Benchmark.Suite()
4343
})
4444
.on('complete', function () {
4545
// @ts-expect-error types are wrong
46-
console.info(`Fastest is ${this.filter('fastest').map('name')}`) // eslint-disable-line @typescript-eslint/restrict-template-expressions
46+
console.info(`Fastest is ${this.filter('fastest').map('name')}`)
4747
})
4848
// run async
4949
.run({ async: true })

packages/protons/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
"pbjs": "^0.0.14",
163163
"protobufjs": "^7.0.0",
164164
"protons-runtime": "^4.0.0",
165-
"uint8arraylist": "^2.3.2",
166-
"uint8arrays": "^4.0.2"
165+
"uint8arraylist": "^2.3.2"
167166
}
168167
}

packages/protons/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ function defineModule (def: ClassDef): ModuleDef {
595595
fieldDef.repeated = fieldDef.rule === 'repeated'
596596
fieldDef.optional = !fieldDef.repeated && fieldDef.options?.proto3_optional === true
597597
fieldDef.map = fieldDef.keyType != null
598+
599+
if (fieldDef.rule === 'required') {
600+
throw new Error('"required" fields are not allowed in proto3 - please convert your proto2 definitions to proto3')
601+
}
598602
}
599603
}
600604

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
syntax = "proto2";
2+
3+
message Message {
4+
required string requiredField = 1;
5+
}

packages/protons/test/index.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-env mocha */
2-
/* eslint-disable @typescript-eslint/restrict-template-expressions */
32

43
import { expect } from 'aegir/chai'
54
import pbjs from 'pbjs'

packages/protons/test/maps.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/* eslint-env mocha */
2-
/* eslint-disable @typescript-eslint/restrict-template-expressions */
32

43
import { expect } from 'aegir/chai'
54
import { MapTypes, SubMessage } from './fixtures/maps.js'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* eslint-env mocha */
2+
3+
import { expect } from 'aegir/chai'
4+
import { generate } from '../src/index.js'
5+
6+
describe('unsupported', () => {
7+
it('should refuse to generate source from proto2 definition', async () => {
8+
await expect(generate('test/fixtures/proto2.proto', {})).to.eventually.be.rejected
9+
.with.property('message').that.contain('"required" fields are not allowed in proto3')
10+
})
11+
})

0 commit comments

Comments
 (0)