Skip to content

Commit

Permalink
fix(schema): type issues with generic native functions
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Oct 28, 2022
1 parent bf9d5a5 commit aaa0f49
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
14 changes: 8 additions & 6 deletions packages/tusken-schema/src/extract/extractFuncs.ts
Expand Up @@ -30,6 +30,7 @@ export async function extractNativeFuncs(client: Client, types: NativeTypes) {
const orderedSetFuncs = ['mode', 'percentile_cont', 'percentile_disc']
const ignoredFuncs = [...hypotheticalSetFuncs, ...orderedSetFuncs]

const anyArrayTypes = ['anyarray', 'anycompatiblearray']
const elementTypes = ['anyelement', 'anycompatible']

return nativeFuncs.filter(fn => {
Expand All @@ -51,11 +52,12 @@ export async function extractNativeFuncs(client: Client, types: NativeTypes) {
}

fn.argTypes = argTypes.map(t => t.jsType)
fn.returnType = types.byId[+fn.returnType].jsType
const returnType = types.byId[+fn.returnType]
fn.returnType = returnType.jsType

const hasGenericReturn = types.any.includes(fn.returnType)
const genericArrayArg = argTypes.find(
t => types.any.includes(t.name) && !elementTypes.includes(t.name)
const hasGenericReturn = types.any.includes(returnType.name)
const genericArrayArg = argTypes.find(argType =>
anyArrayTypes.includes(argType.name)
)

const newArgTypes = [...fn.argTypes]
Expand All @@ -64,7 +66,7 @@ export async function extractNativeFuncs(client: Client, types: NativeTypes) {
newArgTypes[i] = elementTypes.includes(argType.name)
? genericArrayArg
? 't.elementof<T>'
: 'T | UnwrapType<T>'
: 'T'
: 'T'

return true
Expand All @@ -85,7 +87,7 @@ export async function extractNativeFuncs(client: Client, types: NativeTypes) {
: 'T'
}
} else {
fn.typeParams = '<T extends Type = t.any>'
fn.typeParams = '<T extends t.any>'
if (hasGenericReturn) {
fn.returnType = elementTypes.includes(fn.returnType)
? 'T'
Expand Down
7 changes: 4 additions & 3 deletions packages/tusken-schema/src/typescript/generateNativeFuncs.ts
Expand Up @@ -8,7 +8,7 @@ export function generateNativeFuncs(
docs: Record<string, string>
) {
const imports = endent`
import { defineFunction, defineSetFunction, Aggregate, CallExpression, SetRef } from 'tusken'
import { defineFunction, defineSetFunction, Aggregate, CallExpression, Expression, SetRef, Type } from 'tusken'
import * as t from './types'
`

Expand Down Expand Up @@ -204,14 +204,15 @@ function renderInputs(fn: NativeFunc, isNullable?: boolean) {
}
const orNull = isNullable ? ' | t.null' : ''
const argTypes = fn.argTypes.map(type => {
const needParamType = !fn.typeParams || /any\w*array\b/.test(fn.typeParams)
const needParamType =
!fn.typeParams || !/any\w*nonarray\b/.test(fn.typeParams)

type += orNull
return needParamType
? fn.kind == 'a'
? `t.aggParam<${type}>`
: `t.param<${type}>`
: type
: `Expression<${type}>`

This comment has been minimized.

Copy link
@aleclarson

aleclarson Oct 29, 2022

Author Contributor

Hmm.. this seems incorrect. Shouldn't JS types be allowed too?

})
return fn.isVariadic && argTypes.length == 1
? `...args: ${argTypes[0]}[]`
Expand Down
5 changes: 3 additions & 2 deletions packages/tusken-schema/src/typescript/generateNativeTypes.ts
Expand Up @@ -68,7 +68,7 @@ export function generateNativeTypes(

const types: string[] = []
const runtimeTypes: string[] = ['const option = defineOptionalType']

for (const [nativeType, mappedType] of nativeTypeMap) {
types.push(
`type ${nativeType} = Type<"${nativeType}", ${mappedType}, ColumnCast<"${nativeType}">>`
Expand Down Expand Up @@ -104,7 +104,7 @@ export function generateNativeTypes(
const specialTypes = [
'type elementof<T extends Type> = T extends array<infer E> ? E : anyelement',
'type param<T extends Type> = QueryInput<T extends Type<infer Native> ? T | ImplicitCast<Native> : never>',
'type aggParam<T extends Type> = T extends Type<infer Native> ? T | ImplicitCast<Native> | NULL : never',
'type aggParam<T extends Type> = Expression<T extends Type<infer Native> ? T | ImplicitCast<Native> | NULL : never>',
'type record = Type<"record", { [key: string]: any }, never>',
]

Expand All @@ -114,6 +114,7 @@ export function generateNativeTypes(
'defineOptionalType',
'defineType',
'tokenizeJson',
'Expression',
'Interval',
'Json',
'QueryInput',
Expand Down

0 comments on commit aaa0f49

Please sign in to comment.