Skip to content

feat: add DerivedKey rederiveValues generation#28

Merged
WendellXY merged 1 commit into
mainfrom
feat/derivedkey-rederive-values
Jun 16, 2026
Merged

feat: add DerivedKey rederiveValues generation#28
WendellXY merged 1 commit into
mainfrom
feat/derivedkey-rederive-values

Conversation

@WendellXY

Copy link
Copy Markdown
Owner

Summary

  • add derivedFrom defaults to @Codable and @Decodable, letting @DerivedKey omit repeated source names
  • generate fixed rederiveValues() for types with derived properties, mutating for structs and nonmutating for classes
  • tighten DerivedKey diagnostics for let properties, missing sources, invalid defaults, duplicate rederiveValues(), and decode-side ignored sources
  • update README and expansion/runtime coverage for default source overrides and re-derivation

Tests

  • swift test --filter DerivedKey
  • swift test

Notes

  • Subclasses can generate their own rederiveValues() today, but Swift still requires an override if a superclass also declares the same method. Because macros cannot inspect arbitrary superclass members, this inherited-method collision remains a follow-up design question if we want full superclass/child re-derive chaining.

Copilot AI review requested due to automatic review settings June 16, 2026 08:35
@WendellXY WendellXY self-assigned this Jun 16, 2026
@WendellXY WendellXY merged commit be27c3e into main Jun 16, 2026
9 checks passed

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends CodableKit’s derived-property support by adding a type-level default source (derivedFrom:) for @DerivedKey and generating a rederiveValues() method so derived properties can be recomputed after source mutations.

Changes:

  • Add derivedFrom: defaults to @Codable / @Decodable, allowing @DerivedKey to omit repeated from: strings.
  • Generate rederiveValues() for types with derived properties (mutating for structs; nonmutating for classes; throws when needed).
  • Tighten @DerivedKey diagnostics and expand macro + runtime test coverage; update README/docs accordingly.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Tests/DecodableKitTests/CodableMacroTests+derived.swift Adds expansion expectations for default derivedFrom: and generated rederiveValues() in Decodable-only scenarios.
Tests/CodableKitTests/CodableMacroTests+derived.swift Updates Codable expansions and diagnostics tests for new derivedFrom: / rederiveValues() behaviors.
Tests/CodableKitTests/CodableMacroTests+derived.runtime.swift Adds runtime coverage for re-derivation after mutation and decode/encode ignored-source interactions.
Sources/CodableKitMacros/DerivedKeyMacro.swift Removes now-obsolete let+initializer validation from the peer macro.
Sources/CodableKitMacros/CodeGenCore.swift Adds derivedFrom: parsing/storage and enforces new DerivedKey validation + duplicate rederiveValues() diagnostics.
Sources/CodableKitMacros/CodableProperty+Derived.swift Adds helpers for distinguishing omitted vs explicitly-invalid from: arguments.
Sources/CodableKitMacros/CodableMacro.swift Threads derivedFromDefault through generation and emits rederiveValues() when derived properties exist.
Sources/CodableKit/CodableKit.swift Updates public macro signatures/docs and macro attached-member name lists to include new APIs.
README.md Documents default source behavior and rederiveValues(), and updates derived-property examples.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +267 to +272
genRederiveValuesDecl(
from: properties,
modifiers: [accessModifier.witnessSafe],
structureType: structureType,
derivedFromDefault: derivedFromDefault
)
Comment thread README.md
Comment on lines 61 to 64
| Explicit hooks | `@CodableHook(.didDecode)` | Run validation, normalization, or derived-value logic at clear lifecycle stages |
| Transformer pipelines | `@CodableKey(transformer: MyTransformer())` | Compose reusable decode and encode transformations; `DictionaryLookupTransformer` pulls a value out of a decoded dictionary, `liftOptional()` lifts a pipeline to optional input/output, and `onFailure(_:)` observes pipeline errors for logging |
| Derived properties | `@DerivedKey(from: "slots", transformer: MyTransformer())` | Compute typed properties from an already-decoded sibling at the end of `init(from:)` — no coding key, never encoded |
| Derived properties | `@Codable(derivedFrom: "slots")` + `@DerivedKey(transformer: MyTransformer())` | Compute typed properties from an already-decoded sibling at the end of `init(from:)` — no coding key, never encoded |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants