Static Typed Languages (Go & Scala)
Relevant source files
The following files were used as context for generating this wiki page:
- internal/translate/gotypes/translator_test.go
- internal/translate/scala/translator.go
- internal/translate/scala/translator_test.go
- internal/translate/sparkscala/translator.go
- internal/translate/sparkscala/translator_test.go
- internal/translate/prepare.go
- internal/translate/databricksscala/translator.go
The Daco CLI provides robust translation capabilities for static typed languages, specifically targeting Go and Scala ecosystems. This feature enables the conversion of JSON Schemas into native language constructs such as Go structs and Scala case classes. These translators ensure type safety and idiomatic code generation, facilitating the integration of data contracts into backend services and data processing pipelines.
Beyond standard language types, the system supports specialized variants like Spark Scala and Databricks Scala. These specialized translators generate StructType definitions used by Apache Spark for schema enforcement in distributed data processing. The translation logic handles complex structures including nested objects, arrays, and topological ordering of dependent types.
Sources: internal/translate/gotypes/translator_test.go, internal/translate/scala/translator.go, internal/translate/sparkscala/translator.go
Translation Architecture
The translation process for static typed languages follows a structured pipeline. It starts with the Prepare function, which extracts metadata and orders the schema. Each language-specific translator then utilizes a TypeResolver to map JSON Schema types to the target language's primitive and complex types.
Data Flow for Static Type Generation
The following diagram illustrates how a raw JSON Schema is transformed into language-specific source code.
The Prepare logic is responsible for traversing $defs in topological order to ensure that dependencies are defined before they are referenced.
Sources: internal/translate/prepare.go:27-58
Go Type Translation
The Go translator converts JSON Schema objects into Go struct definitions. It implements idiomatic Go patterns, such as using PascalCase for exported fields and pointer types for optional fields to support nil values during JSON unmarshaling.
Feature Mapping
| JSON Schema Feature | Go Implementation | Implementation Detail |
|---|---|---|
string | string | Standard string type |
integer | int64 | Uses 64-bit integers for safety |
number | float64 | Double precision floating point |
boolean | bool | Boolean type |
date-time | time.Time | Automatically adds import "time" |
array | []T | Slices of the target type |
object | struct | Extracted as named types |
| Optional Field | *T | Pointers with omitempty JSON tags |
Sources: internal/translate/gotypes/translator_test.go:19-140
Naming Conventions
The Go translator uses specialized casing logic to ensure identifiers are idiomatic. It converts snake_case and camelCase to PascalCase. It also handles common initialisms like "ID", "API", and "URL" correctly (e.g., user_id becomes UserID).
Sources: internal/translate/gotypes/translator_test.go:300-330, internal/translate/prepare.go:175-188
Scala Translation
The Scala translator supports multiple targets: standard Scala case classes, Spark StructType definitions, and Databricks-specific schemas.
Standard Scala Case Classes
Standard Scala translation generates case classes. Required fields are mapped to direct types, while optional fields are wrapped in Scala's Option[T] type.
Sources: internal/translate/scala/translator.go:37-45, internal/translate/scala/translator_test.go:24-33
Spark and Databricks Scala
Spark-specific translators generate StructType arrays used for Spark SQL schemas.
- Spark Scala: Wraps the schema in an
objectextendingSerializableand useslazy valfor theStructTypedefinition. - Databricks Scala: Extends standard Spark translation by adding metadata, such as column comments derived from JSON Schema descriptions.
Type Dependency and Ordering
For static typed languages, the order of definitions is critical to avoid "type not found" errors during compilation. The system extracts nested inline objects and places them into the global definition list.
Topological Resolution Sequence
The Prepare function ensures that if Type A depends on Type B, Type B appears first in the output Defs slice.
Sources: internal/translate/prepare.go:42-55, internal/translate/scala/translator_test.go:127-160
Implementation Details
Required vs Optional Fields
Translators for Go and Scala prioritize required fields by sorting them to the top of the structure/constructor. This is particularly important for Scala case classes where required parameters must precede those with default values (like None).
// internal/translate/scala/translator.go
sortFields := func(fields []translate.Field) {
sort.SliceStable(fields, func(i, j int) bool {
if fields[i].Nullable != fields[j].Nullable {
return !fields[i].Nullable
}
return false
})
}
Sources: internal/translate/scala/translator.go:44-51, internal/translate/pydantic/translator.go:62-69
File Extensions
All static typed language translators provide the appropriate file extension for the generated artifacts:
- Go:
.go - Scala:
.scala(used for standard, Spark, and Databricks variants)
Sources: internal/translate/gotypes/translator_test.go:343-346, internal/translate/scala/translator.go:32-34, internal/translate/sparkscala/translator.go:23-25
Summary
The static typed language translation module provides a bridge between language-agnostic JSON Schemas and type-safe Go/Scala environments. By handling complex type dependencies, topological sorting, and platform-specific requirements (like Spark StructType), it automates the creation of data access layers and schema definitions across different backend and data engineering stacks.