yarn add jsbuffer
npm i jsbuffer
npm install -g jsbuffer
jsbuffer schema/src -o schema
jsbuffer schema/src -o schema --extends tsconfig.base.json
jsbuffer is the implementation of a type language. We offer tools for you to generate TypeScript interfaces and functions to encode and decode your data, and more.
You can try jsbuffer online in the online playground, still in very early progress, but it works.
The new jsb
command-line tool supports generating code for additional languages, currently, the CLI offer experimental support for the following languages:
- C99
- C++17
npm i -g jsbuffer@^2
jsb -h
Create a schema/main
file:
type User {
int id;
string name;
}
Run your terminal:
npx jsbuffer schema/main -o src/schema
npx jsbuffer -h
Or
npx jsbuffer --help
So this:
type user {
int id;
string name;
}
Generate a TypeScript interface:
interface user {
id: number;
name: string;
}
This:
type user {
int id;
string name;
}
Generate an encode function:
export function encodeUser(s: ISerializer, value: user) {
s.writeInt32(-399411702);
/**
* encoding param: id
*/
const __pv0 = value['id'];
s.writeInt32(__pv0);
/**
* encoding param: name
*/
const __pv1 = value['name'];
s.writeString(__pv1);
}
And a decode function:
export function decodeUser(__d: IDeserializer): user | null {
const __id = __d.readInt32();
/**
* decode header
*/
if (__id !== -399411702) return null;
let id: number;
let name: string;
/**
* decoding param: id
*/
id = __d.readInt32();
/**
* decoding param: name
*/
name = __d.readString();
return {
_name: 'schema.user',
id,
name
};
}
Functions that are supposed to initialize these interfaces with default data in it:
This:
type user {
int id;
string name;
}
Generate this function:
export function defaultUser(params: Partial<userInputParams> = {}): user {
return user({
id: 0,
name: '',
...params
});
}
This:
type user {
int id;
string name;
}
Generate a comparison function:
export function compareUser(__a: user, __b: user) {
return (
/**
* compare parameter id
*/
__a['id'] === __b['id'] &&
/**
* compare parameter name
*/
__a['name'] === __b['name']
);
}
Generated update functions uses the deep comparison expressions to make sure the reference of the input object is never changed, if there is no change in the changes
argument even if you're using complex objects. To give you an example, let's say you have the following type definition:
type testMap2 {
map<optional<string>,string> a;
map<optional<string>,tuple<string,map<int,int>>> b;
}
The code generator will create an update function with the following signature:
function updateTestMap2(
value: testMap2,
changes: Partial<testMap2InputParams>
): testMap2;
So the following test case would pass without errors:
import assert from 'assert';
import { testMap2, updateTestMap2 } from '../out/schema';
const a1 = testMap2({
a: new Map([
['a', '1'],
['b', '2'],
['c', '3']
]),
b: new Map([['a', ['', new Map([[1, 2]])]]])
});
assert.strict.equal(updateTestMap2(a1, {}), a1);
assert.strict.equal(
updateTestMap2(a1, {
b: new Map([['a', ['', new Map([[1, 2]])]]])
}),
a1
);
assert.strict.notEqual(
updateTestMap2(a1, {
b: new Map([['a', ['', new Map([[1, 3]])]]])
}),
a1
);
assert.strict.deepEqual(
updateTestMap2(a1, {
b: new Map([['a', ['', new Map([[1, 3]])]]])
}),
testMap2({
a: new Map([
['a', '1'],
['b', '2'],
['c', '3']
]),
b: new Map([['a', ['', new Map([[1, 3]])]]])
})
);
trait User {}
type user : User {
ulong id;
}
type userDeleted : User {
ulong deletedAt;
}
Becomes this:
export type User = userDeleted | user;
To me, the most cool part of jsbuffer, is that you can create all sort of complex type structures that involve many parts and we will resolve and generate the code and files accordingly:
import {refUser} from "./Ref";
type comment {
int id;
refUser author;
string comment;
}
type post {
int id;
refUser author;
string title;
string contents;
vector<comment> comments;
}
type user {
int id;
string firstName;
vector<post> posts;
}
set<t>
map<k,v>
vector<t>
tuple<a,b,c,d,e,f,...>
null_terminated_string
string
int
int32
uint32
ulong
long
int16
uint16
int8
uint8