Skip to content

Commit

Permalink
feat: scalar constructor with inference (#954)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Jul 1, 2024
1 parent c14e5b2 commit 9827bc9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 34 deletions.
2 changes: 2 additions & 0 deletions src/entrypoints/alpha/_Graffle.ts
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'
1 change: 1 addition & 0 deletions src/entrypoints/alpha/__Graffle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as Graffle from './_Graffle.js'
2 changes: 1 addition & 1 deletion src/entrypoints/alpha/main.ts
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'
2 changes: 1 addition & 1 deletion src/entrypoints/alpha/scalars.ts
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'
31 changes: 22 additions & 9 deletions src/layers/1_Schema/Hybrid/types/Scalar/Scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

import type { GlobalRegistry } from '../../../../2_generator/globalRegistry.js'
import type { Codec } from './codec.js'
import { nativeScalarCodecs } from './nativeScalarCodecs.js'
import { JavaScriptScalarCodecs } from './nativeScalarCodecs.js'

export { nativeScalarCodecs } from './nativeScalarCodecs.js'
export { JavaScriptScalarCodecs } from './nativeScalarCodecs.js'

export const ScalarKind = `Scalar`

export type ScalarKind = typeof ScalarKind

export type StandardScalarRuntimeTypes = boolean | number | string

export const create = <$Name extends string, $Decoded, $Encoded extends StandardScalarRuntimeTypes>(
name: $Name,
codec: {
encode: (value: $Decoded) => $Encoded
decode: (value: $Encoded) => $Decoded
},
): Scalar<$Name, $Decoded, $Encoded> => ({
kind: ScalarKind,
name: name,
codec: codec as any, // eslint-disable-line
})

export const scalar = <$Name extends string, $Codec extends Codec<any, any>>(
name: $Name,
codec: $Codec,
Expand All @@ -23,22 +35,23 @@ export const scalar = <$Name extends string, $Codec extends Codec<any, any>>(

export interface Scalar<
$Name extends string = string,
$Codec extends Codec = Codec,
$Decoded = unknown,
$Encoded extends StandardScalarRuntimeTypes = StandardScalarRuntimeTypes,
> {
kind: ScalarKind
name: $Name
codec: $Codec
codec: Codec<$Decoded, $Encoded>
}

export const String = scalar(`String`, nativeScalarCodecs.String)
export const String = create(`String`, JavaScriptScalarCodecs.String)

export const ID = scalar(`ID`, nativeScalarCodecs.String)
export const ID = create(`ID`, JavaScriptScalarCodecs.String)

export const Int = scalar(`Int`, nativeScalarCodecs.Number)
export const Int = create(`Int`, JavaScriptScalarCodecs.Number)

export const Float = scalar(`Float`, nativeScalarCodecs.Number)
export const Float = create(`Float`, JavaScriptScalarCodecs.Number)

export const Boolean = scalar(`Boolean`, nativeScalarCodecs.Boolean)
export const Boolean = create(`Boolean`, JavaScriptScalarCodecs.Boolean)

export type ID = typeof ID

Expand Down
15 changes: 8 additions & 7 deletions src/layers/1_Schema/Hybrid/types/Scalar/codec.ts
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 src/layers/1_Schema/Hybrid/types/Scalar/nativeScalarCodecs.ts
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,
}),
}
9 changes: 4 additions & 5 deletions tests/_/customScalarCodecs.ts
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

0 comments on commit 9827bc9

Please sign in to comment.