Skip to content

Commit f129625

Browse files
authored
Merge pull request #1700 from polywrap/namesty/plugin-rs-serde-rename
Serde renaming for snake-cased properties in rust plugins types
2 parents 721e05c + 406264a commit f129625

16 files changed

Lines changed: 158 additions & 90 deletions

File tree

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,19 @@ export const serdeKeyword: MustacheFn = () => {
314314
};
315315
};
316316

317+
export const serdeRenameIfCaseMismatch: MustacheFn = () => {
318+
return (value: string, render: (template: string) => string): string => {
319+
const type = render(value);
320+
321+
if (hasUppercase(type) || isKeyword(type)) {
322+
return `#[serde(rename = "${type}")]\n `;
323+
}
324+
return "";
325+
};
326+
};
327+
328+
const hasUppercase = (value: string): boolean => value !== value.toLowerCase();
329+
317330
const toWasmArray = (type: string, optional: boolean): string => {
318331
const result = type.match(/(\[)([[\]A-Za-z1-9_.!]+)(\])/);
319332

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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}{{#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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}{{#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-
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}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/module.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,45 @@ use super::types::*;
1111
#[derive(Clone, Debug, Deserialize, Serialize)]
1212
pub struct ArgsModuleMethod {
1313
pub str: String,
14+
#[serde(rename = "optStr")]
1415
pub opt_str: Option<String>,
1516
pub en: CustomEnum,
17+
#[serde(rename = "optEnum")]
1618
pub opt_enum: Option<CustomEnum>,
19+
#[serde(rename = "enumArray")]
1720
pub enum_array: Vec<CustomEnum>,
21+
#[serde(rename = "optEnumArray")]
1822
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
1923
pub map: Map<String, i32>,
24+
#[serde(rename = "mapOfArr")]
2025
pub map_of_arr: Map<String, Vec<i32>>,
26+
#[serde(rename = "mapOfMap")]
2127
pub map_of_map: Map<String, Map<String, i32>>,
28+
#[serde(rename = "mapOfObj")]
2229
pub map_of_obj: Map<String, AnotherType>,
30+
#[serde(rename = "mapOfArrOfObj")]
2331
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
2432
}
2533

2634
#[derive(Clone, Debug, Deserialize, Serialize)]
2735
pub struct ArgsObjectMethod {
2836
pub object: AnotherType,
37+
#[serde(rename = "optObject")]
2938
pub opt_object: Option<AnotherType>,
39+
#[serde(rename = "objectArray")]
3040
pub object_array: Vec<AnotherType>,
41+
#[serde(rename = "optObjectArray")]
3142
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
3243
}
3344

3445
#[derive(Clone, Debug, Deserialize, Serialize)]
3546
pub struct ArgsOptionalEnvMethod {
3647
pub object: AnotherType,
48+
#[serde(rename = "optObject")]
3749
pub opt_object: Option<AnotherType>,
50+
#[serde(rename = "objectArray")]
3851
pub object_array: Vec<AnotherType>,
52+
#[serde(rename = "optObjectArray")]
3953
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
4054
}
4155

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use polywrap_plugin::error::PluginError;
1818
#[derive(Clone, Debug, Deserialize, Serialize)]
1919
pub struct Env {
2020
pub prop: String,
21+
#[serde(rename = "optProp")]
2122
pub opt_prop: Option<String>,
23+
#[serde(rename = "optMap")]
2224
pub opt_map: Option<Map<String, Option<i32>>>,
2325
}
2426
// Env END //
@@ -28,8 +30,10 @@ pub struct Env {
2830
#[derive(Clone, Debug, Deserialize, Serialize)]
2931
pub struct CustomType {
3032
pub str: String,
33+
#[serde(rename = "optStr")]
3134
pub opt_str: Option<String>,
3235
pub u: u32,
36+
#[serde(rename = "optU")]
3337
pub opt_u: Option<u32>,
3438
pub u8: u8,
3539
pub u16: u16,
@@ -39,35 +43,57 @@ pub struct CustomType {
3943
pub i16: i16,
4044
pub i32: i32,
4145
pub bigint: BigInt,
46+
#[serde(rename = "optBigint")]
4247
pub opt_bigint: Option<BigInt>,
4348
pub bignumber: BigNumber,
49+
#[serde(rename = "optBignumber")]
4450
pub opt_bignumber: Option<BigNumber>,
4551
pub json: JSON::Value,
52+
#[serde(rename = "optJson")]
4653
pub opt_json: Option<JSON::Value>,
4754
pub bytes: Vec<u8>,
55+
#[serde(rename = "optBytes")]
4856
pub opt_bytes: Option<Vec<u8>>,
4957
pub boolean: bool,
58+
#[serde(rename = "optBoolean")]
5059
pub opt_boolean: Option<bool>,
5160
pub u_array: Vec<u32>,
61+
#[serde(rename = "uOpt_array")]
5262
pub u_opt_array: Option<Vec<u32>>,
53-
pub opt_u_opt_array: Option<Vec<Option<u32>>>,
63+
#[serde(rename = "_opt_uOptArray")]
64+
pub _opt_u_opt_array: Option<Vec<Option<u32>>>,
65+
#[serde(rename = "optStrOptArray")]
5466
pub opt_str_opt_array: Option<Vec<Option<String>>>,
67+
#[serde(rename = "uArrayArray")]
5568
pub u_array_array: Vec<Vec<u32>>,
69+
#[serde(rename = "uOptArrayOptArray")]
5670
pub u_opt_array_opt_array: Vec<Option<Vec<Option<u32>>>>,
71+
#[serde(rename = "uArrayOptArrayArray")]
5772
pub u_array_opt_array_array: Vec<Option<Vec<Vec<u32>>>>,
73+
#[serde(rename = "crazyArray")]
5874
pub crazy_array: Option<Vec<Option<Vec<Vec<Option<Vec<u32>>>>>>>,
5975
pub object: AnotherType,
76+
#[serde(rename = "optObject")]
6077
pub opt_object: Option<AnotherType>,
78+
#[serde(rename = "objectArray")]
6179
pub object_array: Vec<AnotherType>,
80+
#[serde(rename = "optObjectArray")]
6281
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
6382
pub en: CustomEnum,
83+
#[serde(rename = "optEnum")]
6484
pub opt_enum: Option<CustomEnum>,
85+
#[serde(rename = "enumArray")]
6586
pub enum_array: Vec<CustomEnum>,
87+
#[serde(rename = "optEnumArray")]
6688
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
6789
pub map: Map<String, i32>,
90+
#[serde(rename = "mapOfArr")]
6891
pub map_of_arr: Map<String, Vec<i32>>,
92+
#[serde(rename = "mapOfObj")]
6993
pub map_of_obj: Map<String, AnotherType>,
94+
#[serde(rename = "mapOfArrOfObj")]
7095
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
96+
#[serde(rename = "mapCustomValue")]
7197
pub map_custom_value: Map<String, Option<CustomMapValue>>,
7298
}
7399
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -109,12 +135,18 @@ pub enum While {
109135
#[derive(Clone, Debug, Deserialize, Serialize)]
110136
pub struct TestImportObject {
111137
pub object: TestImportAnotherObject,
138+
#[serde(rename = "optObject")]
112139
pub opt_object: Option<TestImportAnotherObject>,
140+
#[serde(rename = "objectArray")]
113141
pub object_array: Vec<TestImportAnotherObject>,
142+
#[serde(rename = "optObjectArray")]
114143
pub opt_object_array: Option<Vec<Option<TestImportAnotherObject>>>,
115144
pub en: TestImportEnum,
145+
#[serde(rename = "optEnum")]
116146
pub opt_enum: Option<TestImportEnum>,
147+
#[serde(rename = "enumArray")]
117148
pub enum_array: Vec<TestImportEnum>,
149+
#[serde(rename = "optEnumArray")]
118150
pub opt_enum_array: Option<Vec<Option<TestImportEnum>>>,
119151
}
120152
#[derive(Clone, Debug, Deserialize, Serialize)]
@@ -149,17 +181,26 @@ pub enum TestImportEnumReturn {
149181
#[derive(Clone, Debug, Deserialize, Serialize)]
150182
pub struct TestImportModuleArgsImportedMethod {
151183
pub str: String,
184+
#[serde(rename = "optStr")]
152185
pub opt_str: Option<String>,
153186
pub u: u32,
187+
#[serde(rename = "optU")]
154188
pub opt_u: Option<u32>,
189+
#[serde(rename = "uArrayArray")]
155190
pub u_array_array: Vec<Option<Vec<Option<u32>>>>,
156191
pub object: TestImportObject,
192+
#[serde(rename = "optObject")]
157193
pub opt_object: Option<TestImportObject>,
194+
#[serde(rename = "objectArray")]
158195
pub object_array: Vec<TestImportObject>,
196+
#[serde(rename = "optObjectArray")]
159197
pub opt_object_array: Option<Vec<Option<TestImportObject>>>,
160198
pub en: TestImportEnum,
199+
#[serde(rename = "optEnum")]
161200
pub opt_enum: Option<TestImportEnum>,
201+
#[serde(rename = "enumArray")]
162202
pub enum_array: Vec<TestImportEnum>,
203+
#[serde(rename = "optEnumArray")]
163204
pub opt_enum_array: Option<Vec<Option<TestImportEnum>>>,
164205
}
165206

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
{

0 commit comments

Comments
 (0)