observablehq / stdlib

The Observable standard library.
https://observablehq.com/@observablehq/standard-library
ISC License
957 stars 83 forks source link

Add and export getSchema function #363

Closed annie closed 1 year ago

annie commented 1 year ago

This PR adds a getSchema function, building on a suggestion from Mike. This function conditionally infers a schema depending on whether or not a valid one exists on the source, combines the schema with any saved type assertions, and determines whether or not the source data needs to be coerced.

We will use getSchema in both __table and in the table schema tasks in the worker (I'll update https://github.com/observablehq/observablehq/pull/11496 to use this function), and thereby unify the two paths that we currently have for type inference.

I also added a missing unit test for type assertion in __table, as well as some tests for getSchema.

annie commented 1 year ago

hm on second thought, maybe we should handle type assertions outside of getSchema, because it's a pain to have to pass around operations.types in the worker.