feat: add ResourceType<'name'> as deno signature arg type

This commit is contained in:
Ruben Fiszel
2022-06-14 01:43:24 +02:00
parent 858fb70890
commit c2b8848d2f
4 changed files with 50 additions and 6 deletions
+9 -2
View File
@@ -3117,8 +3117,15 @@ components:
name:
type: string
typ:
type: string
enum: ["str", "float", "int", "bool", "unknown"]
oneOf:
- type: string
enum: ["str", "float", "int", "bool", "unknown"]
- type: object
properties:
resourcetype:
type: string
required:
- resourcetype
has_default:
type: boolean
default: {}
+31 -2
View File
@@ -36,6 +36,7 @@ pub enum Typ {
List,
Bytes,
Datetime,
ResourceType(String),
Unknown,
}
@@ -119,7 +120,7 @@ use swc_common::sync::Lrc;
use swc_common::{FileName, SourceMap};
use swc_ecma_ast::{
AssignPat, BindingIdent, Decl, ExportDecl, FnDecl, Ident, ModuleDecl, ModuleItem, Pat,
TsKeywordTypeKind, TsType,
TsEntityName, TsKeywordTypeKind, TsType, TsTypeRef,
};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
@@ -239,6 +240,33 @@ fn binding_ident_to_arg(
},
// TODO: we can do better here and extract the inner type of array
TsType::TsArrayType(_) => Typ::List,
TsType::TsTypeRef(TsTypeRef {
span: _,
type_name:
TsEntityName::Ident(Ident {
span: _,
sym,
optional: _,
}),
type_params,
}) => {
println!("N{sym:?}\nP{type_params:?}");
match sym.to_string().as_str() {
"ResourceType" => Typ::ResourceType(
type_params
.as_ref()
.and_then(|x| {
x.params.get(0).and_then(|y| {
y.as_ts_lit_type().and_then(|z| {
z.lit.as_str().map(|a| a.to_owned().value.to_string())
})
})
})
.unwrap_or_else(|| "unknown".to_string()),
),
_ => Typ::Unknown,
}
}
_ => Typ::Unknown,
})
.unwrap_or(Typ::Unknown),
@@ -724,7 +752,8 @@ def main():
fn test_parse_deno_sig() -> anyhow::Result<()> {
let code = "
export function main(test1: string, test2: string = \"burkina\") {
export function main(test1: string, test2: string = \"burkina\",
test3: ResourceType<'rt'>, email: email_string) {
console.log(42)
}
+5
View File
@@ -7,6 +7,11 @@ export {
UserApi, WorkspaceApi
} from './windmill-api/index.ts'
export type string_regex<S extends string> = String
export type string_email = String
export type ResourceType<S extends string> = {}
/**
* Create a client configuration from env variables
* @returns client configuration
+5 -2
View File
@@ -31,7 +31,7 @@ export async function inferArgs(
} else {
schema.properties[arg.name] = oldProperties[arg.name]
}
pythonToJsonSchemaType(arg.typ, schema.properties[arg.name])
argSigToJsonSchemaType(arg.typ, schema.properties[arg.name])
schema.properties[arg.name].default = arg.default
if (!arg.has_default) {
@@ -44,7 +44,7 @@ export async function inferArgs(
}
}
function pythonToJsonSchemaType(t: string, s: SchemaProperty): void {
function argSigToJsonSchemaType(t: string | { resourcetype: string }, s: SchemaProperty): void {
if (t === 'int') {
s.type = 'integer'
} else if (t === 'float') {
@@ -63,6 +63,9 @@ function pythonToJsonSchemaType(t: string, s: SchemaProperty): void {
} else if (t === 'datetime') {
s.type = 'string'
s.format = 'date-time'
} else if (typeof t !== 'string' && t.resourcetype != undefined) {
s.type = 'object'
s.format = `resource-${t.resourcetype}`
} else {
s.type = undefined
}