Skip to content

Commit

Permalink
Add option to omit record methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mtth committed Oct 11, 2020
1 parent f3c6b1c commit 75ed60e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/types.js
Expand Up @@ -2109,7 +2109,10 @@ function RecordType(schema, opts) {
}, this));
this._branchConstructor = this._createBranchConstructor();
this._isError = schema.type === 'error';
this.recordConstructor = this._createConstructor(opts.errorStackTraces);
this.recordConstructor = this._createConstructor(
opts.errorStackTraces,
opts.omitRecordMethods
);
this._read = this._createReader();
this._skip = this._createSkipper();
this._write = this._createWriter();
Expand All @@ -2126,7 +2129,7 @@ RecordType.prototype._getConstructorName = function () {
this._isError ? 'Error$' : 'Record$';
};

RecordType.prototype._createConstructor = function (errorStackTraces) {
RecordType.prototype._createConstructor = function (errorStack, plainRecords) {
// jshint -W054
var outerArgs = [];
var innerArgs = [];
Expand All @@ -2139,7 +2142,7 @@ RecordType.prototype._createConstructor = function (errorStackTraces) {
hasDefault = defaultValue() !== undefined;
name = field.name;
if (
errorStackTraces && this._isError && name === 'stack' &&
errorStack && this._isError && name === 'stack' &&
Type.isType(field.type, 'string') && !hasDefault
) {
// We keep track of whether we've encountered a valid stack field (in
Expand Down Expand Up @@ -2174,6 +2177,9 @@ RecordType.prototype._createConstructor = function (errorStackTraces) {
var outerBody = 'return function ' + this._getConstructorName() + '(';
outerBody += innerArgs.join() + ') {\n' + innerBody + '};';
var Record = new Function(outerArgs.join(), outerBody).apply(undefined, ds);
if (plainRecords) {
return Record;
}

var self = this;
Record.getType = function () { return self; };
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "avsc",
"version": "5.4.22",
"version": "5.5.0",
"description": "Avro for JavaScript",
"homepage": "https://github.com/mtth/avsc",
"keywords": [
Expand Down
12 changes: 12 additions & 0 deletions test/test_types.js
Expand Up @@ -2388,6 +2388,18 @@ suite('types', function () {
assert.equal(t.getField('id').getType().getName(), 'Bar');
});

test('omit record methods', function () {
var t = Type.forSchema({
type: 'record',
name: 'Foo',
fields: [{name: 'id', type: 'string'}]
}, {omitRecordMethods: true});
var Foo = t.recordConstructor;
assert.strictEqual(Foo.type, undefined);
var v = t.clone({id: 'abc'});
assert.strictEqual(v.toBuffer, undefined);
});

});

suite('AbstractLongType', function () {
Expand Down

0 comments on commit 75ed60e

Please sign in to comment.