Skip to content

Commit

Permalink
(chore): rename if case mismatch (dont assume snake). Changed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
namesty committed Apr 18, 2023
1 parent 9f3d309 commit 406264a
Show file tree
Hide file tree
Showing 15 changed files with 95 additions and 105 deletions.
15 changes: 3 additions & 12 deletions packages/schema/bind/src/bindings/rust/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,18 @@ export const serdeKeyword: MustacheFn = () => {
};
};

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

if (isSnakeCase(type)) {
return `#[serde(rename = "${snakeCaseToCamelCase(type)}")]\n `;
}

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

const isSnakeCase = (value: string): boolean =>
/[a-z0-9]+(?:_[a-z0-9]+)*/.test(value) && value.includes("_");
const snakeCaseToCamelCase = (value: string): string =>
value
.toLowerCase()
.replace(/[-_][a-z]/g, (group) => group.slice(-1).toUpperCase());
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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}{{#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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}{{#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}}
{{#serdeRenameIfSnakeCase}}{{#toLower}}{{name}}{{/toLower}}{{/serdeRenameIfSnakeCase}}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.

Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ pub struct CustomType {
pub boolean: bool,
#[serde(rename = "optBoolean")]
pub opt_boolean: Option<bool>,
#[serde(rename = "uArray")]
pub u_array: Vec<u32>,
#[serde(rename = "uOptArray")]
#[serde(rename = "uOpt_array")]
pub u_opt_array: Option<Vec<u32>>,
#[serde(rename = "optUOptArray")]
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")]
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
24 changes: 12 additions & 12 deletions packages/test-cases/cases/bind/sanity/output/plugin-ts/wrap.info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1458,66 +1458,66 @@ export const 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 @@ -36,9 +36,9 @@ export class CustomType {
optBytes: ArrayBuffer | null;
_boolean: bool;
optBoolean: Box<bool> | null;
uArray: Array<u32>;
uOptArray: Array<u32> | null;
optUOptArray: Array<Box<u32> | null> | null;
u_array: Array<u32>;
uOpt_array: Array<u32> | null;
_opt_uOptArray: Array<Box<u32> | null> | null;
optStrOptArray: Array<string | null> | null;
uArrayArray: Array<Array<u32>>;
uOptArrayOptArray: Array<Array<Box<u32> | null> | null>;
Expand Down
Loading

0 comments on commit 406264a

Please sign in to comment.