Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample generated AssemblyScript BSON bindings
import "allocator/arena";
// TODO: Why cannot import from index?
// import { BSONEncoder, BSONDecoder } from "../../../assemblyscript-bson/assembly";
import { BSONEncoder } from "../../../assemblyscript-bson/assembly/encoder";
import { BSONDecoder } from "../../../assemblyscript-bson/assembly/decoder";
declare function sayHello(): void;
export class FooBar {
foo: i32 = 0;
bar: i32 = 1;
flag: bool;
baz: string = "123";
foobar: Uint8Array;
}
sayHello();
export function add(x: i32, y: i32): i32 {
return x + y;
}
export function __near_encode_FooBar(value: FooBar): Uint8Array {
let encoder: BSONEncoder = new BSONEncoder();
if (value.foo != null) {
encoder.setInteger("foo", value.foo);
} else {
encoder.setNull("foo");
}
if (value.bar != null) {
encoder.setInteger("bar", value.bar);
} else {
encoder.setNull("bar");
}
if (value.flag != null) {
encoder.setBoolean("flag", value.flag);
} else {
encoder.setNull("flag");
}
if (value.baz != null) {
encoder.setString("baz", value.baz);
} else {
encoder.setNull("baz");
}
if (value.foobar != null) {
encoder.setUint8Array("foobar", value.foobar);
} else {
encoder.setNull("foobar");
}
return encoder.serialize();
}
export class __near_BSONHandler_FooBar {
value: FooBar = new FooBar();
setInteger(name: string, value: i32): void {
if (name == "foo") {
this.value.foo = value;
return;
}
if (name == "bar") {
this.value.bar = value;
return;
}
}
setString(name: string, value: String): void {
if (name == "baz") {
this.value.baz = value;
return;
}
}
setUint8Array(name: string, value: Uint8Array): void {
if (name == "foobar") {
this.value.foobar = value;
return;
}
}
setBoolean(name: string, value: bool): void {
if (name == "flag") {
this.value.flag = value;
return;
}
}
setNull(name: string): void {
if (name == "foo") {
this.value.foo = <i32>null;
return;
}
if (name == "bar") {
this.value.bar = <i32>null;
return;
}
if (name == "flag") {
this.value.flag = <bool>null;
return;
}
if (name == "baz") {
this.value.baz = <String>null;
return;
}
if (name == "foobar") {
this.value.foobar = <Uint8Array>null;
return;
}
}
pushObject(name: string): bool {
return false;
}
popObject(): void {}
pushArray(name: string): bool {
return false;
}
popArray(): void {}
}
export function __near_decode_FooBar(buffer: Uint8Array, offset: i32): FooBar {
let handler = new __near_BSONHandler_FooBar();
let decoder = new BSONDecoder<__near_BSONHandler_FooBar>(handler);
decoder.deserialize(buffer, offset);
return handler.value;
}
export class __near_ArgsParser_add {
__near_param_x: i32;
__near_param_y: i32;
setInteger(name: string, value: i32): void {
if (name == "x") {
this.__near_param_x = value;
return;
}
if (name == "y") {
this.__near_param_y = value;
return;
}
}
setString(name: string, value: String): void {}
setUint8Array(name: string, value: Uint8Array): void {}
setBoolean(name: string, value: bool): void {}
setNull(name: string): void {
if (name == "x") {
this.__near_param_x = <i32>null;
return;
}
if (name == "y") {
this.__near_param_y = <i32>null;
return;
}
}
pushObject(name: string): bool {
return false;
}
popObject(): void {}
pushArray(name: string): bool {
return false;
}
popArray(): void {}
}
function __near_func_add(bson: Uint8Array): Uint8Array {
let handler = new __near_ArgsParser_add();
let decoder = new BSONDecoder<__near_ArgsParser_add>(handler);
decoder.deserialize(bson);
let result = add(handler.__near_param_x, handler.__near_param_y);
let encoder = new BSONEncoder();
if (result != null) {
encoder.setInteger("result", result);
} else {
encoder.setNull("result");
}
return encoder.serialize();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.