-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from mediachain/yn-friendly-repl
Refactor object model to support friendlier REPL interactions
- Loading branch information
Showing
24 changed files
with
954 additions
and
429 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,4 @@ | |
|
||
[options] | ||
suppress_comment= \\(.\\|\n\\)*\\$FlowIssue | ||
|
||
unsafe.enable_getters_and_setters=true |
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,76 @@ | ||
// @flow | ||
|
||
const { Statement, StatementBody } = require('./statement') | ||
import type { QueryResultMsg, QueryResultValueMsg, SimpleValueMsg, CompoundValueMsg } from '../protobuf/types' | ||
|
||
export type QueryResult = QueryResultValue | Error | ||
export type QueryResultValue = SimpleQueryResultValue | CompoundQueryResultValue | ||
export type SimpleQueryResultValue = number | string | Statement | StatementBody | ||
|
||
function unpackQueryResultProtobuf (msg: QueryResultMsg): QueryResult { | ||
if (msg.error != null) { | ||
const errorMsg = msg.error.error || 'Unknown error' | ||
return new Error(errorMsg) | ||
} | ||
if (msg.value != null) { | ||
return unpackQueryResultValueProtobuf((msg.value: any)) | ||
} | ||
throw new Error('Unexpected query result: ' + JSON.stringify(msg)) | ||
} | ||
|
||
function unpackQueryResultValueProtobuf (msg: QueryResultValueMsg): QueryResultValue { | ||
if (msg.simple != null) { | ||
return unpackSimpleValue((msg.simple: any)) | ||
} | ||
if (msg.compound != null) { | ||
return CompoundQueryResultValue.fromProtobuf((msg.compound: any)) | ||
} | ||
throw new Error('Unexpected query result value ' + JSON.stringify(msg)) | ||
} | ||
|
||
function unpackSimpleValue (val: SimpleValueMsg): SimpleQueryResultValue { | ||
if (val.stringValue != null) return (val.stringValue: any) | ||
if (val.intValue != null) return (val.intValue: any) | ||
if (val.stmt != null) return Statement.fromProtobuf((val.stmt: any)) | ||
if (val.stmtBody != null) return StatementBody.fromProtobuf((val.stmtBody: any)) | ||
|
||
throw new Error('Unexpected query result value: ' + JSON.stringify(val)) | ||
} | ||
|
||
type KVPair = {key: string, value: SimpleQueryResultValue} | ||
class CompoundQueryResultValue { | ||
body: Array<KVPair> | ||
|
||
constructor (body: Array<KVPair>) { | ||
this.body = body | ||
} | ||
|
||
static fromProtobuf (msg: CompoundValueMsg): CompoundQueryResultValue { | ||
return new CompoundQueryResultValue( | ||
msg.body.map(kv => ({ | ||
key: kv.key, | ||
value: unpackSimpleValue(kv.value) | ||
})) | ||
) | ||
} | ||
|
||
keys (): Array<string> { | ||
return this.body.map(kv => kv.key) | ||
} | ||
|
||
values (): Array<SimpleQueryResultValue> { | ||
return this.body.map(kv => kv.value) | ||
} | ||
|
||
statements (): Array<Statement> { | ||
return this.values() | ||
.filter(v => v instanceof Statement) | ||
.map(v => (v: any)) // dammit flow | ||
} | ||
} | ||
|
||
module.exports = { | ||
unpackQueryResultProtobuf, | ||
unpackQueryResultValueProtobuf, | ||
CompoundQueryResultValue | ||
} |
Oops, something went wrong.