Generate type validation functions (#254)
This commit is contained in:
1 parent
7eb20dfe2c
commit
575935f5ac
4 files changed
+75
-53
No files matched your search
@@ -70,7 +70,8 @@
|
|||||||
"../tools/bs-plugins/json-yaml-plugin.ts",
|
"../tools/bs-plugins/json-yaml-plugin.ts",
|
||||||
"../tools/bs-plugins/validation-plugin.ts",
|
"../tools/bs-plugins/validation-plugin.ts",
|
||||||
"../tools/bs-plugins/logger-plugin.ts",
|
"../tools/bs-plugins/logger-plugin.ts",
|
||||||
"../tools/bs-plugins/image-gen-plugin.ts"
|
"../tools/bs-plugins/image-gen-plugin.ts",
|
||||||
|
"../tools/bs-plugins/type-gen-plugin.ts"
|
||||||
],
|
],
|
||||||
"retainStagingDir": true,
|
"retainStagingDir": true,
|
||||||
"require": [
|
"require": [
|
||||||
|
|||||||
@@ -26,7 +26,8 @@
|
|||||||
"../tools/bs-plugins/json-yaml-plugin.ts",
|
"../tools/bs-plugins/json-yaml-plugin.ts",
|
||||||
"../tools/bs-plugins/validation-plugin.ts",
|
"../tools/bs-plugins/validation-plugin.ts",
|
||||||
"../tools/bs-plugins/logger-plugin.ts",
|
"../tools/bs-plugins/logger-plugin.ts",
|
||||||
"../tools/bs-plugins/image-gen-plugin.ts"
|
"../tools/bs-plugins/image-gen-plugin.ts",
|
||||||
|
"../tools/bs-plugins/type-gen-plugin.ts"
|
||||||
],
|
],
|
||||||
"rooibos": {
|
"rooibos": {
|
||||||
"logLevel": 2,
|
"logLevel": 2,
|
||||||
|
|||||||
@@ -1,54 +1,4 @@
|
|||||||
function IsString(obj as dynamic) as boolean
|
' This file will be populated with the functions using type-gen-plugin.ts
|
||||||
return obj <> invalid and GetInterface(obj, "ifString") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsArray(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifArray") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsAssociativeArray(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifAssociativeArray") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsByteArray(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifByteArray") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsInt(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifInt") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsDouble(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifDouble") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function IsFunction(obj as dynamic) as boolean
|
|
||||||
return obj <> invalid and GetInterface(obj, "ifFunction") <> invalid
|
|
||||||
end function
|
|
||||||
|
|
||||||
function ValidString(obj as object) as string
|
|
||||||
if obj <> invalid and GetInterface(obj, "ifString") <> invalid
|
|
||||||
return obj
|
|
||||||
else
|
|
||||||
return ""
|
|
||||||
end if
|
|
||||||
end function
|
|
||||||
|
|
||||||
function ValidInt(obj as dynamic) as integer
|
|
||||||
if obj <> invalid and GetInterface(obj, "ifInt") <> invalid
|
|
||||||
return obj
|
|
||||||
else
|
|
||||||
return 0
|
|
||||||
end if
|
|
||||||
end function
|
|
||||||
|
|
||||||
function ValidBool(obj as dynamic) as boolean
|
|
||||||
if obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
|
||||||
return obj
|
|
||||||
else
|
|
||||||
return false
|
|
||||||
end if
|
|
||||||
end function
|
|
||||||
|
|
||||||
function FuncName(func as function) as string
|
function FuncName(func as function) as string
|
||||||
return (LCase(`${func}`)).replace("function:", "").trim()
|
return (LCase(`${func}`)).replace("function:", "").trim()
|
||||||
|
|||||||
@@ -0,0 +1,70 @@
|
|||||||
|
import { CompilerPlugin, Program, SourceObj } from "brighterscript";
|
||||||
|
|
||||||
|
const types = [
|
||||||
|
{ name: 'Bool', interface: 'ifBoolean', type: 'boolean', defaultValue: 'false' },
|
||||||
|
{ name: 'Int', interface: 'ifInt', type: 'integer', defaultValue: '0' },
|
||||||
|
{ name: 'Double', interface: 'ifDouble', type: 'double', defaultValue: '0#' },
|
||||||
|
{ name: 'String', interface: 'ifString', type: 'string', defaultValue: '""' },
|
||||||
|
{ name: 'Array', interface: 'ifArray', type: 'object', defaultValue: '[]' },
|
||||||
|
{ name: 'AssociativeArray', interface: 'ifAssociativeArray', type: 'object', defaultValue: '{}' },
|
||||||
|
{ name: 'ByteArray', interface: 'ifByteArray', type: null, defaultValue: null },
|
||||||
|
{ name: 'Function', interface: 'ifFunction', type: null, defaultValue: null },
|
||||||
|
];
|
||||||
|
|
||||||
|
const generatedCodeHeader = `' The rest of this file is generated by the TypeGenPlugin`
|
||||||
|
|
||||||
|
const typesFilePath = 'source/utils/Types.bs';
|
||||||
|
|
||||||
|
const isTypeTemplate = (name: string, _interface: string) => {
|
||||||
|
return `' Returns true if the given object is of type ${name}, false otherwise
|
||||||
|
function Is${name}(obj as dynamic) as boolean
|
||||||
|
return obj <> invalid and GetInterface(obj, "${_interface}") <> invalid
|
||||||
|
end function`;
|
||||||
|
};
|
||||||
|
|
||||||
|
const validTypeTemplate = (name: string, type: string, _interface: string, defaultValue: string) => {
|
||||||
|
return `' Returns the given object if it is of type ${name}, otherwise returns the default value \`${defaultValue}\`
|
||||||
|
function Valid${name}(obj as dynamic) as ${type}
|
||||||
|
if obj <> invalid and GetInterface(obj, "${_interface}") <> invalid
|
||||||
|
return obj
|
||||||
|
else
|
||||||
|
return ${defaultValue}
|
||||||
|
end if
|
||||||
|
end function`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TypeGenPlugin implements CompilerPlugin {
|
||||||
|
public name = 'TypeGenPlugin';
|
||||||
|
|
||||||
|
program: Program | undefined;
|
||||||
|
|
||||||
|
afterProgramCreate(program: Program) {
|
||||||
|
this.program = program;
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeFileParse(source: SourceObj) {
|
||||||
|
if (!source.srcPath.endsWith(typesFilePath)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (source.source.includes(generatedCodeHeader)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let generatedCode = `\n${generatedCodeHeader}\n\n`;
|
||||||
|
|
||||||
|
types.forEach(type => {
|
||||||
|
generatedCode += `${isTypeTemplate(type.name, type.interface)}\n`;
|
||||||
|
if (type.type !== null && type.defaultValue !== null) {
|
||||||
|
generatedCode += `${validTypeTemplate(type.name, type.type, type.interface, type.defaultValue)}\n`;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const fileContents = source.source + generatedCode;
|
||||||
|
this.program!.setFile(source.srcPath, fileContents)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default () => {
|
||||||
|
return new TypeGenPlugin();
|
||||||
|
};
|
||||||
Reference in new issue
Block a user