Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Mustache Template-Based Generation Example

Java code generation using Mustache templates and the FHIR R4 specification.

Overview

This example demonstrates how to generate Java classes from FHIR R4 using template-based code generation. It includes:

  • Template-driven Java class generation with Mustache
  • Automatic resource and complex type modeling
  • Utility class generation for common operations
  • Post-generation hooks for code formatting and testing
  • Custom name transformations for Java conventions

Setup

Generate Java Types

From the project root:

cd codegen
bun install
bun run examples/mustache/mustache-java-r4-gen.ts

This will output to ./examples/mustache/mustache-java-r4-output/

Build Java Project

cd examples/mustache/mustache-java-r4-output
mvn clean package

Configuration

The generation is configured via java/config.json. Key settings include:

Type Mapping: Maps FHIR primitive types to Java types:

  • booleanBoolean
  • dateString
  • dateTimeOffsetDateTime
  • decimalBigDecimal
  • integerInteger

Name Transformations: Applies Java naming conventions:

  • Enum values: <=LESS_OR_EQUAL
  • Types: Add DTO suffix
  • Fields: Rename reserved words

Post-Generation Hooks:

  • Run Spotless for code formatting
  • Execute Maven tests

Template Structure

Templates

Located in java/templates/:

  • model/resource_or_complex_type.mustache - Main template for resources and complex types
  • model/utils/*.mustache - Utility class templates
  • annotated_type.mustache - Type with annotations
  • plain_type.mustache - Simple type definition
  • primitive_wrapped_plain_type.mustache - Wrapped primitive types

Static Files

Located in java/static/:

  • pom.xml - Maven project configuration
  • model/src/ - Base Java project structure

Using Generated Types

Create a Resource

Patient patient = new Patient()
    .setResourceType("Patient")
    .setId("patient-1")
    .setGender("male");

Add Extensions

patient.getExtension().add(new Extension()
    .setUrl("http://example.com/extension")
    .setValue(new StringType("value")));

Serialization

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(patient);
Patient deserialized = mapper.readValue(json, Patient.class);

Customization

Change Output Package

Edit java/config.json and update the package property in renderings:

"properties": {
  "package": "com.mycompany.fhir.models"
}

Filter Specific Resources

Customize which resources to generate via whitelist/blacklist in config:

"filters": {
  "resource": {
    "whitelist": ["Patient", "Observation"]
  }
}

Add Post-Generation Steps

Add hooks to run additional tools after generation:

"hooks": {
  "afterGenerate": [
    {"cmd": "mvn", "args": ["clean", "compile"]}
  ]
}

Template Variables

Templates have access to the following context:

TypeViewModel:

  • name - Resource or type name
  • saveName - Name safe for use as Java class name
  • description - FHIR description
  • baseType - Parent type if any
  • fields - Array of fields

FieldViewModel:

  • name - Field name
  • saveName - Safe field name
  • type - Field type
  • isArray - Whether field is a list
  • required - Whether field is required
  • description - Field description

Special Variables:

  • meta.timestamp - Generation timestamp
  • meta.generator - Generator identification
  • properties - Custom properties from config

Next Steps