feat: add DerivedKey rederiveValues generation#28
Merged
Conversation
Contributor
There was a problem hiding this comment.
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@DerivedKeyto omit repeatedfrom:strings. - Generate
rederiveValues()for types with derived properties (mutating for structs; nonmutating for classes;throwswhen needed). - Tighten
@DerivedKeydiagnostics 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 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 | | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
derivedFromdefaults to@Codableand@Decodable, letting@DerivedKeyomit repeated source namesrederiveValues()for types with derived properties, mutating for structs and nonmutating for classesletproperties, missing sources, invalid defaults, duplicaterederiveValues(), and decode-side ignored sourcesTests
swift test --filter DerivedKeyswift testNotes
rederiveValues()today, but Swift still requires anoverrideif 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.