Skip to content

Commit

Permalink
fix the build
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloem committed Jan 5, 2022
1 parent e9acdd8 commit 3ba60a2
Showing 1 changed file with 83 additions and 6 deletions.
89 changes: 83 additions & 6 deletions sdks/node-ts/test/standard_coders_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,89 @@ const UNSUPPORTED_CODERS = [
"beam:coder:custom_window:v1"
];

const _urn_to_json_value_parser = {
'beam:coder:bytes:v1': x => new TextEncoder().encode(x),
'beam:coder:bool:v1': x => x,
'beam:coder:string_utf8:v1': x => x,
'beam:coder:varint:v1': x => x,
'beam:coder:kv:v1': (x, components) => (components[0](x['key']), components[1](x['value'])),
// 'beam:coder:interval_window:v1': lambda x: IntervalWindow(
// start=Timestamp(micros=(x['end'] - x['span']) * 1000),
// end=Timestamp(micros=x['end'] * 1000)),
// 'beam:coder:iterable:v1': lambda x,
// parser: list(map(parser, x)),
// 'beam:coder:global_window:v1': lambda x: window.GlobalWindow(),
// 'beam:coder:windowed_value:v1': lambda x,
// value_parser,
// window_parser: windowed_value.create(
// value_parser(x['value']),
// x['timestamp'] * 1000,
// tuple(window_parser(w) for w in x['windows'])),
// 'beam:coder:param_windowed_value:v1': lambda x,
// value_parser,
// window_parser: windowed_value.create(
// value_parser(x['value']),
// x['timestamp'] * 1000,
// tuple(window_parser(w) for w in x['windows']),
// PaneInfo(
// x['pane']['is_first'],
// x['pane']['is_last'],
// PaneInfoTiming.from_string(x['pane']['timing']),
// x['pane']['index'],
// x['pane']['on_time_index'])),
// 'beam:coder:timer:v1': lambda x,
// value_parser,
// window_parser: userstate.Timer(
// user_key=value_parser(x['userKey']),
// dynamic_timer_tag=x['dynamicTimerTag'],
// clear_bit=x['clearBit'],
// windows=tuple(window_parser(w) for w in x['windows']),
// fire_timestamp=None,
// hold_timestamp=None,
// paneinfo=None) if x['clearBit'] else userstate.Timer(
// user_key=value_parser(x['userKey']),
// dynamic_timer_tag=x['dynamicTimerTag'],
// clear_bit=x['clearBit'],
// fire_timestamp=Timestamp(micros=x['fireTimestamp'] * 1000),
// hold_timestamp=Timestamp(micros=x['holdTimestamp'] * 1000),
// windows=tuple(window_parser(w) for w in x['windows']),
// paneinfo=PaneInfo(
// x['pane']['is_first'],
// x['pane']['is_last'],
// PaneInfoTiming.from_string(x['pane']['timing']),
// x['pane']['index'],
// x['pane']['on_time_index'])),
// 'beam:coder:double:v1': parse_float,
// 'beam:coder:sharded_key:v1': lambda x,
// value_parser: ShardedKey(
// key=value_parser(x['key']), shard_id=x['shardId'].encode('utf-8')),
// 'beam:coder:custom_window:v1': lambda x,
// window_parser: window_parser(x['window'])
}

function get_json_value_parser(coderSpec) {
// TODO(pabloem): support 'beam:coder:row:v1' coder.
console.log(coderSpec);
if (coderSpec.components !== undefined) {
const componentParsers = coderSpec.components.map(c => get_json_value_parser(c));
return x => _urn_to_json_value_parser[coderSpec.coder.urn](x, componentParsers)
} else {
return x => _urn_to_json_value_parser[coderSpec.coder.urn](x)
}
}

describe("standard Beam coders on Javascript", function() {
const docs = yaml.loadAll(fs.readFileSync(STANDARD_CODERS_FILE, 'utf8'));
docs.forEach(doc => {
const urn = doc.coder.urn;
if (UNSUPPORTED_CODERS.includes(urn)) {
return;
}
const nested = doc.nested;
if (doc.nested === true) {
// TODO: support nesting of coders
return;
}
const nested = false;
const spec = doc;

const coder = CODER_REGISTRY.get(urn);
Expand All @@ -46,19 +121,21 @@ describe("standard Beam coders on Javascript", function() {
function describeCoder(coder, urn, nested, spec) {
describe(util.format("coder %s(%s) nested %s encodes properly", coder, urn, nested), function() {
let examples = 0;
const parser = get_json_value_parser(spec);
for (let expected in spec.examples) {
value = spec.examples[expected];
value = parser(spec.examples[expected]);
examples += 1;
coderCase(coder, value, expected, examples);
const expectedEncoded = new TextEncoder().encode(expected)
coderCase(coder, value, expectedEncoded, examples);
}
})
});
}

function coderCase(coder, obj, expectedEncoded, exampleCount) {
it(util.format("example %d", exampleCount), function() {
const encoded = coder.encode(obj);
const decoded = coder.decode(expectedEncoded);
assert.equal(expectedEncoded, encoded);
assert.equal(obj, decoded);
assert.deepEqual(expectedEncoded, encoded);
assert.deepEqual(obj, decoded);
});
}

0 comments on commit 3ba60a2

Please sign in to comment.