Scala 3 Data Transformation Library: Automating Data Transformations with ducktape

Scalac
5 min readFeb 16, 2024

--

In the ever-evolving landscape of software development, there are tasks that, while necessary, often lack the thrill of innovation. One such task involves managing data transformations, a critical aspect across various domains. While JSON APIs serve as a prime example where such transformations are crucial, the focus of this discussion transcends JSON and centers on ducktape — a Scala 3 data transformation micro-library designed to revolutionize how developers approach automatic and configurable data transformations. This article takes you through the journey of ducktape, showcasing its power to simplify data model conversions and enhance the efficiency of development workflows.

Disclaimer: Blog first published on https://scalac.io/blog

The Challenge of Data Transformations

In software development, working with data transformations is an everyday necessity. Regardless of the data format or application domain, mapping and converting data structures can be laborious and error-prone. Developers typically need help with the complexities of model conversions between different (often pretty similar to each other) data models, diverting their attention from core business logic and innovation.

Unveiling ducktape: A Paradigm Shift

Not to be confused with its adhesive counterpart, ducktape emerges as a game-changing solution to the intricate web of data transformations. At its core, the libaempowers scala developers to convert data models between different formats seamlessly. This transformation-centric micro library introduces a novel approach to simpler domain modeling, prioritizing automatic data conversion over manual coding. The essence of ducktape lies in its ability to shift the focus away from mundane data-handling tasks, allowing scala developers to dedicate more energy to innovative endeavors.

Key Principles of ducktape

Central Role of Transformers

Transformers lie at the heart of ducktape’s philosophy. These components serve as the bridge between data structures, enabling fluid transitions from one format to another. Within ducktape’s context, a transformer guarantees successful transformations during runtime, possibly encountering compile-time failures if necessary transformations are unavailable. These transformers, designed as type classes, facilitate intricate transformation chains for complex scenarios.

Exclusive Scala 3 Compatibility

Ducktape’s niche lies within the Scala 3 programming language. This exclusivity opens doors to advanced macro features inherent in Scala 3, elevating the library’s implementation. Leveraging Scala 3’s robust type system and macro capabilities, ducktape achieves unparalleled transformation seamlessness (or a little bit paralleled by chimney, which just had its first Scala 3 release).

Automated Derivation: The Jewel in the Crown

A standout feature of ducktape is its prowess in automating the derivation of transformers for case classes and enums. This automated magic unfolds by scrutinizing source and target case class composition and types, generating the requisite transformation logic.

Reimagining Data Transformation Workflows

Ducktape orchestrates a transformational shift in the conventional data workflow, simplifying its multistep journey:

  1. Defining Data Models: ducktape encourages developers to define data models using case classes and enums. These models represent the core structures of both the domain and the API while not shying away from representing different layers of their apps with models made specifically for that particular layer.
  2. Automated Derivation: ducktape takes the reins of transformer derivation, sparing scala developers from manually crafting transformation logic.
  3. Customizable Transformations: Developers enjoy the flexibility of tailoring transformations through various configuration options. These include constants, computed values, field renaming, and multiple others.
  4. Validation and Error Handling: ducktape assumes responsibility for validation and error reporting during transformations. It aggregates errors, presenting developers with a comprehensive overview of areas needing attention.
  5. Integration of Business Logic: Developers can implement business logic once data models are in place and transformers are derived. The streamlined API development facilitated by ducktape paves the way for more effective functionality realization.

A Practical Glimpse: Transforming Conference Data

To exemplify ducktape’s prowess, let’s traverse a real-world scenario. Imagine constructing a REST API for conference management. Employing case classes, ducktape automates the conversion between domain and API representations:

case class CreateConference(name: ConfName, dateSpan: DateSpan, city: CityName)
case class Conference(name: ConfName, dateSpan: DateSpan, city: CityName)

Note how custom new types, constructed using Scala 3’s opaque type feature, bolster validation and guarantee accuracy.

Empowering Development with ducktape

Ducktape radically simplifies the transformation endeavor. By harnessing automated derivation, the Scala 3 data transformation library conjures transformation logic on our behalf. Consider the example above — transforming `CreateConference` instances into `Conference` instances is now an effortless feat:

val createConf: CreateConference = ... // API request body
val conf: Conference = createConf.to[Conference]

Behind the scenes, ducktape orchestrates field mapping and executes vital validations. While automatic derivation champions most scenarios, intricate use cases may necessitate additional configurations. Here, ducktape’s `into` and `via` methods come into play, elegantly handling such complexities.

Advantages and Cautions

Upsides

  • Enhanced Efficiency: Ducktape eliminates the tedium of transformation tasks, optimizing resource allocation.
  • Streamlined Simplicity: Automatic derivation negates the need for extensive boilerplate code, fostering cleaner, more maintainable codebases.
  • Customization Capabilities: Ducktape’s configurability empowers scala developers to tackle unique cases unaddressed by automatic derivation.

Points of Note

  • Learning Curve: While ducktape simplifies many dimensions of API development, a grasp of its principles and mechanics is imperative.
  • Handling Edge Cases: While automated derivation caters to most scenarios, intricate transformations or edge cases might necessitate manual intervention.

So, where can you learn more?

First, check out the video recording of the creator of Ducktape, Aleksander Rainko, from the SF Scala meetup. This video will give you valuable insights and details about the library’s capabilities.

But that’s not all! Head over to Ducktape’s GitHub page to explore the library’s documentation and get started with quicker data transformations.

https://github.com/arainko/ducktape

Don’t let data transformations slow you down. Let ducktape support you in automating your data conversions and making your development process more manageable.

Scala 3 data transformation library | In Closing

Ducktape emerges as an indispensable asset for developers yearning to automate and streamline data transformation complexities. By zeroing in on automatic and customizable transformations, the Scala 3 data transformation library unburdens scala developers from menial tasks, allowing them to channel their energy into innovative problem-solving and application enhancement. With its integration into Scala 3 and harnessing of robust macro features, ducktape introduces a modern approach to API development, revolutionizing the developer’s engagement with data models, irrespective of their format.

Don’t forget to watch the video, visit the GitHub page, and unlock the power of ducktape with Scalac expertise today!

Links

--

--

Scalac

Scalac is a web & software development company with 122 people including Backend, Frontend, DevOps, Machine Learning, Data Engineers, QA’s and UX/UI designers