@wix/be-http-binding
Advanced tools
Comparing version 1.0.4 to 1.0.5
@@ -8,16 +8,8 @@ | ||
return { | ||
field(name, type, tag) { | ||
return messageBuilder({ | ||
...context, | ||
[name]: { | ||
type, tag | ||
} | ||
}); | ||
field(name, type, modifier) { | ||
return defineField(context, name, type, modifier); | ||
}, | ||
repeated(name, type, tag) { | ||
return messageBuilder({ | ||
...context, | ||
[name]: { | ||
type, tag, repeated: true | ||
} | ||
repeated(name, type, modifier) { | ||
return defineField(context, name, type, modifier, { | ||
repeated: true | ||
}); | ||
@@ -31,2 +23,15 @@ }, | ||
function defineField(context, name, type, modifier, props) { | ||
const resolvedModifier = modifierFrom(modifier); | ||
const modifierState = resolvedModifier.define ? resolvedModifier.define({name, type}) : undefined; | ||
return messageBuilder({ | ||
...context, | ||
[name]: { | ||
type, modifier: resolvedModifier, modifierState, | ||
...props | ||
} | ||
}); | ||
} | ||
function message(context) { | ||
@@ -36,11 +41,25 @@ return { | ||
const result = {}; | ||
const valueModifiers = []; | ||
Object.keys(context).forEach((field) => { | ||
if (context[field].repeated) { | ||
result[field] = value[field].map((value) => context[field].type.fromValue(value)); | ||
const {repeated, modifier, type} = context[field]; | ||
if (repeated) { | ||
result[field] = (Array.isArray(value[field]) ? value[field] : [value[field]]).map((value) => type.fromValue(value)); | ||
} else { | ||
result[field] = context[field].type.fromValue(value[field]); | ||
result[field] = type.fromValue(value[field]); | ||
} | ||
if (modifier && modifier.value) { | ||
valueModifiers.push({ | ||
field, | ||
modifier | ||
}); | ||
} | ||
}); | ||
valueModifiers.forEach(({field, modifier}) => { | ||
result[field] = modifier.value(result); | ||
}); | ||
return result; | ||
@@ -50,1 +69,19 @@ } | ||
} | ||
function modifierFrom(modifier) { | ||
if (typeof(modifier) === 'number') { | ||
return assignTag(modifier); | ||
} | ||
return modifier; | ||
} | ||
function assignTag(tag) { | ||
return { | ||
define() { | ||
return { | ||
tag | ||
}; | ||
} | ||
} | ||
} |
const {messageBuilder} = require('./message-builder'); | ||
const {string} = require('./well-known-types'); | ||
const {string, int32} = require('./well-known-types'); | ||
const {expect} = require('chai'); | ||
const _ = require('lodash'); | ||
@@ -20,3 +21,3 @@ describe('message-builder', () => { | ||
it('should read a simple repeated message', () => { | ||
it('should read a simple message with repeated field', () => { | ||
const message = messageBuilder() | ||
@@ -32,2 +33,36 @@ .repeated('names', string, 1) | ||
}); | ||
it('should single field as repeated', () => { | ||
const message = messageBuilder() | ||
.repeated('names', string, 1) | ||
.build(); | ||
expect(message.fromValue({ | ||
names: 'John' | ||
})).to.deep.equal({ | ||
names: ['John'] | ||
}); | ||
}); | ||
it('should add field modifier', () => { | ||
const message = messageBuilder() | ||
.repeated('values', int32, 1) | ||
.field('sum', int32, sumOf('values')) | ||
.build(); | ||
expect(message.fromValue({ | ||
values: [1, 2] | ||
})).to.deep.equal({ | ||
values: [1, 2], | ||
sum: 3 | ||
}); | ||
}); | ||
function sumOf(fieldName) { | ||
return { | ||
value(message) { | ||
return _.sum(message[fieldName]); | ||
} | ||
}; | ||
} | ||
}); |
{ | ||
"name": "@wix/be-http-binding", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"author": "Mantas Indrašius <mantasi@wix.com>", | ||
@@ -19,2 +19,3 @@ "publishConfig": { | ||
"chai": "^4.2.0", | ||
"lodash": "^4.17.11", | ||
"mocha": "^5.2.0", | ||
@@ -21,0 +22,0 @@ "mocha-env-reporter": "^3.0.0", |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
23607
229
0
8