Skip to content

zio-blocks-codegen: Generic Scala code generation IR and emitter #1142

@987Nabil

Description

@987Nabil

Summary

Add a code generation module (zio-blocks-codegen) providing a generic IR (intermediate representation) ADT for Scala data types and a code emitter that generates Scala source files from it.

Motivation

zio-http currently contains zio-http-gen, which generates Scala code from OpenAPI specifications. However, the core concept — an IR representing Scala types (case classes, sealed traits, objects, enums) and emitting formatted source code — is not HTTP-specific. Multiple generators (OpenAPI → Scala, Smithy → Scala, Protobuf → Scala, JSON Schema → Scala) share the same output concern.

By extracting this into zio-blocks, we enable:

  • Reuse across generators (OpenAPI, Smithy, Protobuf, etc.)
  • A single, well-tested code emitter
  • Consistent code style across all generated code
  • Other ZIO ecosystem libraries to leverage codegen without depending on zio-http

Scope

  • IR ADT: Type-safe representation of Scala code structures:
    • File, Package, Import
    • CaseClass, SealedTrait, Object, Enum
    • Field, Method, TypeRef, Annotation
    • Newtype (zio-prelude wrapper types)
  • Emitter: IR → formatted Scala source string
  • Scalafmt integration (optional): Format emitted code
  • No domain-specific logic: The IR is generic — OpenAPI-to-IR conversion stays in zio-http (or zio-blocks-openapi), Smithy-to-IR stays in zio-blocks-smithy, etc.

Related

  • Add OpenAPI micro-library #918 — OpenAPI micro-library (consumer of codegen)
  • zio-http zio-http-gen module — current implementation to extract from
  • zio-http 4.0 migration: codegen moves from zio-httpzio-blocks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions