Skip to content

Commit

Permalink
Tighter types for isPayloadOfSchemaType
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 27, 2024
1 parent 639f802 commit bd1aefe
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 86 deletions.
2 changes: 1 addition & 1 deletion packages/modules/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"@xyo-network/witness": "workspace:^"
},
"devDependencies": {
"@swc/core": "1.10.2",
"@swc/core": "1.10.3",
"@xylabs/delay": "^4.4.27",
"@xylabs/ts-scripts-yarn3": "^4.2.6",
"@xylabs/tsconfig": "^4.2.6",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ export abstract class AbstractModuleProxy<
}

override async startHandler(): Promise<boolean> {
let manifest: ModuleManifestPayload | undefined = this.params.manifest
let manifest: ModuleManifestPayload | NodeManifestPayload | undefined = this.params.manifest
if (!manifest) {
const state = await this.state()
const manifestPayload = state.find(
payload => isPayloadOfSchemaType(NodeManifestPayloadSchema)(payload) || isPayloadOfSchemaType(ModuleManifestPayloadSchema)(payload),
) as ModuleManifestPayload
payload => isPayloadOfSchemaType<NodeManifestPayload>(NodeManifestPayloadSchema)(payload)
|| isPayloadOfSchemaType<ModuleManifestPayload>(ModuleManifestPayloadSchema)(payload),
)
manifest = assertEx(manifestPayload, () => "Can't find manifest payload")
}
this.setConfig({ ...manifest.config })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe('HttpBridge', () => {

if (bridgedHostedNode) {
const state = await bridgedHostedNode.state()
const description = state.find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = state.find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(description?.children).toBeArray()
expect(description?.queries).toBeArray()
expect(description?.queries?.length).toBeGreaterThan(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('HttpBridge', () => {
)

const state = await remoteNode.state()
const description = state.find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = state.find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(description?.children).toBeArray()
expect(description?.children?.length).toBeGreaterThan(0)
expect(description?.queries).toBeArray()
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('HttpBridge', () => {

await memNode3.register(remoteNode)
await memNode3.attach(remoteNode?.address, true)
const description = (await remoteNode.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await remoteNode.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(description?.children).toBeArray()
expect(description?.children?.length).toBeGreaterThan(0)
expect(description?.queries).toBeArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe.skip('WebsocketBridge', () => {
)

const state = await remoteNode.state()
const description = state.find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = state.find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(description?.children).toBeArray()
expect(description?.children?.length).toBeGreaterThan(0)
expect(description?.queries).toBeArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import {

import { AddressChainQuerySchema } from './Schema.ts'

export type AddressChainQueryPayload = Query<{ schema: AddressChainQuerySchema } & PayloadFindFilter>
export const isAddressChainQueryPayload = isPayloadOfSchemaType(AddressChainQuerySchema)
export type AddressChainQueryPayload = Query<{ schema: AddressChainQuerySchema } & Omit<PayloadFindFilter, 'schema'>>
export const isAddressChainQueryPayload = isPayloadOfSchemaType<AddressChainQueryPayload>(AddressChainQuerySchema)
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export const AddressHistoryQuerySchema: AddressHistoryQuerySchema = 'network.xyo
export type AddressHistoryPayload = Payload<{ schema: AddressHistorySchema }>
export const isAddressHistoryPayload = (x?: Payload | null): x is AddressHistoryPayload => x?.schema === AddressHistorySchema

export type AddressHistoryQueryPayload = Query<{ schema: AddressHistoryQuerySchema } & PayloadFindFilter>
export const isAddressHistoryQueryPayload = isPayloadOfSchemaType(AddressHistoryQuerySchema)
export type AddressHistoryQueryPayload = Query<{ schema: AddressHistoryQuerySchema } & Omit<PayloadFindFilter, 'schema'>>
export const isAddressHistoryQueryPayload = isPayloadOfSchemaType<AddressHistoryQueryPayload>(AddressHistoryQuerySchema)
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import {

import { CoinUserLocationsQuerySchema } from './Schema.ts'

export type CoinUserLocationsQueryPayload = Query<{ schema: CoinUserLocationsQuerySchema } & PayloadFindFilter>
export const isCoinUserLocationsQueryPayload = isPayloadOfSchemaType(CoinUserLocationsQuerySchema)
export type CoinUserLocationsQueryPayload = Query<{ schema: CoinUserLocationsQuerySchema } & Omit<PayloadFindFilter, 'schema'>>
export const isCoinUserLocationsQueryPayload = isPayloadOfSchemaType<CoinUserLocationsQueryPayload>(CoinUserLocationsQuerySchema)
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import '@xylabs/vitest-extended'

import { delay } from '@xylabs/delay'
import { IndexedDbArchivist } from '@xyo-network/archivist-indexeddb'
import type { IndexDescription } from '@xyo-network/archivist-model'
import type { PayloadDivinerQueryPayload } from '@xyo-network/diviner-payload-model'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '@xyo-network/diviner-temporal-indexing-model'
import type { Labels } from '@xyo-network/module-model'
import { PayloadBuilder } from '@xyo-network/payload-builder'
import type { Payload } from '@xyo-network/payload-model'
import type { Payload, Schema } from '@xyo-network/payload-model'
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
// TODO: Inherit from JsonPathAggregateDiviner
/**
Expand All @@ -27,34 +27,34 @@ export class TemporalIndexingDivinerDivinerQueryToIndexQueryDiviner<
'network.xyo.diviner.stage': 'divinerQueryToIndexQueryDiviner',
}

private _indexableSchemas: string[] | undefined
private _indexableSchemas: Schema[] | undefined
private _payloadTransformers: SchemaToPayloadTransformersDictionary | undefined

/**
* The schema of the diviner query payloads
*/
protected get divinerQuerySchema(): string {
protected get divinerQuerySchema(): Schema {
return this.config.divinerQuerySchema ?? PayloadDivinerQuerySchema
}

/**
* The schema of the index query payloads
*/
protected get indexQuerySchema(): string {
protected get indexQuerySchema(): Schema {
return this.config.indexQuerySchema ?? PayloadDivinerQuerySchema
}

/**
* The schema of the index payloads
*/
protected get indexSchema(): string {
protected get indexSchema(): Schema {
return this.config.indexSchema ?? TemporalIndexingDivinerResultIndexSchema
}

/**
* List of indexable schemas for this diviner
*/
protected get indexableSchemas(): string[] {
protected get indexableSchemas(): Schema[] {
if (!this._indexableSchemas) this._indexableSchemas = Object.keys(this.schemaTransforms)
return this._indexableSchemas
}
Expand Down Expand Up @@ -97,7 +97,8 @@ export class TemporalIndexingDivinerDivinerQueryToIndexQueryDiviner<
}

protected override async divineHandler(payloads: Payload[] = []): Promise<Payload[]> {
const queries = payloads.filter(isPayloadOfSchemaType<PayloadDivinerQueryPayload>(this.divinerQuerySchema))
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const queries = payloads.filter(isPayloadOfSchemaType<PayloadDivinerQueryPayload>(this.divinerQuerySchema as any))
if (queries.length > 0) {
return await Promise.all(
queries.map(async (query) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,15 @@ describe('TemporalIndexingDivinerDivinerQueryToIndexQueryDiviner', () => {
describe('with single query', () => {
it.each(cases)('transforms query using default settings', async (query, expected) => {
const results = await diviner.divine([query])
const actual = results.filter(isPayloadOfSchemaType(indexQuerySchema))
const actual = results.filter(isPayloadOfSchemaType<Payload>(indexQuerySchema))
expect(actual).toBeArrayOfSize(1)
expect(PayloadBuilder.omitMeta(actual[0])).toEqual(PayloadBuilder.omitMeta(expected))
})
})
describe('with multiple queries', () => {
it('transforms queries using default settings', async () => {
const results = await diviner.divine(queries)
const actual = results.filter(isPayloadOfSchemaType(indexQuerySchema))
const actual = results.filter(isPayloadOfSchemaType<Payload>(indexQuerySchema))
expect(actual).toBeArrayOfSize(expected.length)
expect(actual.map(i => PayloadBuilder.omitMeta(i))).toEqual(expected.map(i => PayloadBuilder.omitMeta(i)))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { SchemaToJsonPathTransformExpressionsDictionary } from '@xyo-network/diviner-jsonpath-model'
import type { DivinerConfig } from '@xyo-network/diviner-model'
import type { Schema } from '@xyo-network/payload-model'

import { TemporalIndexingDivinerDivinerQueryToIndexQueryDivinerSchema } from './Schema.ts'

Expand All @@ -15,15 +16,15 @@ export type TemporalIndexingDivinerDivinerQueryToIndexQueryDivinerConfig = Divin
/**
* The schema of the diviner query payloads
*/
divinerQuerySchema?: string
divinerQuerySchema?: Schema
/**
* The schema of the index query payloads
*/
indexQuerySchema?: string
indexQuerySchema?: Schema
/**
* The schema of the index payloads
*/
indexSchema?: string
indexSchema?: Schema
/**
* The config schema
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('JsonPathAggregateDiviner', () => {
const [timestamp, thumbnail, payload] = input
const payloadDictionary = await PayloadBuilder.toDataHashMap([timestamp, thumbnail, payload])
expect(result).toBeArrayOfSize(1)
expect(result.filter(isPayloadOfSchemaType(destinationSchema))).toBeArrayOfSize(1)
expect(result.filter(isPayloadOfSchemaType<Payload>(destinationSchema))).toBeArrayOfSize(1)
const index = result.find(isPayloadOfSchemaType<ResultType>(destinationSchema))
expect(index?.sources.sort()).toEqual(Object.keys(payloadDictionary).sort())
expect(index?.timestamp).toBe(timestamp.timestamp)
Expand Down Expand Up @@ -145,7 +145,7 @@ describe('JsonPathAggregateDiviner', () => {
const [boundWitness, timestamp, thumbnail, payload] = input
const payloadDictionary = await PayloadBuilder.toDataHashMap([boundWitness, timestamp, thumbnail, payload])
expect(result).toBeArrayOfSize(1)
expect(result.filter(isPayloadOfSchemaType(destinationSchema))).toBeArrayOfSize(1)
expect(result.filter(isPayloadOfSchemaType<Payload>(destinationSchema))).toBeArrayOfSize(1)
const index = result.find(isPayloadOfSchemaType<ResultType>(destinationSchema))
expect(index?.sources.sort()).toEqual(Object.keys(payloadDictionary).sort())
expect(index?.timestamp).toBe(timestamp.timestamp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export type PayloadDivinerQueryPayload<T extends EmptyObject = EmptyObject> = Qu
{ schema: PayloadDivinerQuerySchema } & PayloadDivinerPredicate<T>
>

export const isPayloadDivinerQueryPayload = isPayloadOfSchemaType(PayloadDivinerQuerySchema)
export const isPayloadDivinerQueryPayload = isPayloadOfSchemaType<PayloadDivinerQueryPayload>(PayloadDivinerQuerySchema)

export const asPayloadDivinerQueryPayload = AsObjectFactory.create(isPayloadDivinerQueryPayload)
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ describe('MemoryNode', () => {
}
describe('node without child modules', () => {
it('describes node alone', async () => {
const description = (await node.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await node.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
validateModuleDescription(description)
expect(description?.children).toBeArrayOfSize(0)
})
it('serializes to JSON consistently', async () => {
const description = (await node.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await node.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(prettyPrintDescription(description)).toMatchSnapshot()
})
})
Expand All @@ -260,13 +260,13 @@ describe('MemoryNode', () => {
)
})
it('describes node and child mods', async () => {
const description = (await node.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await node.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
validateModuleDescription(description)
expect(description?.children).toBeArrayOfSize(2)
// description.children?.map(validateModuleDescription)
})
it('serializes to JSON consistently', async () => {
const description = (await node.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await node.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(prettyPrintDescription(description)).toMatchSnapshot()
})
})
Expand Down Expand Up @@ -298,13 +298,13 @@ describe('MemoryNode', () => {
await memoryNode.register(archivist2)
await memoryNode.attach(archivist2.address, true)
console.log('status:', memoryNode.status)
const description = (await memoryNode.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await memoryNode.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
validateModuleDescription(description)
expect(description?.children).toBeArrayOfSize(2)
// description.children?.map(validateModuleDescription)
})
it('serializes to JSON consistently', async () => {
const description = (await node.state()).find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
const description = (await node.state()).find(isPayloadOfSchemaType<ModuleDescriptionPayload>(ModuleDescriptionSchema))
expect(prettyPrintDescription(description)).toMatchSnapshot()
})
it('clone-all', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { isAnyPayload } from './isPayload.ts'
import type { Payload, WithSources } from './Payload.ts'

export const isPayloadOfSchemaType = <T extends Payload>(schema: string) => {
export function isPayloadOfSchemaType<T extends Payload | never = never>(schema: T['schema']) {
return (x?: unknown | null): x is T => isAnyPayload(x) && x?.schema === schema
}

export const isPayloadOfSchemaTypeWithSources = <T extends Payload>(schema: string) => {
export const isPayloadOfSchemaTypeWithSources = <T extends Payload | never = never>(schema: T['schema']) => {
return (x?: unknown | null): x is WithSources<T> =>
isPayloadOfSchemaType<WithSources<T>>(schema)(x) && x.$sources !== undefined && Array.isArray(x.$sources)
}

export const notPayloadOfSchemaType = <T extends Payload>(schema: string) => {
export const notPayloadOfSchemaType = <T extends Payload | never = never>(schema: T['schema']) => {
return (x?: unknown | null): x is T => !isAnyPayload(x) || x?.schema !== schema
}

Expand All @@ -26,7 +26,7 @@ const testPayload: Test = { field: 'test', schema: TestSchema }
const testPayloads: Payload[] = [testPayload]
const isTest: Test[] = testPayloads.filter(isPayloadOfSchemaType(TestSchema))
const isTest: Test[] = testPayloads.filter(isPayloadOfSchemaType<Payload>(TestSchema))
const isTestFromMetaTyped = testMetaPayloads.filter(isPayloadOfSchemaType<Test>(TestSchema))
*/
Loading

0 comments on commit bd1aefe

Please sign in to comment.