Skip to content

annurdien/populate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PopulateMacros

Swift package providing source macros to quickly generate mock / prefilled factory methods for your model types.

Features

  • @Prefilled attached member macro generates:
    • static func mock() -> Self
    • static func prefilled() -> Self
  • Optional control: @Prefilled(fillOptionals: true) fills optional properties with placeholder values (otherwise nil).
  • Automatic placeholder generation for common scalar types, arrays, and simple custom types.
  • Array auto-fill: non-optional arrays and (when fillOptionals is 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 nil unless you manually compose deeper mocks.

Placeholders

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.

Example

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

Arrays and Nested Types

@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]? }

Installation

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).

Usage Notes

  • 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 @Prefilled or manually compose deep mocks after calling top-level factories.
  • Multi-binding variable declarations (let a: Int, b: Int) are ignored.
  • Computed properties are ignored.

Limitations & Future Ideas

  • Optional custom types remain nil even with fillOptionals: 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")).

Testing

Run the test suite:

swift test

Tests verify macro expansions for scalar, optional, nested, and array scenarios plus complex composition & recursion safety.

Contributing

Issues and PRs welcome. Please include tests for new behavior.

License

MIT

About

Macro to populate swift struct

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages