Skip to content

Commit

Permalink
Schema: Arbitrary remove from/to API in favour of make (#1877)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Jan 8, 2024
1 parent 7b2f874 commit a904a73
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 227 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-ducks-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

Schema: Arbitrary remove `from`/`to` API in favour of `make`
21 changes: 6 additions & 15 deletions packages/schema/src/Arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,14 @@ export const unsafe = <I, A>(
): Arbitrary<A> => go(schema.ast, {})

/**
* Returns a fast-check Arbitrary for the `A` type of the provided schema.
*
* @category arbitrary
* @since 1.0.0
*/
export const to = <I, A>(
export const make = <I, A>(
schema: Schema.Schema<I, A>
): Arbitrary<A> => go(AST.to(schema.ast), {})

/**
* @category arbitrary
* @since 1.0.0
*/
export const from = <I, A>(
schema: Schema.Schema<I, A>
): Arbitrary<I> => go(AST.from(schema.ast), {})
): Arbitrary<A> => go(schema.ast, {})

const depthSize = 1

Expand Down Expand Up @@ -99,12 +93,9 @@ const go = (ast: AST.AST, options: Options): Arbitrary<any> => {
return hook.value(...ast.typeParameters.map((p) => go(p, options)))
case "Refinement":
return hook.value(getRefinementFromArbitrary(ast, options))
case "Suspend":
default:
return hook.value()
}
throw new Error(
"Arbitrary annotations are only managed in the following three scenarios: Declarations, Refinements, Suspensions"
)
}
switch (ast._tag) {
case "Declaration": {
Expand Down Expand Up @@ -293,7 +284,7 @@ const go = (ast: AST.AST, options: Options): Arbitrary<any> => {
return (fc) => fc.constant(null).chain(() => get()(fc))
}
case "Transform":
throw new Error("cannot build an Arbitrary for transformations")
return go(ast.to, options)
}
}

Expand Down
18 changes: 5 additions & 13 deletions packages/schema/src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3287,8 +3287,7 @@ export {
const makeEncodingTransformation = (
id: string,
decode: (s: string) => Either.Either<Encoding.DecodeException, Uint8Array>,
encode: (u: Uint8Array) => string,
arbitrary: Arbitrary<Uint8Array>
encode: (u: Uint8Array) => string
): Schema<string, Uint8Array> =>
transformOrFail(
string,
Expand All @@ -3300,11 +3299,7 @@ const makeEncodingTransformation = (
),
(u) => ParseResult.succeed(encode(u)),
{ strict: false }
).pipe(annotations({
[AST.IdentifierAnnotationId]: id,
[hooks.PrettyHookId]: (): Pretty.Pretty<Uint8Array> => (u) => `${id}(${encode(u)})`,
[hooks.ArbitraryHookId]: () => arbitrary
}))
).pipe(identifier(id))

/**
* @category Encoding transformations
Expand All @@ -3313,8 +3308,7 @@ const makeEncodingTransformation = (
export const Base64: Schema<string, Uint8Array> = makeEncodingTransformation(
"Base64",
Encoding.decodeBase64,
Encoding.encodeBase64,
(fc) => fc.base64String().map((s) => Either.getOrThrow(Encoding.decodeBase64(s)))
Encoding.encodeBase64
)

/**
Expand All @@ -3324,8 +3318,7 @@ export const Base64: Schema<string, Uint8Array> = makeEncodingTransformation(
export const Base64Url: Schema<string, Uint8Array> = makeEncodingTransformation(
"Base64Url",
Encoding.decodeBase64Url,
Encoding.encodeBase64Url,
(fc) => fc.base64String().map((s) => Either.getOrThrow(Encoding.decodeBase64Url(s)))
Encoding.encodeBase64Url
)

/**
Expand All @@ -3335,8 +3328,7 @@ export const Base64Url: Schema<string, Uint8Array> = makeEncodingTransformation(
export const Hex: Schema<string, Uint8Array> = makeEncodingTransformation(
"Hex",
Encoding.decodeHex,
Encoding.encodeHex,
(fc) => fc.hexaString().map((s) => Either.getOrThrow(Encoding.decodeHex(s)))
Encoding.encodeHex
)

/**
Expand Down
Loading

0 comments on commit a904a73

Please sign in to comment.