Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rename nullable to optional #937 #960

Merged
merged 1 commit into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const toMsgPack: MustacheFn = () => {
if (type[type.length - 1] === "!") {
type = type.substring(0, type.length - 1);
} else {
modifier = "Nullable";
modifier = "Optional";
}

if (type[0] === "[") {
Expand Down Expand Up @@ -50,12 +50,12 @@ export const toWasmInit: MustacheFn = () => {
type = type.substring(0, type.length - 1);
} else {
const nullType = toWasm()(value, render);
const nullable = "Option";
const optional = "Option";
const nullOptional = "| null";

if (nullType.endsWith(nullOptional)) {
return "null";
} else if (nullType.startsWith(nullable)) {
} else if (nullType.startsWith(optional)) {
type = nullType.substring(6);
return `Option.None${type}()`;
}
Expand Down Expand Up @@ -112,19 +112,19 @@ export const toWasm: MustacheFn = () => {
let type = render(value);
let isEnum = false;

let nullable = false;
let optional = false;
if (type[type.length - 1] === "!") {
type = type.substring(0, type.length - 1);
} else {
nullable = true;
optional = true;
}

if (type[0] === "[") {
return toWasmArray(type, nullable);
return toWasmArray(type, optional);
}

if (type.startsWith("Map<")) {
return toWasmMap(type, nullable);
return toWasmMap(type, optional);
}

switch (type) {
Expand Down Expand Up @@ -177,22 +177,22 @@ export const toWasm: MustacheFn = () => {
}
}

return applyNullable(type, nullable, isEnum);
return applyOptional(type, optional, isEnum);
};
};

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

if (!result || result.length !== 4) {
throw Error(`Invalid Array: ${type}`);
}

const wasmType = toWasm()(result[2], (str) => str);
return applyNullable("Array<" + wasmType + ">", nullable, false);
return applyOptional("Array<" + wasmType + ">", optional, false);
};

const toWasmMap = (type: string, nullable: boolean): string => {
const toWasmMap = (type: string, optional: boolean): string => {
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");

Expand All @@ -212,15 +212,15 @@ const toWasmMap = (type: string, nullable: boolean): string => {
const keyType = toWasm()(keyValTypes[0], (str) => str);
const valType = toWasm()(keyValTypes[1], (str) => str);

return applyNullable(`Map<${keyType}, ${valType}>`, nullable, false);
return applyOptional(`Map<${keyType}, ${valType}>`, optional, false);
};

const applyNullable = (
const applyOptional = (
type: string,
nullable: boolean,
optional: boolean,
isEnum: boolean
): string => {
if (nullable) {
if (optional) {
if (
type.indexOf("Array") === 0 ||
type.indexOf("string") === 0 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function write{{name}}Args(
writer.writeInt32(input.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{^required}}
writer.writeNullableInt32(input.{{#handleKeywords}}{{name}}{{/handleKeywords}});
writer.writeOptionalInt32(input.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{/enum}}
{{#object}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function write{{type}}(writer: Write, type: {{type}}): void {
writer.writeInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{^required}}
writer.writeNullableInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
writer.writeOptionalInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{/enum}}
writer.context().pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export function write{{name}}Result(writer: Write, result: {{#return}}{{#toWasm}
writer.writeInt32(result);
{{/required}}
{{^required}}
writer.writeNullableInt32(result);
writer.writeOptionalInt32(result);
{{/required}}
{{/enum}}
{{#object}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function write{{type}}(writer: Write, type: {{type}}): void {
writer.writeInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{^required}}
writer.writeNullableInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
writer.writeOptionalInt32(type.{{#handleKeywords}}{{name}}{{/handleKeywords}});
{{/required}}
{{/enum}}
writer.context().pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
writer.writeInt32(item);
{{/required}}
{{^required}}
writer.writeNullableInt32(item);
writer.writeOptionalInt32(item);
{{/required}}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ writer.write{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}(value, (writer: Write,
writer.writeInt32(value);
{{/required}}
{{^required}}
writer.writeNullableInt32(value);
writer.writeOptionalInt32(value);
{{/required}}
{{/enum}}
{{#object}}
Expand Down
54 changes: 27 additions & 27 deletions packages/schema/bind/src/bindings/rust/wasm-rs/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const toMsgPack: MustacheFunction = () => {
if (type[type.length - 1] === "!") {
type = type.substr(0, type.length - 1);
} else {
modifier = "nullable_";
modifier = "optional_";
}

if (type[0] === "[") {
Expand Down Expand Up @@ -141,20 +141,20 @@ export const toMsgPack: MustacheFunction = () => {
export const toWasmInit: MustacheFunction = () => {
return (value: string, render: (template: string) => string) => {
let type = render(value);
let nullable = false;
let optional = false;

const nullableModifier = (str: string): string => {
return !nullable ? str : "None";
const optionalModifier = (str: string): string => {
return !optional ? str : "None";
};

if (type[type.length - 1] === "!") {
type = type.substr(0, type.length - 1);
} else {
nullable = true;
optional = true;
}

if (type[0] === "[") {
return nullableModifier("vec![]");
return optionalModifier("vec![]");
}

if (type.startsWith("Map<")) {
Expand All @@ -178,24 +178,24 @@ export const toWasmInit: MustacheFunction = () => {
case "UInt16":
case "UInt32":
case "UInt64":
return nullableModifier("0");
return optionalModifier("0");
case "String":
return nullableModifier("String::new()");
return optionalModifier("String::new()");
case "Boolean":
return nullableModifier("false");
return optionalModifier("false");
case "Bytes":
return nullableModifier("vec![]");
return optionalModifier("vec![]");
case "BigInt":
return nullableModifier("BigInt::default()");
return optionalModifier("BigInt::default()");
case "BigNumber":
return nullableModifier("BigNumber::default()");
return optionalModifier("BigNumber::default()");
case "JSON":
return nullableModifier("JSON::Value::Null");
return optionalModifier("JSON::Value::Null");
default:
if (type.includes("Enum_")) {
return nullableModifier(`${toWasm()(value, render)}::_MAX_`);
return optionalModifier(`${toWasm()(value, render)}::_MAX_`);
} else {
return nullableModifier(`${toWasm()(value, render)}::new()`);
return optionalModifier(`${toWasm()(value, render)}::new()`);
}
}
};
Expand All @@ -206,19 +206,19 @@ export const toWasm: MustacheFunction = () => {
let type = render(value);
let objectType = false;

let nullable = false;
let optional = false;
if (type[type.length - 1] === "!") {
type = type.substr(0, type.length - 1);
} else {
nullable = true;
optional = true;
}

if (type[0] === "[") {
return toWasmArray(type, nullable);
return toWasmArray(type, optional);
}

if (type.startsWith("Map<")) {
return toWasmMap(type, nullable);
return toWasmMap(type, optional);
}

switch (type) {
Expand Down Expand Up @@ -278,8 +278,8 @@ export const toWasm: MustacheFunction = () => {
}

return objectType
? applyNullable(type, nullable)
: applyNullable(type, nullable);
? applyOptional(type, optional)
: applyOptional(type, optional);
};
};

Expand All @@ -295,18 +295,18 @@ export const detectKeyword: MustacheFunction = () => {
};
};

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

if (!result || result.length !== 4) {
throw Error(`Invalid Array: ${type}`);
}

const wasmType = toWasm()(result[2], (str) => str);
return applyNullable("Vec<" + wasmType + ">", nullable);
return applyOptional("Vec<" + wasmType + ">", optional);
};

const toWasmMap = (type: string, nullable: boolean): string => {
const toWasmMap = (type: string, optional: boolean): string => {
const firstOpenBracketIdx = type.indexOf("<");
const lastCloseBracketIdx = type.lastIndexOf(">");

Expand All @@ -326,11 +326,11 @@ const toWasmMap = (type: string, nullable: boolean): string => {
const keyType = toWasm()(keyValTypes[0], (str) => str);
const valType = toWasm()(keyValTypes[1], (str) => str);

return applyNullable(`Map<${keyType}, ${valType}>`, nullable);
return applyOptional(`Map<${keyType}, ${valType}>`, optional);
};

const applyNullable = (type: string, nullable: boolean): string => {
if (nullable) {
const applyOptional = (type: string, optional: boolean): string => {
if (optional) {
return `Option<${type}>`;
} else {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn write_{{#toLower}}{{name}}{{/toLower}}_args<W: Write>(input: &Input{{#toU
writer.write_i32(&(input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}} as i32))?;
{{/required}}
{{^required}}
writer.write_nullable_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
writer.write_optional_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
{{/required}}
{{/enum}}
{{#object}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn write_{{#toLower}}{{type}}{{/toLower}}<W: Write>(input: &{{#toUpper}}{{ty
writer.write_i32(&(input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}} as i32))?;
{{/required}}
{{^required}}
writer.write_nullable_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
writer.write_optional_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
{{/required}}
{{/enum}}
writer.context().pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn write_{{#toLower}}{{name}}{{/toLower}}_result<W: Write>(result: {{#return
writer.write_i32(&(*result as i32))?;
{{/required}}
{{^required}}
writer.write_nullable_i32(&result.map(|f| f as i32))?;
writer.write_optional_i32(&result.map(|f| f as i32))?;
{{/required}}
{{/enum}}
{{#object}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn write_{{#toLower}}{{type}}{{/toLower}}<W: Write>(input: &{{#toUpper}}{{ty
writer.write_i32(&(input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}} as i32))?;
{{/required}}
{{^required}}
writer.write_nullable_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
writer.write_optional_i32(&input.{{#detectKeyword}}{{#toLower}}{{name}}{{/toLower}}{{/detectKeyword}}.map(|f| f as i32))?;
{{/required}}
{{/enum}}
writer.context().pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
writer.write_i32(&(*item as i32))
{{/required}}
{{^required}}
writer.write_nullable_i32(&item.map(|f| f as i32))
writer.write_optional_i32(&item.map(|f| f as i32))
{{/required}}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ writer.write_{{#toLower}}{{#toMsgPack}}{{toGraphQLType}}{{/toMsgPack}}{{/toLower
writer.write_i32(&(*value as i32))
{{/required}}
{{^required}}
writer.write_nullable_i32(&value.map(|f| f as i32))
writer.write_optional_i32(&value.map(|f| f as i32))
{{/required}}
{{/enum}}
{{#object}}
Expand Down
Loading