Skip to content

Make FieldValue.list homogeneous by construction #481

Closed

Description

@leogdion

Problem

FieldValue.list([FieldValue]) (Sources/MistKit/Models/FieldValues/FieldValue.swift:42) can hold mixed element types and arbitrary nesting. CloudKit cannot represent either.

The CloudKit schema grammar is LIST<primitive-type> (.claude/docs/cloudkit-schema-reference.md:48-57): a list is homogeneous, and its element must be a scalar primitive. There is no LIST<LIST<…>>.

So both of these compile, and both are rejected by the server with BAD_REQUEST:

.list([.string("a"), .int64(1)])   // heterogeneous
.list([.list([.string("a")])])     // nested

The domain type is wider than the API it models, and the gap is only discovered on a live write.

The invariant is already assumed in three places

Each of these works around the missing type guarantee rather than relying on it:

  1. FilterBuilder.cloudKitListType(for:) (Sources/MistKit/Models/Queries/FilterBuilder/FilterBuilder.swift:170-203) derives the *_LIST request tag from values.first only — remaining elements are assumed to match, unchecked. It also returns nil for .list, with the comment "Nested lists aren't valid in IN/NOT_IN; omit the type and let CloudKit reject." The invariant is stated in a comment and enforcement is deferred to the server.

  2. Components.Schemas.FieldValueRequest.init(list:) (Sources/MistKit/OpenAPI/Components/Components.Schemas.FieldValueRequest.swift:130-133) sends lists untagged, so a homogeneous [.date] or [.bytes] list loses the very disambiguation the scalar path is careful to apply (issue FieldValueRequest can't tag scalar TIMESTAMP — Date/Time writes fail with BAD_REQUEST #375). Elements go out shape-only (Components.Schemas.ListValuePayload.swift:45-58).

  3. Response LIST tag validation is container-level only — FieldValue+Components.swift:154 notes element types "stay lenient," because there is nothing in the type to validate them against.

Proposal

Replace the payload with a homogeneous-by-construction enum:

public enum ListValue: Codable, Equatable, Sendable {
  case strings([String])
  case int64s([Int])
  case doubles([Double])
  case bytes([Data])
  case dates([Date])
  case locations([Location])
  case references([Reference])
  case assets([Asset])
}

// FieldValue
case list(ListValue)

Heterogeneity becomes unrepresentable, and nesting disappears because there is no case lists. The element-type tag becomes total and exact rather than first-element guesswork, which lets FilterBuilder drop its unchecked assumption and lets the request path finally tag TIMESTAMP_LIST / BYTES_LIST correctly.

This matches how the codebase already reasons about the wire: the default-free switches at every conversion boundary exist so the compiler catches unclassified cases. This applies the same instinct one level up.

Why not deprecate the old case

@available(*, deprecated) on an enum case fires on construction but not usefully on pattern matching, so every consumer reading a list keeps compiling silently and keeps getting the old shape. Worse, a deprecated case still counts toward exhaustiveness — we would carry both case list([FieldValue]) and case list(ListValue) under different labels, and every default-free switch (FieldValueRequest.init(from:), ListValuePayload.init(from:), FilterBuilder.cloudKitListType(for:), the response conversions) would have to handle both. That doubles the surface of the mechanism whose exhaustiveness is the load-bearing safety property, to soften a break in a pre-1.0 beta.

It is also the wrong use of the annotation per .claude/memory/feedback_available_semantics.md: deprecated is reserved for symbols that have been replaced. A heterogeneous .list was never a working API — keeping it functional-but-deprecated would tell users "this still works, tidy up later" when a mixed list has never worked against CloudKit and never will.

Break it cleanly. We are pre-1.0; this is the window the beta line exists for. The compiler drives the migration: every construction site and every switch becomes a build error, which is a better experience than an ignorable warning, because the fix is genuinely different code rather than a rename.

Migration plan

  • Add ListValue and change FieldValue.list to carry it
  • Update the conversion boundaries: FieldValue+Components+List.swift, Components.Schemas.ListValuePayload.swift, Components.Schemas.FieldValueRequest.swift
  • Make FilterBuilder.cloudKitListType(for:) total on ListValue (no more values.first, no nil nesting escape hatch)
  • Tag list requests with the correct *_LIST type, closing the TIMESTAMP_LIST / BYTES_LIST gap noted above
  • Deprecate the accessor, not the case: keep listValue: [FieldValue]? (FieldValue+Convenience.swift:148) working by flattening ListValue back to [FieldValue], marked @available(*, deprecated) alongside a new typed accessor. This is a genuine replaced-symbol deprecation, and it lets read-only consumers (the MistDemo formatters) compile untouched while write paths migrate.
  • Add static factory shims for ergonomic continuity where the call site need not change, e.g. static func list(_ values: [String]) -> FieldValue
  • Update Tests/MistKitTests/Models/FieldValues/FieldValueConversionTests+Lists.swift — :44 ("mixed types") and :83 ("nested list") currently assert the behavior being removed and must become negative/compile-time cases
  • Update Examples: Article+MistKit.swift:119, Feed+MistKit.swift:134 (one-liners), RecordInfo+Parsing.swift:160, FieldValueFormatter.swift:57,85
  • Update the FieldValue architecture section of CLAUDE.md
  • ReleaseNotes.md migration note with before/after for common shapes

Open design questions

  1. Empty lists. .strings([]) and .int64s([]) are distinct values that encode identically on the wire, and the decoder cannot recover an element type from []. Either add a separate case empty (clean Equatable, but every switch grows a case) or accept the asymmetry and document it. Affects Equatable and round-trip tests.
  2. Naming. ListValue collides conceptually with the generated Components.Schemas.ListValue / ListValuePayload. Consider FieldValue.List or ElementList to keep the domain/wire distinction legible.
  3. ASSETID. Response tag ASSETID shares AssetValue with ASSET (FieldValue+ResponseTypeTag.swift); confirm no separate list case is needed.

Notes

Timing matters: the repo is at v1.0.0-beta.5, so a source-breaking change to a public enum is cheap now and expensive after 1.0.

Activity

  1. added this to the v1.0.0 milestone on Sep 9, 2026
  2. added
    enhancement
    architecture
    library-api
    type-safety
    on Sep 9, 2026
  3. added a commit that references this issue on Sep 9, 2026
  4. leogdion commented on Sep 9, 2026

    @leogdion
    MemberAuthor

    Live probe: empty list wire shape (2026-09-09)

    Against iCloud.com.brightdigit.MistDemo / development / public, S2S. Note.tags added as LIST<STRING> in Console.

    Write Create response Lookup
    tags: { value: [] } present: "type":"STRING_LIST","value":[] same
    tags: { value: ["a","b"] } present: "type":"STRING_LIST","value":["a","b"] same

    Implications for #481:

    1. Empty lists are writable and round-trip as present [] — not absent, not rejected.
    2. Responses carry the granular *_LIST family (STRING_LIST), not the flat LIST currently modeled on FieldValueResponse in openapi.yaml. That mismatch is why MistKit currently fails to decode any list response (Cannot initialize _typePayload from invalid String value STRING_LIST).
    3. Because empty [] still carries STRING_LIST, the element type is recoverable on read — so case empty is not required for honest decode if the response type enum is corrected to the *_LIST family. Still useful as a write-side construction for callers who don't care about element type, but not forced by the wire.

    Harness: .claude/probes/481-empty-list/.

  5. leogdion commented on Sep 9, 2026

    @leogdion
    MemberAuthor

    Implementation landed on branch 481-fieldvalue-homogeneous-list (one PR)

    Domain: each FieldValue kind carries Arity<T> (.value / .list). No case list([FieldValue]). Empty lists are typed (.string(.list([]))).

    Migration: deprecated scalar factories only (string(_: String) → .value). No heterogeneous list polyfill.

    Wire: FieldValueResponse.type OpenAPI enum aligned to live *_LIST family; list record writes tagged; MistDemo StringListRoundTripPhase exercises Note.tags on public + private.

    Research: .claude/docs/research/481-homogeneous-list-without-duplicate-enums.md

  6. added 4 commits that reference this issue on Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    architectureenhancementlibrary-apitype-safety

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions