Remove type-gen-plugin.ts
This commit is contained in:
1 parent
079f93bddb
commit
f858d3a848
5 files changed
+113
-109
No files matched your search
@@ -88,7 +88,6 @@
|
||||
"../tools/bs-plugins/json-yaml-plugin.ts",
|
||||
"../tools/bs-plugins/validation-plugin.ts",
|
||||
"../tools/bs-plugins/logger-plugin.ts",
|
||||
"../tools/bs-plugins/type-gen-plugin.ts",
|
||||
"../tools/bs-plugins/web-server-plugin.ts",
|
||||
"../tools/bs-plugins/locale-validation-plugin.ts",
|
||||
"../tools/bs-plugins/proto-gen-plugin.ts"
|
||||
|
||||
@@ -26,7 +26,6 @@
|
||||
"../tools/bs-plugins/json-yaml-plugin.ts",
|
||||
"../tools/bs-plugins/validation-plugin.ts",
|
||||
"../tools/bs-plugins/logger-plugin.ts",
|
||||
"../tools/bs-plugins/type-gen-plugin.ts",
|
||||
"../tools/bs-plugins/web-server-plugin.ts",
|
||||
"../tools/bs-plugins/locale-validation-plugin.ts",
|
||||
"../tools/bs-plugins/proto-gen-plugin.ts"
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
- [Bindings](#bindings)
|
||||
- [Job System Generator](#job-system-generator)
|
||||
- [Logger](#logger)
|
||||
- [Type Gen](#type-gen)
|
||||
- [Web Server](#web-server)
|
||||
- [Locale validation](#locale-validation)
|
||||
- [Protobuf Generator](#protobuf-generator)
|
||||
@@ -506,36 +505,6 @@ To understand more about what's being generated, download `playlet-lib.zip` from
|
||||
|
||||
To give credit where credit is due, some of the ideas here (like adding the source location to the log) came from [roku-log](https://github.com/georgejecook/roku-log)
|
||||
|
||||
## Type Gen
|
||||
|
||||
**[Source](/tools/bs-plugins/type-gen-plugin.ts)**
|
||||
|
||||
### Why
|
||||
|
||||
I found myself implementing type checking functions like `IsBool`, `IsInt`, and `IsString`, and functions for getting a valid type, like `ValidBool`, `ValidInt`, and `ValidString`. The implementation is the same, why not generate them?
|
||||
|
||||
### How
|
||||
|
||||
Simply we declare them in an object describing the type, the interface, and the default value, and generate all corresponding functions.
|
||||
|
||||
Sample output:
|
||||
|
||||
```brs
|
||||
' Returns true if the given object is of type Bool, false otherwise
|
||||
function IsBool(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Bool, otherwise returns the default value `false`
|
||||
function ValidBool(obj as dynamic) as boolean
|
||||
if obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
||||
return obj
|
||||
else
|
||||
return false
|
||||
end if
|
||||
end function
|
||||
```
|
||||
|
||||
## Web Server
|
||||
|
||||
**[Source](/tools/bs-plugins/web-server-plugin.ts)**
|
||||
|
||||
@@ -1,5 +1,116 @@
|
||||
' This file will be populated with the functions using type-gen-plugin.ts
|
||||
|
||||
function FuncName(func as function) as string
|
||||
return (LCase(`${func}`)).replace("function:", "").trim()
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Bool, false otherwise
|
||||
function IsBool(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Bool, otherwise returns the default value `false`
|
||||
function ValidBool(obj as dynamic) as boolean
|
||||
if obj <> invalid and GetInterface(obj, "ifBoolean") <> invalid
|
||||
return obj
|
||||
else
|
||||
return false
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Int, false otherwise
|
||||
function IsInt(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifInt") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Int, otherwise returns the default value `0%`
|
||||
function ValidInt(obj as dynamic) as integer
|
||||
if obj <> invalid and GetInterface(obj, "ifInt") <> invalid
|
||||
return obj
|
||||
else
|
||||
return 0%
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Float, false otherwise
|
||||
function IsFloat(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifFloat") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Float, otherwise returns the default value `0!`
|
||||
function ValidFloat(obj as dynamic) as float
|
||||
if obj <> invalid and GetInterface(obj, "ifFloat") <> invalid
|
||||
return obj
|
||||
else
|
||||
return 0!
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Double, false otherwise
|
||||
function IsDouble(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifDouble") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Double, otherwise returns the default value `0#`
|
||||
function ValidDouble(obj as dynamic) as double
|
||||
if obj <> invalid and GetInterface(obj, "ifDouble") <> invalid
|
||||
return obj
|
||||
else
|
||||
return 0#
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type String, false otherwise
|
||||
function IsString(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifString") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type String, otherwise returns the default value `""`
|
||||
function ValidString(obj as dynamic) as string
|
||||
if obj <> invalid and GetInterface(obj, "ifString") <> invalid
|
||||
return obj
|
||||
else
|
||||
return ""
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Array, false otherwise
|
||||
function IsArray(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifArray") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type Array, otherwise returns the default value `CreateObject("roArray", 0, true)`
|
||||
function ValidArray(obj as dynamic) as object
|
||||
if obj <> invalid and GetInterface(obj, "ifArray") <> invalid
|
||||
return obj
|
||||
else
|
||||
return CreateObject("roArray", 0, true)
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type AssociativeArray, false otherwise
|
||||
function IsAssociativeArray(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifAssociativeArray") <> invalid
|
||||
end function
|
||||
|
||||
' Returns the given object if it is of type AssociativeArray, otherwise returns the default value `{}`
|
||||
function ValidAssociativeArray(obj as dynamic) as object
|
||||
if obj <> invalid and GetInterface(obj, "ifAssociativeArray") <> invalid
|
||||
return obj
|
||||
else
|
||||
return {}
|
||||
end if
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type ByteArray, false otherwise
|
||||
function IsByteArray(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifByteArray") <> invalid
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Function, false otherwise
|
||||
function IsFunction(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifFunction") <> invalid
|
||||
end function
|
||||
|
||||
' Returns true if the given object is of type Node, false otherwise
|
||||
function IsNode(obj as dynamic) as boolean
|
||||
return obj <> invalid and GetInterface(obj, "ifSGNodeDict") <> invalid
|
||||
end function
|
||||
@@ -1,74 +0,0 @@
|
||||
// This plugin generates a file containing functions for type checking and type casting.
|
||||
import { CompilerPlugin, Program, SourceObj } from "brighterscript";
|
||||
import path from "path";
|
||||
|
||||
const types = [
|
||||
{ name: 'Bool', interface: 'ifBoolean', type: 'boolean', defaultValue: 'false' },
|
||||
{ name: 'Int', interface: 'ifInt', type: 'integer', defaultValue: '0%' },
|
||||
{ name: 'Float', interface: 'ifFloat', type: 'float', defaultValue: '0!' },
|
||||
{ name: 'Double', interface: 'ifDouble', type: 'double', defaultValue: '0#' },
|
||||
{ name: 'String', interface: 'ifString', type: 'string', defaultValue: '""' },
|
||||
{ name: 'Array', interface: 'ifArray', type: 'object', defaultValue: 'CreateObject("roArray", 0, true)' },
|
||||
{ name: 'AssociativeArray', interface: 'ifAssociativeArray', type: 'object', defaultValue: '{}' },
|
||||
{ name: 'ByteArray', interface: 'ifByteArray', type: null, defaultValue: null },
|
||||
{ name: 'Function', interface: 'ifFunction', type: null, defaultValue: null },
|
||||
{ name: 'Node', interface: 'ifSGNodeDict', type: null, defaultValue: null },
|
||||
];
|
||||
|
||||
const generatedCodeHeader = `' The rest of this file is generated by the TypeGenPlugin`
|
||||
|
||||
const typesFilePath = path.join("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