Skip to content

Commit 406264a

Browse files
committed
(chore): rename if case mismatch (dont assume snake). Changed test cases
1 parent 9f3d309 commit 406264a

15 files changed

Lines changed: 95 additions & 105 deletions

File tree

packages/schema/bind/src/bindings/rust/functions.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,27 +314,18 @@ export const serdeKeyword: MustacheFn = () => {
314314
};
315315
};
316316

317-
export const serdeRenameIfSnakeCase: MustacheFn = () => {
317+
export const serdeRenameIfCaseMismatch: MustacheFn = () => {
318318
return (value: string, render: (template: string) => string): string => {
319319
const type = render(value);
320320

321-
if (isSnakeCase(type)) {
322-
return `#[serde(rename = "${snakeCaseToCamelCase(type)}")]\n `;
323-
}
324-
325-
if (isKeyword(type)) {
321+
if (hasUppercase(type) || isKeyword(type)) {
326322
return `#[serde(rename = "${type}")]\n `;
327323
}
328324
return "";
329325
};
330326
};
331327

332-
const isSnakeCase = (value: string): boolean =>
333-
/[a-z0-9]+(?:_[a-z0-9]+)*/.test(value) && value.includes("_");
334-
const snakeCaseToCamelCase = (value: string): string =>
335-
value
336-
.toLowerCase()
337-
.replace(/[-_][a-z]/g, (group) => group.slice(-1).toUpperCase());
328+
const hasUppercase = (value: string): boolean => value !== value.toLowerCase();
338329

339330
const toWasmArray = (type: string, optional: boolean): string => {
340331
const result = type.match(/(\[)([[\]A-Za-z1-9_.!]+)(\])/);

packages/schema/bind/src/bindings/rust/plugin/templates/module-rs.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::types::*;
1313
#[derive(Clone, Debug, Deserialize, Serialize)]
1414
pub struct Args{{#toUpper}}{{name}}{{/toUpper}} {
1515
{{#arguments}}
16-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
16+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
1717
{{/arguments}}
1818
}
1919

packages/schema/bind/src/bindings/rust/plugin/templates/types-rs.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use polywrap_plugin::error::PluginError;
2121
#[derive(Clone, Debug, Deserialize, Serialize)]
2222
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
2323
{{#properties}}
24-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
24+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
2525
{{/properties}}
2626
}
2727
{{/envType}}
@@ -33,7 +33,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
3333
#[derive(Clone, Debug, Deserialize, Serialize)]
3434
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
3535
{{#properties}}
36-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
36+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
3737
{{/properties}}
3838
}
3939
{{/objectTypes}}
@@ -45,7 +45,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
4545
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
4646
pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
4747
{{#constants}}
48-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
48+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
4949
{{/constants}}
5050
_MAX_
5151
}
@@ -58,7 +58,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
5858
#[derive(Clone, Debug, Deserialize, Serialize)]
5959
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
6060
{{#properties}}
61-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
61+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
6262
{{/properties}}
6363
}
6464
{{/importedObjectTypes}}
@@ -70,7 +70,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
7070
#[derive(Clone, Debug, Deserialize, Serialize)]
7171
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
7272
{{#properties}}
73-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
73+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
7474
{{/properties}}
7575
}
7676
{{/importedEnvType}}
@@ -82,7 +82,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
8282
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
8383
pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
8484
{{#constants}}
85-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
85+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
8686
{{/constants}}
8787
_MAX_
8888
}
@@ -97,7 +97,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
9797
#[derive(Clone, Debug, Deserialize, Serialize)]
9898
pub struct {{#toUpper}}{{parent.type}}{{/toUpper}}Args{{#toUpper}}{{name}}{{/toUpper}} {
9999
{{#arguments}}
100-
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
100+
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
101101
{{/arguments}}
102102
}
103103

packages/test-cases/cases/bind/sanity/input/schema.graphql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ type CustomType {
109109
optBytes: Bytes
110110
boolean: Boolean!
111111
optBoolean: Boolean
112-
uArray: [UInt!]!
113-
uOptArray: [UInt!]
114-
optUOptArray: [UInt]
112+
u_array: [UInt!]!
113+
uOpt_array: [UInt!]
114+
_opt_uOptArray: [UInt]
115115
optStrOptArray: [String]
116116
uArrayArray: [[UInt!]!]!
117117
uOptArrayOptArray: [[UInt32]]!

packages/test-cases/cases/bind/sanity/output/app-ts/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export interface CustomType {
4545
optBytes?: Types.Bytes | null;
4646
boolean: Types.Boolean;
4747
optBoolean?: Types.Boolean | null;
48-
uArray: Array<Types.UInt>;
49-
uOptArray?: Array<Types.UInt> | null;
50-
optUOptArray?: Array<Types.UInt | null> | null;
48+
u_array: Array<Types.UInt>;
49+
uOpt_array?: Array<Types.UInt> | null;
50+
_opt_uOptArray?: Array<Types.UInt | null> | null;
5151
optStrOptArray?: Array<Types.String | null> | null;
5252
uArrayArray: Array<Array<Types.UInt>>;
5353
uOptArrayOptArray: Array<Array<Types.UInt32 | null> | null>;

packages/test-cases/cases/bind/sanity/output/plugin-py/types.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@
4343
"optBytes": Optional[bytes],
4444
"boolean": bool,
4545
"optBoolean": Optional[bool],
46-
"uArray": list[int],
47-
"uOptArray": Optional[list[int]],
48-
"optUOptArray": Optional[list[Optional[int]]],
46+
"u_array": list[int],
47+
"uOpt_array": Optional[list[int]],
48+
"_opt_uOptArray": Optional[list[Optional[int]]],
4949
"optStrOptArray": Optional[list[Optional[str]]],
5050
"uArrayArray": list[list[int]],
5151
"uOptArrayOptArray": list[Optional[list[Optional[int]]]],

packages/test-cases/cases/bind/sanity/output/plugin-py/wrap_info.py

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,11 @@ pub struct CustomType {
5757
pub boolean: bool,
5858
#[serde(rename = "optBoolean")]
5959
pub opt_boolean: Option<bool>,
60-
#[serde(rename = "uArray")]
6160
pub u_array: Vec<u32>,
62-
#[serde(rename = "uOptArray")]
61+
#[serde(rename = "uOpt_array")]
6362
pub u_opt_array: Option<Vec<u32>>,
64-
#[serde(rename = "optUOptArray")]
65-
pub opt_u_opt_array: Option<Vec<Option<u32>>>,
63+
#[serde(rename = "_opt_uOptArray")]
64+
pub _opt_u_opt_array: Option<Vec<Option<u32>>>,
6665
#[serde(rename = "optStrOptArray")]
6766
pub opt_str_opt_array: Option<Vec<Option<String>>>,
6867
#[serde(rename = "uArrayArray")]

packages/test-cases/cases/bind/sanity/output/plugin-rs/wrap.info.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,66 +1460,66 @@ pub fn get_manifest() -> WrapManifest {
14601460
"array": {
14611461
"item": {
14621462
"kind": 4,
1463-
"name": "uArray",
1463+
"name": "u_array",
14641464
"required": true,
14651465
"type": "UInt"
14661466
},
14671467
"kind": 18,
1468-
"name": "uArray",
1468+
"name": "u_array",
14691469
"required": true,
14701470
"scalar": {
14711471
"kind": 4,
1472-
"name": "uArray",
1472+
"name": "u_array",
14731473
"required": true,
14741474
"type": "UInt"
14751475
},
14761476
"type": "[UInt]"
14771477
},
14781478
"kind": 34,
1479-
"name": "uArray",
1479+
"name": "u_array",
14801480
"required": true,
14811481
"type": "[UInt]"
14821482
},
14831483
{
14841484
"array": {
14851485
"item": {
14861486
"kind": 4,
1487-
"name": "uOptArray",
1487+
"name": "uOpt_array",
14881488
"required": true,
14891489
"type": "UInt"
14901490
},
14911491
"kind": 18,
1492-
"name": "uOptArray",
1492+
"name": "uOpt_array",
14931493
"scalar": {
14941494
"kind": 4,
1495-
"name": "uOptArray",
1495+
"name": "uOpt_array",
14961496
"required": true,
14971497
"type": "UInt"
14981498
},
14991499
"type": "[UInt]"
15001500
},
15011501
"kind": 34,
1502-
"name": "uOptArray",
1502+
"name": "uOpt_array",
15031503
"type": "[UInt]"
15041504
},
15051505
{
15061506
"array": {
15071507
"item": {
15081508
"kind": 4,
1509-
"name": "optUOptArray",
1509+
"name": "_opt_uOptArray",
15101510
"type": "UInt"
15111511
},
15121512
"kind": 18,
1513-
"name": "optUOptArray",
1513+
"name": "_opt_uOptArray",
15141514
"scalar": {
15151515
"kind": 4,
1516-
"name": "optUOptArray",
1516+
"name": "_opt_uOptArray",
15171517
"type": "UInt"
15181518
},
15191519
"type": "[UInt]"
15201520
},
15211521
"kind": 34,
1522-
"name": "optUOptArray",
1522+
"name": "_opt_uOptArray",
15231523
"type": "[UInt]"
15241524
},
15251525
{

packages/test-cases/cases/bind/sanity/output/plugin-ts/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ export interface CustomType {
5858
optBytes?: Types.Bytes | null;
5959
boolean: Types.Boolean;
6060
optBoolean?: Types.Boolean | null;
61-
uArray: Array<Types.UInt>;
62-
uOptArray?: Array<Types.UInt> | null;
63-
optUOptArray?: Array<Types.UInt | null> | null;
61+
u_array: Array<Types.UInt>;
62+
uOpt_array?: Array<Types.UInt> | null;
63+
_opt_uOptArray?: Array<Types.UInt | null> | null;
6464
optStrOptArray?: Array<Types.String | null> | null;
6565
uArrayArray: Array<Array<Types.UInt>>;
6666
uOptArrayOptArray: Array<Array<Types.UInt32 | null> | null>;

0 commit comments

Comments
 (0)