Skip to content

Commit

Permalink
Merge pull request #1700 from polywrap/namesty/plugin-rs-serde-rename
Browse files Browse the repository at this point in the history
Serde renaming for snake-cased properties in rust plugins types
  • Loading branch information
dOrgJelli authored Apr 19, 2023
2 parents 721e05c + 406264a commit f129625
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 90 deletions.
13 changes: 13 additions & 0 deletions packages/schema/bind/src/bindings/rust/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ export const serdeKeyword: MustacheFn = () => {
};
};

export const serdeRenameIfCaseMismatch: MustacheFn = () => {
return (value: string, render: (template: string) => string): string => {
const type = render(value);

if (hasUppercase(type) || isKeyword(type)) {
return `#[serde(rename = "${type}")]\n `;
}
return "";
};
};

const hasUppercase = (value: string): boolean => value !== value.toLowerCase();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::types::*;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Args{{#toUpper}}{{name}}{{/toUpper}} {
{{#arguments}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/arguments}}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use polywrap_plugin::error::PluginError;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#properties}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/properties}}
}
{{/envType}}
Expand All @@ -33,7 +33,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#properties}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/properties}}
}
{{/objectTypes}}
Expand All @@ -45,7 +45,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#constants}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
{{/constants}}
_MAX_
}
Expand All @@ -58,7 +58,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#properties}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/properties}}
}
{{/importedObjectTypes}}
Expand All @@ -70,7 +70,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#properties}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/properties}}
}
{{/importedEnvType}}
Expand All @@ -82,7 +82,7 @@ pub struct {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}}
#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
{{#constants}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}{{#detectKeyword}}{{.}}{{/detectKeyword}},
{{/constants}}
_MAX_
}
Expand All @@ -97,7 +97,7 @@ pub enum {{#detectKeyword}}{{#toUpper}}{{type}}{{/toUpper}}{{/detectKeyword}} {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct {{#toUpper}}{{parent.type}}{{/toUpper}}Args{{#toUpper}}{{name}}{{/toUpper}} {
{{#arguments}}
{{#serdeKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/serdeKeyword}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{#serdeRenameIfCaseMismatch}}{{name}}{{/serdeRenameIfCaseMismatch}}pub {{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}: {{#toWasm}}{{toGraphQLType}}{{/toWasm}},
{{/arguments}}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/test-cases/cases/bind/sanity/input/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ type CustomType {
optBytes: Bytes
boolean: Boolean!
optBoolean: Boolean
uArray: [UInt!]!
uOptArray: [UInt!]
optUOptArray: [UInt]
u_array: [UInt!]!
uOpt_array: [UInt!]
_opt_uOptArray: [UInt]
optStrOptArray: [String]
uArrayArray: [[UInt!]!]!
uOptArrayOptArray: [[UInt32]]!
Expand Down
6 changes: 3 additions & 3 deletions packages/test-cases/cases/bind/sanity/output/app-ts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export interface CustomType {
optBytes?: Types.Bytes | null;
boolean: Types.Boolean;
optBoolean?: Types.Boolean | null;
uArray: Array<Types.UInt>;
uOptArray?: Array<Types.UInt> | null;
optUOptArray?: Array<Types.UInt | null> | null;
u_array: Array<Types.UInt>;
uOpt_array?: Array<Types.UInt> | null;
_opt_uOptArray?: Array<Types.UInt | null> | null;
optStrOptArray?: Array<Types.String | null> | null;
uArrayArray: Array<Array<Types.UInt>>;
uOptArrayOptArray: Array<Array<Types.UInt32 | null> | null>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
"optBytes": Optional[bytes],
"boolean": bool,
"optBoolean": Optional[bool],
"uArray": list[int],
"uOptArray": Optional[list[int]],
"optUOptArray": Optional[list[Optional[int]]],
"u_array": list[int],
"uOpt_array": Optional[list[int]],
"_opt_uOptArray": Optional[list[Optional[int]]],
"optStrOptArray": Optional[list[Optional[str]]],
"uArrayArray": list[list[int]],
"uOptArrayOptArray": list[Optional[list[Optional[int]]]],
Expand Down

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions packages/test-cases/cases/bind/sanity/output/plugin-rs/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,45 @@ use super::types::*;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsModuleMethod {
pub str: String,
#[serde(rename = "optStr")]
pub opt_str: Option<String>,
pub en: CustomEnum,
#[serde(rename = "optEnum")]
pub opt_enum: Option<CustomEnum>,
#[serde(rename = "enumArray")]
pub enum_array: Vec<CustomEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
pub map: Map<String, i32>,
#[serde(rename = "mapOfArr")]
pub map_of_arr: Map<String, Vec<i32>>,
#[serde(rename = "mapOfMap")]
pub map_of_map: Map<String, Map<String, i32>>,
#[serde(rename = "mapOfObj")]
pub map_of_obj: Map<String, AnotherType>,
#[serde(rename = "mapOfArrOfObj")]
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsObjectMethod {
pub object: AnotherType,
#[serde(rename = "optObject")]
pub opt_object: Option<AnotherType>,
#[serde(rename = "objectArray")]
pub object_array: Vec<AnotherType>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ArgsOptionalEnvMethod {
pub object: AnotherType,
#[serde(rename = "optObject")]
pub opt_object: Option<AnotherType>,
#[serde(rename = "objectArray")]
pub object_array: Vec<AnotherType>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
}

Expand Down
43 changes: 42 additions & 1 deletion packages/test-cases/cases/bind/sanity/output/plugin-rs/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ use polywrap_plugin::error::PluginError;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Env {
pub prop: String,
#[serde(rename = "optProp")]
pub opt_prop: Option<String>,
#[serde(rename = "optMap")]
pub opt_map: Option<Map<String, Option<i32>>>,
}
// Env END //
Expand All @@ -28,8 +30,10 @@ pub struct Env {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct CustomType {
pub str: String,
#[serde(rename = "optStr")]
pub opt_str: Option<String>,
pub u: u32,
#[serde(rename = "optU")]
pub opt_u: Option<u32>,
pub u8: u8,
pub u16: u16,
Expand All @@ -39,35 +43,57 @@ pub struct CustomType {
pub i16: i16,
pub i32: i32,
pub bigint: BigInt,
#[serde(rename = "optBigint")]
pub opt_bigint: Option<BigInt>,
pub bignumber: BigNumber,
#[serde(rename = "optBignumber")]
pub opt_bignumber: Option<BigNumber>,
pub json: JSON::Value,
#[serde(rename = "optJson")]
pub opt_json: Option<JSON::Value>,
pub bytes: Vec<u8>,
#[serde(rename = "optBytes")]
pub opt_bytes: Option<Vec<u8>>,
pub boolean: bool,
#[serde(rename = "optBoolean")]
pub opt_boolean: Option<bool>,
pub u_array: Vec<u32>,
#[serde(rename = "uOpt_array")]
pub u_opt_array: Option<Vec<u32>>,
pub opt_u_opt_array: Option<Vec<Option<u32>>>,
#[serde(rename = "_opt_uOptArray")]
pub _opt_u_opt_array: Option<Vec<Option<u32>>>,
#[serde(rename = "optStrOptArray")]
pub opt_str_opt_array: Option<Vec<Option<String>>>,
#[serde(rename = "uArrayArray")]
pub u_array_array: Vec<Vec<u32>>,
#[serde(rename = "uOptArrayOptArray")]
pub u_opt_array_opt_array: Vec<Option<Vec<Option<u32>>>>,
#[serde(rename = "uArrayOptArrayArray")]
pub u_array_opt_array_array: Vec<Option<Vec<Vec<u32>>>>,
#[serde(rename = "crazyArray")]
pub crazy_array: Option<Vec<Option<Vec<Vec<Option<Vec<u32>>>>>>>,
pub object: AnotherType,
#[serde(rename = "optObject")]
pub opt_object: Option<AnotherType>,
#[serde(rename = "objectArray")]
pub object_array: Vec<AnotherType>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<AnotherType>>>,
pub en: CustomEnum,
#[serde(rename = "optEnum")]
pub opt_enum: Option<CustomEnum>,
#[serde(rename = "enumArray")]
pub enum_array: Vec<CustomEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<CustomEnum>>>,
pub map: Map<String, i32>,
#[serde(rename = "mapOfArr")]
pub map_of_arr: Map<String, Vec<i32>>,
#[serde(rename = "mapOfObj")]
pub map_of_obj: Map<String, AnotherType>,
#[serde(rename = "mapOfArrOfObj")]
pub map_of_arr_of_obj: Map<String, Vec<AnotherType>>,
#[serde(rename = "mapCustomValue")]
pub map_custom_value: Map<String, Option<CustomMapValue>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -109,12 +135,18 @@ pub enum While {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportObject {
pub object: TestImportAnotherObject,
#[serde(rename = "optObject")]
pub opt_object: Option<TestImportAnotherObject>,
#[serde(rename = "objectArray")]
pub object_array: Vec<TestImportAnotherObject>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<TestImportAnotherObject>>>,
pub en: TestImportEnum,
#[serde(rename = "optEnum")]
pub opt_enum: Option<TestImportEnum>,
#[serde(rename = "enumArray")]
pub enum_array: Vec<TestImportEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<TestImportEnum>>>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -149,17 +181,26 @@ pub enum TestImportEnumReturn {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct TestImportModuleArgsImportedMethod {
pub str: String,
#[serde(rename = "optStr")]
pub opt_str: Option<String>,
pub u: u32,
#[serde(rename = "optU")]
pub opt_u: Option<u32>,
#[serde(rename = "uArrayArray")]
pub u_array_array: Vec<Option<Vec<Option<u32>>>>,
pub object: TestImportObject,
#[serde(rename = "optObject")]
pub opt_object: Option<TestImportObject>,
#[serde(rename = "objectArray")]
pub object_array: Vec<TestImportObject>,
#[serde(rename = "optObjectArray")]
pub opt_object_array: Option<Vec<Option<TestImportObject>>>,
pub en: TestImportEnum,
#[serde(rename = "optEnum")]
pub opt_enum: Option<TestImportEnum>,
#[serde(rename = "enumArray")]
pub enum_array: Vec<TestImportEnum>,
#[serde(rename = "optEnumArray")]
pub opt_enum_array: Option<Vec<Option<TestImportEnum>>>,
}

Expand Down
24 changes: 12 additions & 12 deletions packages/test-cases/cases/bind/sanity/output/plugin-rs/wrap.info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1460,66 +1460,66 @@ pub fn get_manifest() -> WrapManifest {
"array": {
"item": {
"kind": 4,
"name": "uArray",
"name": "u_array",
"required": true,
"type": "UInt"
},
"kind": 18,
"name": "uArray",
"name": "u_array",
"required": true,
"scalar": {
"kind": 4,
"name": "uArray",
"name": "u_array",
"required": true,
"type": "UInt"
},
"type": "[UInt]"
},
"kind": 34,
"name": "uArray",
"name": "u_array",
"required": true,
"type": "[UInt]"
},
{
"array": {
"item": {
"kind": 4,
"name": "uOptArray",
"name": "uOpt_array",
"required": true,
"type": "UInt"
},
"kind": 18,
"name": "uOptArray",
"name": "uOpt_array",
"scalar": {
"kind": 4,
"name": "uOptArray",
"name": "uOpt_array",
"required": true,
"type": "UInt"
},
"type": "[UInt]"
},
"kind": 34,
"name": "uOptArray",
"name": "uOpt_array",
"type": "[UInt]"
},
{
"array": {
"item": {
"kind": 4,
"name": "optUOptArray",
"name": "_opt_uOptArray",
"type": "UInt"
},
"kind": 18,
"name": "optUOptArray",
"name": "_opt_uOptArray",
"scalar": {
"kind": 4,
"name": "optUOptArray",
"name": "_opt_uOptArray",
"type": "UInt"
},
"type": "[UInt]"
},
"kind": 34,
"name": "optUOptArray",
"name": "_opt_uOptArray",
"type": "[UInt]"
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export interface CustomType {
optBytes?: Types.Bytes | null;
boolean: Types.Boolean;
optBoolean?: Types.Boolean | null;
uArray: Array<Types.UInt>;
uOptArray?: Array<Types.UInt> | null;
optUOptArray?: Array<Types.UInt | null> | null;
u_array: Array<Types.UInt>;
uOpt_array?: Array<Types.UInt> | null;
_opt_uOptArray?: Array<Types.UInt | null> | null;
optStrOptArray?: Array<Types.String | null> | null;
uArrayArray: Array<Array<Types.UInt>>;
uOptArrayOptArray: Array<Array<Types.UInt32 | null> | null>;
Expand Down
Loading

0 comments on commit f129625

Please sign in to comment.