Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-variant-schema-default-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Separate the default `VariantSchema` cache from named variant entries.
14 changes: 9 additions & 5 deletions packages/effect/src/unstable/schema/VariantSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as Struct_ from "../../Struct.ts"
export const TypeId = "~effect/schema/VariantSchema"

const cacheSymbol = Symbol.for(`${TypeId}/cache`)
const defaultCacheSymbol = Symbol.for(`${TypeId}/defaultCache`)

/**
* Pipeable container of schema fields that can be extracted into per-variant
Expand All @@ -38,6 +39,8 @@ export interface Struct<in out A extends Field.Fields> extends Pipeable {
readonly [TypeId]: A
/** @internal */
[cacheSymbol]?: Record<string, Schema.Top>
/** @internal */
[defaultCacheSymbol]?: Record<string, Schema.Top>
}

/**
Expand Down Expand Up @@ -214,10 +217,11 @@ const extract: {
readonly isDefault?: boolean | undefined
}
): Extract<V, A> => {
const cache = self[cacheSymbol] ?? (self[cacheSymbol] = Object.create(null))
const cacheKey = options?.isDefault === true ? "__default" : variant
if (Object.hasOwn(cache, cacheKey)) {
return cache[cacheKey] as any
const cache = options?.isDefault === true
? self[defaultCacheSymbol] ?? (self[defaultCacheSymbol] = Object.create(null))
: self[cacheSymbol] ?? (self[cacheSymbol] = Object.create(null))
if (Object.hasOwn(cache, variant)) {
return cache[variant] as any
}
const fields: Record<string, any> = {}
for (const key of Object.keys(self[TypeId])) {
Expand All @@ -243,7 +247,7 @@ const extract: {
}
}
const schema = Schema.Struct(fields)
cache[cacheKey] = schema
cache[variant] = schema
return schema as any
}
)
Expand Down
19 changes: 19 additions & 0 deletions packages/effect/test/unstable/schema/VariantSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ describe("VariantSchema", () => {

assert.deepStrictEqual(Object.keys(Test.extract(struct, "a").fields), ["value"])
})

it("does not collide the __default variant with the default-schema cache entry", () => {
Comment thread
pullfrog[bot] marked this conversation as resolved.
const Test = VariantSchema.make({ variants: ["a", "__default"], defaultVariant: "a" })
const defaultFirst = Test.Struct({
value: Test.Field({ a: Schema.String, __default: Schema.Number })
})

Test.extract(defaultFirst, "a")

assert.strictEqual(Test.extract(defaultFirst, "__default").fields.value, Schema.Number)

const namedFirst = Test.Struct({
value: Test.Field({ a: Schema.String, __default: Schema.Number })
})

Test.extract(namedFirst, "__default")

assert.strictEqual(Test.extract(namedFirst, "a").fields.value, Schema.String)
})
})

describe("Model", () => {
Expand Down
Loading