From f6962712d9b8b9ee4e6a65ee8d8c63f087fec0aa Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Thu, 24 Sep 2015 18:42:35 -0400 Subject: [PATCH] Add basic support for TypeRef in decode.js For now it decodes into an Immutable.Map --- js/src/decode.js | 29 +++++++++++++++++++++++++++++ js/test/decode.js | 26 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/js/src/decode.js b/js/src/decode.js index 0f94f54431..c5f5e78492 100644 --- a/js/src/decode.js +++ b/js/src/decode.js @@ -81,6 +81,35 @@ function decodeSet(input, ref, getChunk) { } function decodeType(input, ref, getChunk) { + return new Ref(ref, () => { + // We always have a kind an a name. + let p = decodeValue(input.name, ref, getChunk).then(name => { + return decodeValue(input.kind, ref, getChunk).then(kind => { + return {name, kind}; + }); + }); + + // Package is not yet implemented. Just use null for now. + if (input.pkgRef) { + throw new Error('Not implemented') + } else { + p = p.then(({name, kind}) => ({name, kind, pkg: null})); + } + + // If desc is present it is a list + p = p.then(({name, kind, pkg}) => { + if (!input.desc) { + return {name, kind, pkg, desc: null}; + } + return decodeValue(input.desc, ref, getChunk).deref().then(desc => { + return {name, kind, pkg, desc}; + }); + }); + + return p.then(obj => { + return Immutable.Map(obj); + }); + }); } function decodeCompoundBlob(value, ref, getChunk) { diff --git a/js/test/decode.js b/js/test/decode.js index f97494081a..44f4725ad4 100644 --- a/js/test/decode.js +++ b/js/test/decode.js @@ -84,6 +84,32 @@ suite('decode.js', function() { assert.isTrue(Immutable.List.of(true, false).equals(value)); }); + testCompound('type', 'j {"type":{"kind":{"uint8":0},"name":""}}', value => { + assert.isTrue(Immutable.Map.isMap(value)); + assert.equal(value.get('kind'), 0); + assert.equal(value.get('name'), ''); + }); + + testCompound('type with desc', 'j {"type":{"kind":{"uint8":14},"name":"T","desc":{"list":[{"ref":"sha1-list"},{"ref":"sha1-map"}]}}}', value => { + assert.isTrue(Immutable.Map.isMap(value)); + assert.equal(value.get('kind'), 14); + assert.equal(value.get('name'), 'T'); + assert.isTrue(Immutable.List.isList(value.get('desc'))); + assert.equal(2, value.get('desc').size); + assert.isTrue(Ref.isRef(value.get('desc').get(0))); + assert.isTrue(Ref.isRef(value.get('desc').get(1))); + }); + +testCompound('type enum', 'j {"type":{"desc":{"list":["f","g"]},"kind":{"uint8":18},"name":"enum"}}', value => { + assert.isTrue(Immutable.Map.isMap(value)); + assert.equal(value.get('kind'), 18); + assert.equal(value.get('name'), 'enum'); + assert.isTrue(Immutable.List.isList(value.get('desc'))); + assert.equal(2, value.get('desc').size); + assert.equal('f', value.get('desc').get(0)); + assert.equal('g', value.get('desc').get(1)); +}); + test('blob', done => { let data = 'b abc'; let ref = 'sha1-c0ffee';