Skip to content

Commit

Permalink
Merge pull request #18 from dylibso/refactor-bindgen
Browse files Browse the repository at this point in the history
Refactor Bindgen to simpler form
  • Loading branch information
bhelx authored Nov 1, 2024
2 parents c5acb3f + 9a1e0ea commit 70905c7
Show file tree
Hide file tree
Showing 12 changed files with 839 additions and 488 deletions.
77 changes: 52 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dylibso/xtp-bindgen",
"version": "1.0.0-rc.11",
"version": "1.0.0-rc.12",
"description": "XTP bindgen helper library",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -15,12 +15,15 @@
"devDependencies": {
"@extism/js-pdk": "^1.0.1",
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.8.1",
"const": "^1.0.0",
"esbuild": "^0.17.0",
"esbuild-plugin-d.ts": "^1.2.3",
"jest": "^29.0.0",
"js-yaml": "^4.1.0",
"ts-jest": "^29.0.0",
"typescript": "^5.0.0"
"typescript": "^5.6.3"
},
"files": [
"dist"
Expand Down
10 changes: 5 additions & 5 deletions src/common.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class ValidationError extends Error {
constructor(public message: string, public path: string) {
super(message);
Object.setPrototypeOf(this, ValidationError.prototype);
}
export class ValidationError {
constructor(public message: string, public path: string) {
this.message = message
this.path = path
}
}
53 changes: 50 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
XtpSchema,
} from "./normalizer";
import { CodeSample } from "./parser";
import { XtpNormalizedType } from "./types";
export * from "./normalizer";
export * from "./types";
export { ValidationError } from "./common";

export function parse(schema: string) {
Expand Down Expand Up @@ -104,9 +106,43 @@ function isPrimitive(p: Property | Parameter): boolean {
return !!p.$ref.enum || !p.$ref.properties;
}

function isDateTime(p: Property | Parameter | null): boolean {
if (!p) return false;
return p.type === "string" && p.format === "date-time";
export type XtpTyped = { xtpType: XtpNormalizedType };

function isDateTime(p: XtpTyped): boolean {
return p?.xtpType?.kind === 'date-time'
}
function isBuffer(p: XtpTyped): boolean {
return p?.xtpType?.kind === "buffer"
}
function isObject(p: XtpTyped): boolean {
return p?.xtpType?.kind === "object"
}
function isArray(p: XtpTyped): boolean {
return p?.xtpType?.kind === "array"
}
function isEnum(p: XtpTyped): boolean {
return p?.xtpType?.kind === "enum"
}
function isString(p: XtpTyped): boolean {
return p?.xtpType?.kind === "string"
}
function isInt32(p: XtpTyped): boolean {
return p?.xtpType?.kind === "int32"
}
function isInt64(p: XtpTyped): boolean {
return p?.xtpType?.kind === "int64"
}
function isFloat(p: XtpTyped): boolean {
return p?.xtpType?.kind === "float"
}
function isDouble(p: XtpTyped): boolean {
return p?.xtpType?.kind === "double"
}
function isBoolean(p: XtpTyped): boolean {
return p?.xtpType?.kind === "boolean"
}
function isMap(p: XtpTyped): boolean {
return p?.xtpType?.kind === "map"
}

function capitalize(s: string) {
Expand Down Expand Up @@ -135,6 +171,17 @@ export const helpers = {
codeSamples,
isDateTime,
isPrimitive,
isBuffer,
isObject,
isEnum,
isArray,
isString,
isInt32,
isInt64,
isFloat,
isDouble,
isMap,
isBoolean,
isJsonEncoded,
isUtf8Encoded,
capitalize,
Expand Down
Loading

0 comments on commit 70905c7

Please sign in to comment.