Add basic support for TypeRef in decode.js

For now it decodes into an Immutable.Map
This commit is contained in:
Erik Arvidsson
2015-09-24 18:42:35 -04:00
parent ba08bbe32e
commit f6962712d9
2 changed files with 55 additions and 0 deletions
+29
View File
@@ -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) {
+26
View File
@@ -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';