Skip to content

Add RandomGenerable and the opt-in @RandomGenerable derivation macro (Derive trait) - #14

Merged
ibrahimkteish merged 1 commit into
mainfrom
feature/derive-macro
Jul 23, 2026
Merged

ibrahimkteish merged 1 commit into
mainfrom
feature/derive-macro

Conversation

@ibrahimkteish

@ibrahimkteish ibrahimkteish commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Summary

Adds generator derivation for user-defined types, gated behind a new opt-in Derive package trait so the swift-syntax build cost is only paid by consumers who want the macro.

  • RandomGenerable protocol (always available, no trait needed): types expose a canonical static var generator. Standard types ship pre-conformed with simple, documented defaults — fixed-width integers (full range), Double/Float (0...1), Bool, String (8 alphanumerics), Character (ASCII letter), Optional (nil half the time), Array (0–10 elements).
  • @RandomGenerable macro (requires the Derive trait): derives a conformance for structs (through the memberwise initializer) and enums (uniform case selection, associated values included). @Gen(...) overrides the generator for a single stored property. Derived generators are ordinary generators — combinators and seeded RNGs apply.
  • With the trait off (the default), swift-syntax is never compiled; the core library remains a zero-build-cost dependency. It is still fetched at resolution time (SE-0450 applies traits after resolution).
@RandomGenerable
struct User {
    @Gen(IntGenerator(in: 18...99)) var age: Int
    var name: String        // 8 random alphanumerics
    var isAdmin: Bool
    var nickname: String?   // nil half the time
}

let team = User.generator.array(5).run()

Consumers opt in from their manifest (Swift 6.1+), or in Xcode 26.4+ via package-trait settings:

.package(url: "https://github.com/ibrahimkteish/SwiftRandomKit.git", from: "2.2.0", traits: ["Derive"])

Implementation notes

  • swift-tools-version 6.0 → 6.1 (required for traits). Older toolchains keep resolving 2.1.0; SwiftPM skips manifests it cannot read.
  • The macro target and its swift-syntax products hang off condition: .when(traits: ["Derive"]) edges, so nothing macro-related enters a trait-off consumer's build graph.
  • Diagnostics for unsupported shapes: classes/actors, generic types, missing type annotations, @Gen on multi-bindings or non-properties, case-less enums.
  • Macro expansion tests use MacroTesting inline snapshots in a dedicated SwiftRandomKitMacrosTests target; runtime behavior (determinism, @Gen ranges, enum coverage, nesting, combinators) is covered in the main test target behind #if Derive.
  • CI runs the suite in both trait configurations. Trait-enabled test runs pass --disable-experimental-prebuilts: MacroTesting links swift-syntax as a library, which is incompatible with SwiftPM's prebuilt swift-syntax binaries.

Test plan

  • swift test (trait off): 187 tests, 38 suites — pass
  • swift test --traits Derive --disable-experimental-prebuilts: 208 tests, 40 suites — pass
  • swift test -c release: pass
  • Example target runs in both configurations (derive section prints only with the trait)
  • First CI run validates the Swift 6.1 pin on macos-15 and the prebuilts flag on SwiftPM 6.1

Release intent: 2.2.0 (additive; changelog entry is staged as Unreleased — date/tag on release).

…acro

- RandomGenerable protocol with default generators for standard types
  (always available, no trait required)
- New Derive package trait (off by default): enables the @RandomGenerable
  extension macro for structs and enums, plus @gen per-property overrides;
  swift-syntax is only built by consumers who enable the trait
- swift-tools-version 6.0 -> 6.1 (required for package traits)
- Macro expansion tests via MacroTesting in a dedicated test target;
  runtime tests for derived generators; both trait configurations in CI
@ibrahimkteish
ibrahimkteish merged commit dfbc083 into main Jul 23, 2026
4 checks passed
@ibrahimkteish
ibrahimkteish deleted the feature/derive-macro branch July 23, 2026 09:12
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.

1 participant