-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non-immediate: Add map type and associated tests.
- Loading branch information
Showing
4 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const path = require('path') | ||
|
||
const Schema = require(path.join(__dirname, '..', '..', 'model', 'Schema.js')) | ||
const list = require(path.join(__dirname, 'list.js')) | ||
|
||
const schemaCache = new Map() | ||
|
||
const genMapSchema = (data) => { | ||
if (schemaCache.has(data)) { | ||
return schemaCache.get(data) | ||
} | ||
else { | ||
const generated = new Schema([ | ||
{ | ||
'name': 'pairs', | ||
'type': list, | ||
'of': new Schema([ | ||
Object.assign(data.key, { | ||
'name': 'key' | ||
}), | ||
Object.assign(data.value, { | ||
'name': 'value' | ||
}) | ||
]) | ||
} | ||
]) | ||
|
||
schemaCache.set(data, generated) | ||
|
||
return generated | ||
} | ||
} | ||
|
||
module.exports = { | ||
'parse': (buf, from, data) => { | ||
if (buf.length - from < 1) { | ||
return { | ||
'hadUnderflow': true | ||
} | ||
} | ||
|
||
const mapSchema = genMapSchema(data) | ||
|
||
const parseResult = mapSchema.parse(buf, from, { | ||
'returnDetails': true | ||
}) | ||
|
||
if (parseResult.hadUnderflow) { | ||
return { | ||
'hadUnderflow': true | ||
} | ||
} | ||
|
||
const parsedMap = new Map(parseResult.data.pairs.map((pair) => [pair.key, pair.value])) | ||
|
||
return { | ||
'readBytes': parseResult.finishedIndex - from, | ||
'data': parsedMap, | ||
'hadUnderflow': false | ||
} | ||
}, | ||
'serialize': (value, data) => { | ||
const mapSchema = genMapSchema(data) | ||
|
||
return mapSchema.build({ | ||
'pairs': Array.from(value).map((entry) => new Object({ | ||
'key': entry[0], | ||
'value': entry[1] | ||
})) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters