No fuckin idea what.

This commit is contained in:
didier amyot committed 2023-01-25 09:59:32 -05:00
1 parent d74d8b9d3e
commit e1e5d5dbf3
4 files changed
+180 -48

No files matched your search

+75 -17
View File
@@ -1,6 +1,7 @@
import {Command} from "https://deno.land/x/cliffy@v0.25.7/command/mod.ts";
import {concat, partial} from 'https://deno.land/x/lodash@4.17.15-es/lodash.js';
import {join, dirname} from "https://deno.land/std@0.171.0/path/mod.ts";
import {dirname, join} from "https://deno.land/std@0.171.0/path/mod.ts";
import avro from "npm:avro-js@1.11.1";
enum AvroPrimitives {
@@ -43,7 +44,7 @@ type AvroUnion = AvroTypes[];
type AvroTypes = AvroPrimitives | AvroRecord | AvroUnion | AvroEnum;
const CacheChequeEvent: AvroRecord = {
name: "event",
name: "CacheChequeEvent",
type: "record",
fields: [
{name: "account", type: AvroPrimitives.Long},
@@ -51,7 +52,7 @@ const CacheChequeEvent: AvroRecord = {
}
const BogusEvent: AvroRecord = {
name: "bogus",
name: "BogusEvent",
type: "record",
fields: [
{name: "account", type: AvroPrimitives.Long},
@@ -92,7 +93,7 @@ async function drySerializer(outputDir: string, rec: AvroRecord): Promise<void>
}
async function fileSerializer(outputDir: string, rec: AvroRecord): Promise<void> {
let outputFile = getOutputFileName(outputDir, rec);
const outputFile = getOutputFileName(outputDir, rec);
await Deno.mkdir(dirname(outputFile), { recursive: true });
await Deno.writeTextFile(outputFile, JSON.stringify(rec, null, 4));
@@ -102,24 +103,81 @@ function getSerializer(dry: boolean, outputDir: string): Serializer {
return dry ? partial(drySerializer, outputDir) : partial(fileSerializer, outputDir);
}
async function main(options: CliOptions): Promise<void> {
let serializer = getSerializer(options.dry, options.output);
for(let e of concat([], allEvents, LedgerEventsSchema)){
await serializer(e);
async function buildSchemata(options: CliOptions): Promise<void> {
const serializer = getSerializer(options.dry, options.output);
for (const e of concat([], allEvents, LedgerEventsSchema, eventTypes)) {
await serializer(e);
}
}
async function testShit(): Promise<void> {
const type = avro.parse(CacheChequeEvent);
const buff = await Deno.readFile("/tmp/foo.data");
console.log(buff);
console.log(type.fromBuffer(buff));
}
async function createMockData(): Promise<void> {
const type = avro.parse(
{
"name": "CacheChequeEvent",
"type": "record",
"fields": [
{
"name": "account",
"type": "long"
}
]
}
);
const e = {
account: 666
// event_type: CacheChequeEvent.name,
// event: {CacheChequeEvent: {account: 1788301184}}
};
const encoder = avro.createFileEncoder("/tmp/foo.data", type);
encoder.write({account: 666});
encoder.end();
return;
// const file = await Deno.open("/tmp/foo.data", {write: true, create: true});
// for (let i = 0; i < 1; i++) {
// await Deno.write(file.rid, type.toBuffer(e).buffer);
// }
}
function buildSchemataCommand() {
return new Command()
.description("Build the avro schemata")
.option("--dry [value:boolean]", "Do not output any files", {
default: false,
})
.option("--output [value:file]", "directory to output schemata", {
default: '.',
})
.action(buildSchemata);
}
function buildMockEventsCommand() {
return new Command()
.description("Generate mock data")
.option("--output [value:file]", "output data file", {
default: '.',
})
.action(createMockData);
}
function buildTestingCommand() {
return new Command()
.description("Test mock data")
.action(testShit);
}
await new Command()
.name("ledger-avro-schemata-builder")
.version("0.1.0")
.description("Build avro schemata for ledger.")
.option("--dry [value:boolean]", "Do not output any files", {
default: false,
})
.option("--output [value:file]", "directory to output schemata", {
default: '.',
})
.action(main)
.command('build-schema', buildSchemataCommand())
.command('create-mock-data', buildMockEventsCommand())
.command('testing', buildTestingCommand())
.parse(Deno.args);