generate model
Create a Codable model, optionally inferred from a JSON sample.
Creates a Codable struct. Without a sample, it’s an empty skeleton. With --from, fields are inferred from a real JSON payload.
Synopsis
Section titled “Synopsis”swiftspawn generate model <Name> [--from <path-or-url>] [--in <feature>] [--dry-run] [--force] [--quiet] [--verbose]Description
Section titled “Description”generate model is meant to be the fastest path from an example JSON response to a Swift type that decodes it.
Without --from, it produces an empty Codable struct with a comment placeholder, ready for you to fill in.
With --from <path-or-url>, the generator:
- Loads the JSON sample (file path or HTTP URL).
- Walks every field; infers its Swift type (
Int,Double,String,Bool, arrays, nested structs). - Marks fields
Optionalif they’renullin the sample. - Converts
snake_caseJSON keys tocamelCaseSwift properties and emits aCodingKeysenum.
A single sample only sees one shape of the response. If the API returns null for a field that’s non-null in your sample, the generated type will fail to decode at runtime; open the file and add ? to the affected fields.
Examples
Section titled “Examples”swiftspawn generate model Genreswiftspawn generate model Movie --from sample.json --in Moviesswiftspawn generate model User --from https://api.example.com/users/1Generated files
Section titled “Generated files”With --in <feature>:
Features/<Feature>/Models/<Name>.swiftWithout --in:
Models/<Name>.swiftSide effects
Section titled “Side effects”None.
Options
Section titled “Options”| Flag | Description |
|---|---|
--from <path-or-url> | Path or URL to a JSON sample to infer the schema from. |
--in <feature> | Feature folder. Defaults to top-level Models/. Bare in <feature> also works. |
--dry-run | Preview without writing. |
--force | Overwrite existing file. |
Exit codes
Section titled “Exit codes”| Code | Meaning |
|---|---|
| 2 | Invalid name. |
| 5 | Not in a swiftspawn project. |
| 9 | File not found at --from path. |
| 10 | Failed to fetch URL at --from. |
| 11 | Malformed JSON at --from. |
See also
Section titled “See also”generate servicerecipe crudbundlesgenerate model --frominto a full pattern.