Skip to content

Commit

Permalink
fix: PascalCase identifiers in all cases, bump xtp bindgen, js pdk
Browse files Browse the repository at this point in the history
  • Loading branch information
G4Vi committed Sep 20, 2024
1 parent 6cb8191 commit 50d437f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 25 deletions.
27 changes: 18 additions & 9 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
"author": "Dylibso, Inc.",
"license": "BSD-3-Clause",
"devDependencies": {
"@extism/js-pdk": "^1.0.1",
"@extism/js-pdk": "^1.1.1",
"@types/ejs": "^3.1.5",
"const": "^1.0.0",
"esbuild": "^0.19.6",
"typescript": "^5.3.2"
},
"dependencies": {
"@dylibso/xtp-bindgen": "1.0.0-rc.5",
"@dylibso/xtp-bindgen": "1.0.0-rc.8",
"ejs": "^3.1.10"
}
}
16 changes: 7 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import ejs from "ejs";
import { getContext, helpers, Property, Import, Export, Parameter, Schema } from "@dylibso/xtp-bindgen";

function makePublic(s: string) {
const cap = s.charAt(0).toUpperCase();
if (s.charAt(0) === cap) {
return s;
}
function snakeToPascalCase(s: string) {
return helpers.capitalize(helpers.snakeToCamelCase(s));
}

const pub = cap + s.slice(1);
return pub;
function cppIdentifer(s: string) {
return snakeToPascalCase(s);
}

function objectReferencesObject(existing: any, name: string) {
Expand All @@ -23,7 +21,7 @@ function objectReferencesObject(existing: any, name: string) {
type PropertyLike = Property | Parameter;

function toCppTypeInner(property: PropertyLike, refnamespace: string): string {
if (property.$ref) return refnamespace + property.$ref.name;
if (property.$ref) return refnamespace + cppIdentifer(property.$ref.name);
switch (property.type) {
case "string":
if (property.format === "date-time") {
Expand Down Expand Up @@ -328,7 +326,7 @@ export function render() {
const ctx = {
...helpers,
...prevctx,
makePublic,
cppIdentifer,
enums,
objects,
toCppType,
Expand Down
6 changes: 3 additions & 3 deletions template/pdk.gen.cpp.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ static jsoncons::json toJSONFromByteBuffer(const std::vector<uint8_t> &val) {
<%# encode enums -%>
<% for (const schema of enums) { -%>
JSONCONS_ENUM_TRAITS( <%- 'pdk::'+makePublic(schema.name) -%>,
JSONCONS_ENUM_TRAITS( <%- 'pdk::'+cppIdentifer(schema.name) -%>,
<%- schema.enum.join(', ') -%> )
<% } -%>
<%# encode structs -%>
<% for (const schema of objects) { -%>
<% if (!objectHasBuffer(schema)) { -%>
JSONCONS_ALL_MEMBER_TRAITS( <%- 'pdk::'+makePublic(schema.name) -%>,
JSONCONS_ALL_MEMBER_TRAITS( <%- 'pdk::'+cppIdentifer(schema.name) -%>,
<%- getPropertyNames(schema) -%> )
<% } else {-%>
JSONCONS_ALL_MEMBER_NAME_TRAITS( <%- 'pdk::'+makePublic(schema.name) -%>
JSONCONS_ALL_MEMBER_NAME_TRAITS( <%- 'pdk::'+cppIdentifer(schema.name) -%>
<% for (const prop of schema.properties) { -%>
<% if (!isBuffer(prop)) { -%>
, (<%- prop.name -%>, "<%- prop.name -%>")
Expand Down
4 changes: 2 additions & 2 deletions template/pdk.gen.hpp.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace pdk {
<%# encode enums -%>
<% for (const schema of enums) { %>
// <%- formatCommentLine(schema.description) %>
enum class <%- makePublic(schema.name) %> {
enum class <%- cppIdentifer(schema.name) %> {
<%- schema.enum.join(', ') %>
};
<% } %>

<%# encode structs -%>
<% for (const schema of objects) { %>
// <%- formatCommentLine(schema.description) %>
struct <%- makePublic(schema.name) %> {
struct <%- cppIdentifer(schema.name) %> {
<% for (const prop of schema.properties) { -%>
// <%- formatCommentLine(prop.description) %>
<%-toCppType(prop, '') %> <%- prop.name -%>;
Expand Down

0 comments on commit 50d437f

Please sign in to comment.