Skip to main content

Code Generation Customization

Configure cedar4s code generation to match your project's needs.

Plugin Settings

Schema File

build.sbt
// Cedar schema file (required)
cedarSchemaFile := baseDirectory.value / "src" / "main" / "resources" / "schema" / "myapp.cedarschema"

Output Package

build.sbt
// Package for generated Scala code
cedarScalaPackage := "com.example.myapp.cedar"

Generated code is written to sbt's managed sources directory (target/scala-X.Y/src_managed/main/), organized by package structure.

Typed IDs

cedar4s automatically generates typed ID newtypes for every entity in your schema. These provide compile-time type safety with zero runtime overhead. See Typed IDs for details.

Tenant Roots

Override automatic root entity detection:

build.sbt
// Empty = auto-detect from schema (default)
cedarTenantRoots := Set.empty

// Explicit roots
cedarTenantRoots := Set("Organization", "Tenant")

Root entities are those without parents in the entity hierarchy. These affect how loadEntityWithParents traverses the graph.

Smithy Generation

Generate Smithy action enums for use with smithy4s APIs:

build.sbt
// Enable Smithy generation
cedarSmithyNamespace := Some("com.example.api.authz")
cedarSmithyOutputDir := Some(baseDirectory.value / "smithy-specs")

Both settings are required to enable Smithy generation. See Smithy Generation for details.

All Settings Reference

SettingTypeDefaultDescription
cedarSchemaFileFilesrc/main/resources/schema/schema.cedarschemaPath to Cedar schema
cedarScalaPackageString"cedar.policies"Package for generated Scala
cedarTenantRootsSet[String]Set.emptyOverride root detection
cedarSmithyNamespaceOption[String]NoneSmithy namespace
cedarSmithyOutputDirOption[File]NoneSmithy output directory

sbt Task

TaskDescription
cedarCodegenGenerate Scala code from Cedar schema

The task runs automatically during compile. To run manually:

sbt cedarCodegen

Multi-Module Projects

For projects with separate API and implementation modules:

build.sbt
lazy val cedarApi = project
.enablePlugins(Cedar4sPlugin)
.settings(
cedarSchemaFile := file("cedar/schema.cedarschema"),
cedarScalaPackage := "com.example.cedar"
)

lazy val app = project
.dependsOn(cedarApi)
.settings(
libraryDependencies += "io.github.devnico" %% "cedar4s-core" % "@VERSION@"
)

Environment-Specific Configuration

Use sbt settings for different schemas per project:

build.sbt
val cedarSettings = Seq(
cedarSchemaFile := file("src/main/cedar/schema.cedarschema"),
cedarScalaPackage := "com.example.cedar"
)

val testCedarSettings = Seq(
cedarSchemaFile := file("src/test/cedar/test-schema.cedarschema"),
cedarScalaPackage := "com.example.cedar.test"
)

lazy val app = project
.enablePlugins(Cedar4sPlugin)
.settings(cedarSettings)

lazy val testSupport = project
.enablePlugins(Cedar4sPlugin)
.settings(testCedarSettings)