-
Notifications
You must be signed in to change notification settings - Fork 14
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 #1 from recursethis/fork/main
Added Protobuf Support
- Loading branch information
Showing
11 changed files
with
200 additions
and
10 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
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,87 @@ | ||
import { OutputFormat } from "./outputFormat"; | ||
import protobuf from "protobufjs"; | ||
import alert from "cli-alerts"; | ||
import { globSync } from "glob"; | ||
|
||
export class ProtoFormat implements OutputFormat { | ||
private schemas: any = {}; | ||
private schemaFiles: Set<string>; | ||
|
||
static async getProtoSchemas(megaRecord: any, protoSchemaFiles: string[]) { | ||
|
||
if (!protoSchemaFiles || protoSchemaFiles.length === 0) { | ||
protoSchemaFiles = []; | ||
protoSchemaFiles.push(...(await ProtoFormat.getProtoSchemaFiles(megaRecord))); | ||
} | ||
|
||
const protoSchemas = {}; | ||
const protoRoot = protobuf.loadSync(protoSchemaFiles); | ||
for (const topic in megaRecord) { | ||
|
||
const protoSchema = {}; | ||
try { | ||
protoSchema["messageType"] = protoRoot.lookupType(megaRecord[topic].schema); | ||
protoSchema["name"] = topic | ||
protoSchema["namespace"] = megaRecord[topic].schema | ||
|
||
if (global.debug) { | ||
alert({ | ||
type: `success`, | ||
name: `Proto Schema for topic ${topic}:`, | ||
msg: `\n ${JSON.stringify(protoSchema, null, 2)}` | ||
}); | ||
} | ||
|
||
protoSchemas[topic] = protoSchema; | ||
} catch (error) { | ||
alert({ | ||
type: `error`, | ||
name: `protobuf lookup type error for schema ${megaRecord[topic].schema}`, | ||
msg: `${error}` | ||
}); | ||
process.exit(1); | ||
|
||
} | ||
} | ||
|
||
return protoSchemas; | ||
} | ||
|
||
static async getProtoSchemaFiles(megaRecord: any) { | ||
const protoFiles = new Set<string>(); | ||
for (const topic in megaRecord) { | ||
(await ProtoFormat.getProtoSchemaFilesSync(megaRecord[topic].schemaDir)).forEach(file => protoFiles.add(file)); | ||
} | ||
return protoFiles; | ||
} | ||
|
||
static async getProtoSchemaFilesSync(directory: string) { | ||
if (!directory) { | ||
return []; | ||
} | ||
return globSync(directory + (directory.endsWith("/") ? "" : "/") + "**/*.proto"); | ||
} | ||
|
||
async register(megaRecord: any): Promise<void> { | ||
this.schemaFiles = await ProtoFormat.getProtoSchemaFiles(megaRecord); | ||
this.schemas = await ProtoFormat.getProtoSchemas(megaRecord, Array.from(this.schemaFiles)); | ||
} | ||
|
||
async encode(record: any, topic: string): Promise<Buffer> { | ||
const messageType = this.schemas[topic]['messageType']; | ||
|
||
// check if the message is valid | ||
const error = messageType.verify(record); | ||
if (global.debug && error) { | ||
alert({ | ||
type: `warning`, | ||
name: `${record} with ${this.schemas[topic]['namespace']} is not valid`, | ||
msg: `${error}` | ||
}); | ||
} | ||
// if the message is not valid, convert plain object | ||
const message = error ? messageType.fromObject(record) : messageType.create(record); | ||
|
||
return messageType.encode(message).finish(); | ||
} | ||
} |
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,3 @@ | ||
export function accessRecordKey(path: string, record: any): any { | ||
return path?.split('.').reduce((level, key) => level && level[key], record); | ||
} |
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 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,38 @@ | ||
syntax = "proto3"; | ||
|
||
package datagen; | ||
|
||
// Definition for the User data | ||
message User { | ||
message Nested { | ||
string phone = 1; | ||
string website = 2; | ||
} | ||
|
||
int32 id = 1; // Assuming IDs are integers | ||
string name = 2; | ||
string email = 3; | ||
string phone = 4; | ||
string website = 5; | ||
string city = 6; | ||
string company = 7; | ||
Nested nested = 8; // Nested message for phone and website | ||
} | ||
|
||
// Definition for the Post data | ||
message Post { | ||
int32 id = 1; | ||
int32 user_id = 2; // Assuming this is a reference to a User ID | ||
string title = 3; | ||
string body = 4; | ||
} | ||
|
||
// Definition for the Comment data | ||
message Comment { | ||
int32 id = 1; | ||
int32 user_id = 2; // Assuming this is a reference to a User ID | ||
int32 post_id = 3; // Assuming this is a reference to a Post ID | ||
string body = 4; | ||
int32 views = 5; // Assuming views is an integer | ||
int32 status = 6; // Assuming status is an integer representing some enum | ||
} |