fabric-contract-api
Advanced tools
Comparing version 1.4.0-snapshot.49 to 1.4.0-snapshot.50
@@ -24,3 +24,3 @@ /* | ||
*/ | ||
toBuffer(result) { | ||
toBuffer(result, schema = {}) { | ||
@@ -30,4 +30,14 @@ // relay on the default algorithms, including for Buffers. Just retunring the buffer | ||
if (result) { | ||
const payload = JSON.stringify(result); | ||
return Buffer.from(payload); | ||
// check that schema to see exactly how we should de-marshall this | ||
if (schema.type && (schema.type === 'string' || schema.type === 'number')) { | ||
// ok so this is a basic primitive type, and for strings and numbers the wireprotocol is different | ||
// double check the type of the result passed in | ||
if (schema.type !== typeof result) { | ||
throw new Error(`Returned value is ${typeof result} does not match schema type of ${schema.type}`); | ||
} | ||
return Buffer.from(result.toString()); | ||
} else { | ||
const payload = JSON.stringify(result); | ||
return Buffer.from(payload); | ||
} | ||
} else { | ||
@@ -34,0 +44,0 @@ return; |
{ | ||
"name": "fabric-contract-api", | ||
"version": "1.4.0-snapshot.49", | ||
"version": "1.4.0-snapshot.50", | ||
"tag": "unstable", | ||
@@ -5,0 +5,0 @@ "description": "A node.js implementation of Hyperledger Fabric chaincode shim, to allow endorsing peers and user-provided chaincodes to communicate with each other", |
@@ -77,3 +77,3 @@ /* | ||
it ('should return undefined if nothing passed in ', () => { | ||
it ('should return undefined if nothing passed in (no schema) ', () => { | ||
const sc0 = new JSONSerializer(); | ||
@@ -83,6 +83,4 @@ expect(sc0.toBuffer()).to.be.equal(undefined); | ||
it ('should return stringifed result', () => { | ||
it ('should return stringifed result (no schema)', () => { | ||
const sc0 = new JSONSerializer(); | ||
for (let i = 0; i < data.length; i++) { | ||
@@ -92,2 +90,19 @@ expect(sc0.toBuffer(data[i])).to.deep.equal(buffer[i]); | ||
}); | ||
it ('should return string from a string in result if schema given', () => { | ||
const sc0 = new JSONSerializer(); | ||
expect(sc0.toBuffer('hello world', {type:'string'})).to.deep.equal(Buffer.from('hello world')); | ||
}); | ||
it ('should return number from a number in result if schema given', () => { | ||
const sc0 = new JSONSerializer(); | ||
expect(sc0.toBuffer(42, {type:'number'})).to.deep.equal(Buffer.from('42')); | ||
}); | ||
it ('should throw an error if the type of data passed does not match schema given', () => { | ||
const sc0 = new JSONSerializer(); | ||
(() => { | ||
sc0.toBuffer(42, {type:'string'}); | ||
}).should.throw(/Returned value is .* does not match schema type of .*/); | ||
}); | ||
}); | ||
@@ -94,0 +109,0 @@ |
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
89378
2245