Swift package providing source macros to quickly generate mock / prefilled factory methods for your model types.
@Prefilledattached member macro generates:static func mock() -> Selfstatic func prefilled() -> Self
- Optional control:
@Prefilled(fillOptionals: true)fills optional properties with placeholder values (otherwisenil). - Automatic placeholder generation for common scalar types, arrays, and simple custom types.
- Array auto-fill: non-optional arrays and (when
fillOptionalsis true) optional arrays are populated with 3 placeholder elements. - Safe handling of self-recursive arrays (e.g.
[MyType]?) – results in an empty array to avoid infinite recursion. - Conservative with optional custom (non-primitive) types: leaves them
nilunless you manually compose deeper mocks.
| Type Category | Placeholder (non-optional or fillOptionals: true) |
|---|---|
String |
"sample" |
| Integer types | 0 |
| Float types | 0.0 |
Bool |
false |
| Arrays | 3 elements (scalar placeholders or Element.mock()) |
| Custom types | .init() when non-optional, else nil if optional |
Optional properties become nil unless fillOptionals: true.
import PopulateMacros
@Prefilled
struct User { let id: Int; let name: String; let nickname: String? }
@Prefilled(fillOptionals: true)
struct Profile { let displayName: String?; let isActive: Bool? }
let u1 = User.mock() // nickname == nil
let p1 = Profile.prefilled() // optionals filled@Prefilled
struct LineItem { let sku: String; let qty: Int; let note: String? }
@Prefilled(fillOptionals: true)
struct Order {
let id: String
let items: [LineItem] // -> 3 LineItem.mock() elements
let tags: [String]? // -> ["sample","sample","sample"]
let customer: Customer? // remains nil (custom optional)
}
@Prefilled(fillOptionals: true)
struct Customer { let name: String?; let emails: [String]? }Add to Package.swift dependencies:
.package(url: "https://github.com/annurdien/PopulateMacros.git", from: "0.1.0")Then add PopulateMacros as a dependency for your target.
Ensure you are using a Swift toolchain that supports macros (Swift 5.9+ / swift-syntax 600.x).
- Annotate each struct / class you want factories for with
@Prefilled. - To generate placeholders for optionals, pass
fillOptionals: true. - For nested mocks, either annotate nested types with
@Prefilledor manually compose deep mocks after calling top-level factories. - Multi-binding variable declarations (
let a: Int, b: Int) are ignored. - Computed properties are ignored.
- Optional custom types remain
nileven withfillOptionals: true(avoid assuming a default initializer). Possible future option:fillCustomOptionals. - Custom arrays rely on
Element.mock(); ensure those element types are also annotated. - No configuration yet for array element count (fixed at 3). Could add:
@Prefilled(arrayCount: 5). - No per-property override (planned:
@MockValue("hard-coded")).
Run the test suite:
swift testTests verify macro expansions for scalar, optional, nested, and array scenarios plus complex composition & recursion safety.
Issues and PRs welcome. Please include tests for new behavior.
MIT