Skip to content

Commit

Permalink
add: Schema.contextFromServices
Browse files Browse the repository at this point in the history
  • Loading branch information
patroza committed Nov 6, 2024
1 parent 419bfba commit 82363f6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/purple-weeks-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect-app": minor
---

add: Schema.contextFromServices
24 changes: 23 additions & 1 deletion packages/effect-app/src/Schema/ext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import type { SchemaAST } from "effect"
import { Effect, ParseResult, pipe } from "effect"
import type { NonEmptyReadonlyArray } from "effect/Array"
import type { Tag } from "effect/Context"
import type { Schema } from "effect/Schema"
import * as S from "effect/Schema"
import type { Context } from "../internal/lib.js"
import { Context } from "../internal/lib.js"
import { extendM, typedKeysOf } from "../utils.js"

export const withDefaultConstructor: <A, I, R>(
Expand Down Expand Up @@ -284,3 +285,24 @@ export const provide = <Self extends S.Schema.Any, R>(
encode: (t) => (n) => provide(ParseResult.encodeUnknown(t)(n))
}) as any
}
export const contextFromServices = <Self extends S.Schema.Any, Tags extends readonly Tag<any, any>[]>(
self: Self,
...services: Tags
): Effect.Effect<
S.SchemaClass<
S.Schema.Type<Self>,
S.Schema.Encoded<Self>,
Exclude<S.Schema.Context<Self>, { [K in keyof Tags]: Tag.Identifier<Tags[K]> }[number]>
>,
never,
{ [K in keyof Tags]: Tag.Identifier<Tags[K]> }[number]
> =>
Effect.gen(function*() {
const context = Context.pick(...services)(yield* Effect.context())
const provide = Effect.provide(context)
return S
.declare([self], {
decode: (t) => (n) => provide(ParseResult.decodeUnknown(t)(n)),
encode: (t) => (n) => provide(ParseResult.encodeUnknown(t)(n))
})
}) as any

0 comments on commit 82363f6

Please sign in to comment.