-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: scalar constructor with inference (#954)
- Loading branch information
1 parent
c14e5b2
commit 9827bc9
Showing
8 changed files
with
50 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from '../../layers/5_client/client.js' | ||
export * as Scalars from './scalars.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * as Graffle from './_Graffle.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { execute } from '../../layers/0_functions/execute.js' | ||
export { request } from '../../layers/0_functions/request.js' | ||
export * as Graffle from '../../layers/5_client/client.js' | ||
export * from './__Graffle.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export * from '../../layers/1_Schema/Hybrid/types/Scalar/Scalar.js' | ||
export { create } from '../../layers/1_Schema/Hybrid/types/Scalar/Scalar.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import type { StandardScalarRuntimeTypes } from './Scalar.js' | ||
|
||
export const codec = <Decoded = any, Encoded extends StandardScalarRuntimeTypes = StandardScalarRuntimeTypes>( | ||
codec: Codec<Decoded, Encoded>, | ||
) => codec | ||
|
||
export type Codec<Decoded = any, Encoded extends StandardScalarRuntimeTypes = StandardScalarRuntimeTypes> = { | ||
encode: (value: Decoded) => Encoded | ||
decode: (value: Encoded) => Decoded | ||
export type Codec<$Decoded = any, $Encoded extends StandardScalarRuntimeTypes = StandardScalarRuntimeTypes> = { | ||
encode: (value: $Decoded) => $Encoded | ||
decode: (value: $Encoded) => $Decoded | ||
} | ||
|
||
export const createCodec = <$Decoded, $Encoded extends StandardScalarRuntimeTypes>(codec: { | ||
encode: (value: $Decoded) => $Encoded | ||
decode: (value: $Encoded) => $Decoded | ||
}): Codec<$Decoded, $Encoded> => codec |
22 changes: 11 additions & 11 deletions
22
src/layers/1_Schema/Hybrid/types/Scalar/nativeScalarCodecs.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { codec } from './codec.js' | ||
import { createCodec } from './codec.js' | ||
|
||
export const nativeScalarCodecs = { | ||
String: codec<string, string>({ | ||
encode: (value) => value, | ||
decode: (value) => value, | ||
export const JavaScriptScalarCodecs = { | ||
String: createCodec({ | ||
encode: (value: string) => value, | ||
decode: (value: string) => value, | ||
}), | ||
Number: codec<number, number>({ | ||
encode: (value) => value, | ||
decode: (value) => value, | ||
Number: createCodec({ | ||
encode: (value: number) => value, | ||
decode: (value: number) => value, | ||
}), | ||
Boolean: codec<boolean, boolean>({ | ||
encode: (value) => value, | ||
decode: (value) => value, | ||
Boolean: createCodec({ | ||
encode: (value: boolean) => value, | ||
decode: (value: boolean) => value, | ||
}), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
import { Scalar } from '../../src/layers/1_Schema/__.js' | ||
import type { Codec } from '../../src/layers/1_Schema/Hybrid/types/Scalar/codec.js' | ||
import { Graffle } from '../../src/entrypoints/alpha/main.js' | ||
|
||
export const Date = Scalar.scalar<'Date', Codec<globalThis.Date, string>>(`Date`, { | ||
encode: value => value.toISOString(), | ||
decode: value => new globalThis.Date(value), | ||
export const Date = Graffle.Scalars.create(`Date`, { | ||
encode: (value: globalThis.Date) => value.toISOString(), | ||
decode: (value: string) => new globalThis.Date(value), | ||
}) | ||
|
||
export type Date = typeof Date |