MCPcopy Create free account
hub / github.com/Effect-TS/effect / Option

Function Option

packages/effect/src/Schema.ts:9611–9672  ·  view source on GitHub ↗
(value: A)

Source from the content-addressed store, hash-verified

9609 * @since 3.10.0
9610 */
9611export function Option<A extends Constraint>(value: A): Option<A> {
9612 const schema = declareConstructor<
9613 Option_.Option<A["Type"]>,
9614 Option_.Option<A["Encoded"]>,
9615 OptionIso<A>
9616 >()(
9617 [value],
9618 ([value]) => (input, ast, options) => {
9619 if (Option_.isOption(input)) {
9620 if (Option_.isNone(input)) {
9621 return Effect.succeedNone
9622 }
9623 return Effect.mapBothEager(
9624 SchemaParser.decodeUnknownEffect(value)(input.value, options),
9625 {
9626 onSuccess: Option_.some,
9627 onFailure: (issue) => SchemaIssue.makeCompositeAtKey(ast, "value", issue, input, options)
9628 }
9629 )
9630 }
9631 return Effect.fail(new SchemaIssue.InvalidType(ast, input, options))
9632 },
9633 {
9634 representation: {
9635 id: "effect/schema/Option",
9636 payload: null
9637 },
9638 toCode: ({ typeParameters }) => ({
9639 runtime: `Schema.Option(${typeParameters[0].runtime})`,
9640 Type: `Option.Option<${typeParameters[0].Type}>`,
9641 importDeclarations: [`import * as Option from "effect/Option"`]
9642 }),
9643 expected: "Option",
9644 toCodec: ([value]) =>
9645 link<Option_.Option<A["Encoded"]>>()(
9646 Union([
9647 Struct({ _tag: Literal("Some"), value }),
9648 Struct({ _tag: Literal("None") })
9649 ]),
9650 SchemaTransformation.transform({
9651 decode: (e) => e._tag === "None" ? Option_.none() : Option_.some(e.value),
9652 encode: (o) => (Option_.isSome(o) ? { _tag: "Some", value: o.value } as const : { _tag: "None" } as const)
9653 })
9654 ),
9655 toArbitrary: ([value]) => (fc, ctx) => {
9656 const terminal = fc.constant(Option_.none())
9657 const arbitrary = fc.oneof(
9658 terminal,
9659 value.arbitrary.map(Option_.some)
9660 )
9661 return withRecursion(fc, ctx, terminal, arbitrary)
9662 },
9663 toEquivalence: ([value]) => Option_.makeEquivalence(value),
9664 toFormatter: ([value]) =>
9665 Option_.match({
9666 onNone: () => "none()",
9667 onSome: (t) => `some(${value(t)})`
9668 })

Callers 7

Schema.tsFile · 0.70
OptionFromNullOrFunction · 0.70
OptionFromUndefinedOrFunction · 0.70
OptionFromNullishOrFunction · 0.70
OptionFromOptionalKeyFunction · 0.70
OptionFromOptionalFunction · 0.70
OptionFromOptionalNullOrFunction · 0.70

Calls 12

withRecursionFunction · 0.85
someMethod · 0.80
declareConstructorFunction · 0.70
linkFunction · 0.70
UnionFunction · 0.70
StructFunction · 0.70
LiteralFunction · 0.70
valueFunction · 0.70
makeFunction · 0.70
transformMethod · 0.65
failMethod · 0.45
mapMethod · 0.45

Tested by

no test coverage detected