Deloitte Esraa Eid Oct 7, 2025

A Guide to Generating Swift Code with Swift Mustache (Part 1)

Article Summary

Esraa Eid from Deloitte UK reveals how to make Swift code generate Swift code—no low-level languages required. She explores three powerful approaches to transform JSON schemas into production-ready Swift structs.

This hands-on guide walks through generating Swift code programmatically using Swift Mustache, a logic-less templating engine. The article includes a complete working example with code samples, showing how to transform JSON Schema specifications into Swift structs with support for optionals, arrays, and Codable conformance.

Key Takeaways

Critical Insight

Swift Mustache provides a flexible, template-based approach to generate Swift structs from JSON schemas, though it requires learning Mustache syntax and lacks robust debugging for complex scenarios.

Part 2 promises deeper dives into SwiftSyntax and Swift OpenAPI Generator for even more precise compiler-level control over code generation.

About This Article

Problem

Esraa Eid needed to convert JSON Schema specifications into Swift structs. The challenge was keeping type safety and Codable conformance intact while handling optional fields, arrays, and nested properties without writing code manually.

Solution

She built Swift Mustache templates with conditional logic using {{#isArray}}, {{#isOptional}}, and section tags. These templates automatically generate struct definitions from JSON Schema data. The reusable template files keep generation logic separate from the main codebase.

Impact

The template approach made it possible to write files that produce production-ready Swift code, like struct User: Codable with correct type annotations. Mustache syntax handles custom inheritance, loops, and encoding/decoding, so you don't need deep language knowledge to use it.