### Typed FHIR Code Values Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Explains the `Enumeration` class for handling FHIR codes bound to ValueSets, using generated enums for type safety and providing examples of creating and using enumerations. ```APIDOC ## `Enumeration` — Typed FHIR Code Values FHIR codes bound to a ValueSet are typed as `Enumeration` where `T` is a generated enum. This wraps the enum value together with FHIR element metadata (`id`, `extension`). Each generated enum has `getCode()`, `getSystem()`, `getDisplay()`, and `fromCode()`. ```kotlin import dev.ohs.fhir.model.r4.Enumeration import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender // Creating an Enumeration from an enum constant val gender = Enumeration(value = AdministrativeGender.Female) println(gender.value?.getCode()) // female println(gender.value?.getSystem()) // http://hl7.org/fhir/administrative-gender println(gender.value?.getDisplay()) // Female // Round-tripping from a code string val fromCode = AdministrativeGender.fromCode("male") println(fromCode) // male (toString() returns the code) check(fromCode == AdministrativeGender.Male) // Using in resource field access after deserialization val patient: Patient = FhirR4Json().decodeFromString(patientJson) as Patient when (patient.gender?.value) { AdministrativeGender.Male -> println("Patient is male") AdministrativeGender.Female -> println("Patient is female") AdministrativeGender.Other -> println("Patient: other gender") AdministrativeGender.Unknown -> println("Patient: unknown gender") null -> println("Gender not recorded") } ``` ``` -------------------------------- ### Verify GPG Signing Configuration Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Check if your GPG signing configuration is correctly set up for publishing artifacts. This command verifies that Gradle can access and use your signing keys. ```shell ./gradlew :fhir-model:checkSigningConfiguration ``` -------------------------------- ### Resource Base Class Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Explains the `Resource` abstract base class, its common properties, and the usage of `toBuilder()` and `build()` for resource manipulation. ```APIDOC ## `Resource` — Abstract Base Class `Resource` is the abstract Kotlin base class for all FHIR resources. Every generated resource class (e.g. `Patient`, `Observation`, `Bundle`) extends it and inherits `id`, `meta`, `implicitRules`, and `language`. Every resource also exposes `toBuilder()` to get a mutable `Builder` and `build()` to construct an immutable instance. ```kotlin import dev.ohs.fhir.model.r4.Resource fun printResource(resource: Resource) { println("Type : ${resource::class.simpleName}") println("ID : ${resource.id}") println("Lang : ${resource.language?.value}") } // Polymorphic dispatch after deserialization val json = FhirR4Json() val resource = json.decodeFromString("{ \"resourceType\": \"Patient\", \"id\": \"p-1\" }") when (resource) { is Patient -> println("Patient: ${resource.name.firstOrNull()?.family}") is Observation -> println("Observation: ${resource.status}") is Bundle -> println("Bundle entries: ${resource.entry.size}") else -> println("Other: ${resource::class.simpleName}") } ``` ``` -------------------------------- ### Working with the Resource Base Class Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Utilize the `Resource` abstract base class for polymorphic dispatch and access common properties like `id`, `meta`, `implicitRules`, and `language`. Use `toBuilder()` and `build()` for mutable instance manipulation. ```kotlin import dev.ohs.fhir.model.r4.Resource import dev.ohs.fhir.model.r4.Patient import dev.ohs.fhir.model.r4.Observation import dev.ohs.fhir.model.r4.Bundle fun printResource(resource: Resource) { println("Type : ${resource::class.simpleName}") println("ID : ${resource.id}") println("Lang : ${resource.language?.value}") } // Polymorphic dispatch after deserialization val json = FhirR4Json() val resource = json.decodeFromString("{ \"resourceType\": \"Patient\", \"id\": \"p-1\" }") when (resource) { is Patient -> println("Patient: ${resource.name.firstOrNull()?.family}") is Observation -> println("Observation: ${resource.status}") is Bundle -> println("Bundle entries: ${resource.entry.size}") else -> println("Other: ${resource::class.simpleName}") } ``` -------------------------------- ### JSON Serialization Entry Points Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Demonstrates how to use FhirR4Json, FhirR4bJson, and FhirR5Json for deserializing and serializing FHIR resources to and from JSON. ```APIDOC ## `FhirR4Json` / `FhirR4bJson` / `FhirR5Json` — JSON Serialization Entry Points Each FHIR version exposes a dedicated JSON codec class (`FhirR4Json`, `FhirR4bJson`, `FhirR5Json`). They wrap `kotlinx.serialization`'s `Json` object with the correct polymorphic serializers module and support an optional `JsonBuilder` initializer for further customization. ```kotlin import dev.ohs.fhir.model.r4.FhirR4Json import dev.ohs.fhir.model.r4.Patient import dev.ohs.fhir.model.r4.Resource // Default configuration val json = FhirR4Json() // Custom configuration (e.g. ignore unknown keys from external servers) val lenientJson = FhirR4Json { ignoreUnknownKeys = true } // --- Deserialization --- val rawJson = """ { "resourceType": "Patient", "id": "patient-01", "gender": "female", "birthDate": "1990-06-15" } """.trimIndent() val resource: Resource = json.decodeFromString(rawJson) check(resource is Patient) println(resource.id) // patient-01 println(resource.birthDate) // Date(date=1990-06-15) // --- Serialization --- val encoded: String = json.encodeToString(resource) println(encoded) // { // "resourceType": "Patient", // "id": "patient-01", // "gender": "female", // "birthDate": "1990-06-15" // } // --- R5 variant --- import dev.ohs.fhir.model.r5.FhirR5Json val jsonR5 = FhirR5Json() ``` ``` -------------------------------- ### Multi-Version FHIR Support (R4, R4B, R5) Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Illustrates how to use different FHIR versions (R4, R4B, R5) by importing version-specific classes and JSON codecs. Classes are not cross-version compatible, so ensure correct imports for the target version. ```kotlin // --- FHIR R4 --- import dev.ohs.fhir.model.r4.Patient as PatientR4 import dev.ohs.fhir.model.r4.FhirR4Json val r4Json = FhirR4Json() val r4Patient = r4Json.decodeFromString(r4JsonString) as PatientR4 println("R4 Patient ID: ${r4Patient.id}") // --- FHIR R4B --- import dev.ohs.fhir.model.r4b.Patient as PatientR4B import dev.ohs.fhir.model.r4b.FhirR4bJson val r4bJson = FhirR4bJson() val r4bPatient = r4bJson.decodeFromString(r4bJsonString) as PatientR4B println("R4B Patient ID: ${r4bPatient.id}") // --- FHIR R5 --- import dev.ohs.fhir.model.r5.Patient as PatientR5 import dev.ohs.fhir.model.r5.FhirR5Json val r5Json = FhirR5Json() val r5Patient = r5Json.decodeFromString(r5JsonString) as PatientR5 println("R5 Patient ID: ${r5Patient.id}") // Choosing a version at runtime fun makeJson(version: String) = when (version) { "r4" -> dev.ohs.fhir.model.r4.FhirR4Json() "r4b" -> dev.ohs.fhir.model.r4b.FhirR4bJson() "r5" -> dev.ohs.fhir.model.r5.FhirR5Json() else -> error("Unknown FHIR version: $version") } ``` -------------------------------- ### Add mavenCentral() to build.gradle.kts Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Include the mavenCentral() repository in your project's root build.gradle.kts file to resolve dependencies. This is a prerequisite for adding the Kotlin FHIR library. ```gradle // build.gradle.kts repositories { // Other repositories such as gradlePluginPortal() and google() mavenCentral() } ``` -------------------------------- ### Building a Patient Resource Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Demonstrates how to construct a FHIR Patient resource using the `Patient.Builder` class. It covers setting basic properties like ID, name, gender, birth date, and active status. ```APIDOC ## `Patient.Builder` — Building a Patient Resource Every FHIR resource ships with a fluent `Builder` class. All optional properties have `null` defaults; required list properties are `MutableList`. Calling `build()` produces an immutable `data class` instance. ```kotlin import dev.ohs.fhir.model.r4.* import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender val patient = Patient.Builder().apply { id = "patient-42" // Name name.add(HumanName.Builder().apply { family = String.Builder().apply { value = "Smith" }.build() given.add(String.Builder().apply { value = "Jane" }) }) // Gender using generated enum gender = Enumeration(value = AdministrativeGender.Female) // Birth date (partial FHIR date – year only, year-month, or full date) birthDate = Date.Builder().apply { value = FhirDate.fromString("1990-06-15") } // Active flag active = Boolean.Builder().apply { value = true } }.build() println(patient.id) // patient-42 println(patient.name.first().family) // Smith println(patient.gender?.value) // Female println(patient.birthDate?.value) // Date(date=1990-06-15) ``` ``` -------------------------------- ### Running FHIR Code Generation with Gradle Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Commands for using the `fhir-codegen` Gradle plugin to regenerate Kotlin model classes from FHIR specification packages. This is primarily for library development. ```bash # Generate models for all three FHIR versions and sync into the main source set ./gradlew codegen # Generate only a specific version ./gradlew r4 # Output: fhir-model/build/generated/r4 ./gradlew r4b # Output: fhir-model/build/generated/r4b ./gradlew r5 # Output: fhir-model/build/generated/r5 # Run JVM tests (includes round-trip tests for all official example resources) ./gradlew :fhir-model:jvmTest # Run Android unit tests ./gradlew :fhir-model:testDebugUnitTest # Publish to local Maven repository for integration testing ./gradlew :fhir-model:publishToMavenLocal ``` -------------------------------- ### Adding the Dependency Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Instructions on how to add the fhir-model dependency to your Kotlin Multiplatform or Android project using Maven Central. ```APIDOC ## Adding the Dependency Add `fhir-model` to a Kotlin Multiplatform or Android project via Maven Central. ```kotlin // settings.gradle.kts / build.gradle.kts (project root) repositories { mavenCentral() } // Kotlin Multiplatform – shared/build.gradle.kts kotlin { sourceSets { commonMain.dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } } } // Android-only – app/build.gradle.kts dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } ``` ``` -------------------------------- ### Configure Maven Central Credentials in Gradle Properties Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Store your Maven Central username, password, and GPG signing key details in the global Gradle properties file for secure and convenient access during publishing. ```properties # Maven Central Credentials mavenCentralUsername=YOUR_USERNAME mavenCentralPassword=YOUR_PASSWORD # GPG Key Details signing.keyId=YOUR_KEY_ID signing.password=YOUR_KEY_PASSWORD signing.secretKeyRingFile=/path/to/secring.gpg ``` -------------------------------- ### Create a FHIR Patient Resource Instance Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Use the generated builder classes to create new FHIR resource instances. Ensure the correct version-specific package (e.g., dev.ohs.fhir.model.r4) is imported. ```kotlin import dev.ohs.fhir.model.r4.Date import dev.ohs.fhir.model.r4.FhirDate import dev.ohs.fhir.model.r4.HumanName import dev.ohs.fhir.model.r4.Patient fun main() { val patient = Patient.Builder() .apply { id = "patient-01" name.add( HumanName.Builder().apply { given.add(dev.ohs.fhir.model.r4.String.Builder().apply { value = "John" }) } ) birthDate = Date.Builder().apply { value = FhirDate.fromString("2000-01-01") } } .build() } ``` -------------------------------- ### Initialize FHIR JSON Serializer Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Instantiate the FhirJson class for serializing and deserializing FHIR resources. An optional lambda can configure the underlying Json object. ```kotlin import dev.ohs.fhir.model.r4.FhirR4Json // or dev.ohs.fhir.model.r4b.FhirR4bJson or dev.ohs.fhir.model.r5.FhirR5Json fun main() { val jsonR4 = FhirR4Json() val jsonR4 = FhirR4Json({ ignoreUnknownKeys = true }) // optional lambda to configure the Json object } ``` -------------------------------- ### Run FHIR Model Codegen Locally Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Execute the gradlew codegen task to generate FHIR models for all supported versions. The generated code is placed in the fhir-model/src/commonMain/kotlin directory. ```bash ./gradlew codegen ``` -------------------------------- ### Partial Date Handling Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Explains the use of `FhirDate` and `FhirDateTime` for handling FHIR's partial date and date-time formats, including year-only, year-month, and full date/datetime values. ```APIDOC ## `FhirDate` and `FhirDateTime` — Partial Date Handling FHIR allows partial dates (year-only, year-month, or full date) and partial date-times. The library models these as sealed interfaces `FhirDate` and `FhirDateTime`, with subtypes `Year`, `YearMonth`, `Date`, and (for `FhirDateTime`) `DateTime`. Use `fromString()` to parse any valid FHIR date/datetime string. ```kotlin import dev.ohs.fhir.model.r4.FhirDate import dev.ohs.fhir.model.r4.FhirDateTime //FhirDate: year only val yearOnly: FhirDate = FhirDate.fromString("2023")!! check(yearOnly is FhirDate.Year) println((yearOnly asFhirDate.Year).value) // 2023 // FhirDate: year-month val yearMonth: FhirDate = FhirDate.fromString("2023-04")!! check(yearMonth isFhirDate.YearMonth) // FhirDate: full date val fullDate: FhirDate = FhirDate.fromString("2023-04-21")!! check(fullDate isFhirDate.Date) println(fullDate.toString()) // 2023-04-21 //FhirDateTime: full datetime with timezone offset val dt: FhirDateTime = FhirDateTime.fromString("2023-04-21T10:30:00+02:00")!! check(dt is FhirDateTime.DateTime) println(dt.toString()) // 2023-04-21T10:30:00+02:00 //FhirDateTime: year-only is also valid val partialDt: FhirDateTime = FhirDateTime.fromString("2021")!! check(partialDt isFhirDateTime.Year) ``` ``` -------------------------------- ### Run JVM Tests for FHIR Model Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Execute the comprehensive test suites for the FHIR model on the JVM. These tests cover equality, serialization, and builder round-trips for FHIR resources. ```shell ./gradlew :fhir-model:jvmTest ``` -------------------------------- ### Deserialize and Process FHIR Bundle Resource Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Demonstrates deserializing a FHIR `Bundle` resource from JSON and processing its entries. The `Bundle` resource is used to contain collections of other FHIR resources, such as search results. ```kotlin import dev.ohs.fhir.model.r4.* val json = FhirR4Json() // Deserialize a search Bundle returned by a FHIR server val bundleJson = """ { "resourceType": "Bundle", "type": "searchset", "total": 2, "entry": [ { "resource": { "resourceType": "Patient", "id": "p1", "gender": "male" } }, { "resource": { "resourceType": "Patient", "id": "p2", "gender": "female" } } ] } """.trimIndent() val bundle = json.decodeFromString(bundleJson) as Bundle println(bundle.total?.value) // 2 val patients = bundle.entry .mapNotNull { it.resource } .filterIsInstance() patients.forEach { p -> println("${p.id} → ${p.gender?.value?.getCode()}") } // p1 → male // p2 → female // Re-serialize the bundle println(json.encodeToString(bundle)) ``` -------------------------------- ### Handle FHIR Choice Types in Kotlin Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Demonstrates using Kotlin's smart casts with sealed interfaces to handle FHIR choice types within a `when` statement. This approach reduces boilerplate code and enhances type safety. ```kotlin when (val multipleBirth = patient.multipleBirth) { is Patient.MultipleBirth.Boolean -> { // Smart cast to Boolean println("Whether patient is part of a multiple birth: ${multipleBirth.value.value}") } is Patient.MultipleBirth.Integer -> { // Smart cast to Integer println("Birth order: ${multipleBirth.value.value}") } null -> { // Do nothing } } ``` -------------------------------- ### Handle Partial Dates and DateTimes with FhirDate/FhirDateTime in Kotlin Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Utilize `FhirDate` and `FhirDateTime` to represent and parse FHIR's partial date/datetime formats (year-only, year-month, full date/datetime). Use `fromString()` for parsing and check subtypes like `Year`, `YearMonth`, `Date`, and `DateTime`. ```kotlin import dev.ohs.fhir.model.r4.FhirDate import dev.ohs.fhir.model.r4.FhirDateTime //FhirDate: year only val yearOnly: FhirDate = FhirDate.fromString("2023")!! check(yearOnly is FhirDate.Year) println((yearOnly as FhirDate.Year).value) // 2023 // FhirDate: year-month val yearMonth: FhirDate = FhirDate.fromString("2023-04")!! check(yearMonth isFhirDate.YearMonth) // FhirDate: full date val fullDate: FhirDate = FhirDate.fromString("2023-04-21")!! check(fullDate isFhirDate.Date) println(fullDate.toString()) // 2023-04-21 // FhirDateTime: full datetime with timezone offset val dt: FhirDateTime = FhirDateTime.fromString("2023-04-21T10:30:00+02:00")!! check(dt is FhirDateTime.DateTime) println(dt.toString()) // 2023-04-21T10:30:00+02:00 // FhirDateTime: year-only is also valid val partialDt: FhirDateTime = FhirDateTime.fromString("2021")!! check(partialDt isFhirDateTime.Year) ``` -------------------------------- ### Modifying an Existing Resource Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Shows how to update an existing FHIR resource immutably by converting it to a builder using `toBuilder()`, making modifications, and then rebuilding the resource. ```APIDOC ## `Patient.toBuilder()` — Modifying an Existing Resource Any resource can be converted back to its builder with `toBuilder()`, modified, and rebuilt — enabling safe, immutable updates. ```kotlin import dev.ohs.fhir.model.r4.* val original: Patient = FhirR4Json().decodeFromString(patientJson) as Patient val updated = original.toBuilder().apply { // Add a second given name name.first().given.add(String.Builder().apply { value = "Marie" }) // Mark as inactive active = Boolean.Builder().apply { value = false } }.build() check(updated.id == original.id) check(updated.active?.value == false) check(updated.name.first().given.size == original.name.first().given.size + 1) ``` ``` -------------------------------- ### JSON Serialization and Deserialization with FhirR4Json Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Use `FhirR4Json` to serialize and deserialize FHIR resources to and from JSON. Customize the `Json` object using a builder for options like ignoring unknown keys. Supports R4, R4B, and R5 versions via dedicated classes. ```kotlin import dev.ohs.fhir.model.r4.FhirR4Json import dev.ohs.fhir.model.r4.Patient import dev.ohs.fhir.model.r4.Resource // Default configuration val json = FhirR4Json() // Custom configuration (e.g. ignore unknown keys from external servers) val lenientJson = FhirR4Json { ignoreUnknownKeys = true } // --- Deserialization --- val rawJson = """ { "resourceType": "Patient", "id": "patient-01", "gender": "female", "birthDate": "1990-06-15" } """.trimIndent() val resource: Resource = json.decodeFromString(rawJson) check(resource is Patient) println(resource.id) // patient-01 println(resource.birthDate) // Date(date=1990-06-15) // --- Serialization --- val encoded: String = json.encodeToString(resource) println(encoded) // { // "resourceType": "Patient", // "id": "patient-01", // "gender": "female", // "birthDate": "1990-06-15" // } // --- R5 variant --- import dev.ohs.fhir.model.r5.FhirR5Json val jsonR5 = FhirR5Json() ``` -------------------------------- ### Serialize and Deserialize FHIR Resources Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Use the encodeToString and decodeFromString functions from the initialized FhirJson object to convert FHIR resources to and from JSON strings. ```kotlin import dev.ohs.fhir.model.r4.Patient import dev.ohs.fhir.model.r4.Resource fun main() { val jsonString = jsonR4.encodeToString(patient) // Serialization val reconstructedPatient = jsonR4.decodeFromString(jsonString) // Deserialization check(reconstructedPatient is Patient) } ``` -------------------------------- ### Build a FHIR Patient Resource with Kotlin Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Use the `Patient.Builder` to construct a FHIR Patient resource with various fields like ID, name, gender, birth date, and active status. Required list properties are mutable, and optional properties default to null. ```kotlin import dev.ohs.fhir.model.r4.* import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender val patient = Patient.Builder().apply { id = "patient-42" // Name name.add(HumanName.Builder().apply { family = String.Builder().apply { value = "Smith" }.build() given.add(String.Builder().apply { value = "Jane" }) }) // Gender using generated enum gender = Enumeration(value = AdministrativeGender.Female) // Birth date (partial FHIR date – year only, year-month, or full date) birthDate = Date.Builder().apply { value = FhirDate.fromString("1990-06-15") } // Active flag active = Boolean.Builder().apply { value = true } }.build() println(patient.id) // patient-42 println(patient.name.first().family) // Smith println(patient.gender?.value) // Female println(patient.birthDate?.value) // Date(date=1990-06-15) ``` -------------------------------- ### Add Kotlin FHIR dependency to KMP project Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md For Kotlin Multiplatform projects, add the fhir-model dependency to the commonMain source set in your module's build.gradle.kts file. This makes the library available across all platforms. ```gradle // e.g., composeApp/build.gradle.kts or shared/build.gradle.kts kotlin { sourceSets { commonMain.dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } } } ``` -------------------------------- ### Publish FHIR Model Artifacts to Maven Central Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Publish the latest release of the FHIR model artifacts to Maven Central. Ensure your GPG signing key and repository credentials are correctly configured. ```shell ./gradlew :fhir-model:publishToMavenCentral ``` -------------------------------- ### Run Android Unit Tests for FHIR Model Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Execute the unit tests for the FHIR model specifically for Android environments. This ensures compatibility and correctness on the Android platform. ```shell ./gradlew :fhir-model:testDebugUnitTest ``` -------------------------------- ### Publish FHIR Model Artifacts to Maven Local Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Publish the FHIR model artifacts to your local Maven repository. This is useful for local development and testing before publishing to remote repositories. ```shell ./gradlew :fhir-model:publishToMavenLocal ``` -------------------------------- ### Add FHIR Model Dependency to Android Project Source: https://github.com/ohs-foundation/kotlin-fhir/blob/main/README.md Add this dependency to your module's build.gradle.kts file to include the FHIR model library. ```gradle // e.g., app/build.gradle.kts dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } ``` -------------------------------- ### Modify an Existing FHIR Resource Immutably in Kotlin Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Convert an existing FHIR resource to its builder using `toBuilder()`, make modifications, and then build a new, updated immutable resource. This ensures safe, immutable updates. ```kotlin import dev.ohs.fhir.model.r4.* val original: Patient = FhirR4Json().decodeFromString(patientJson) as Patient val updated = original.toBuilder().apply { // Add a second given name name.first().given.add(String.Builder().apply { value = "Marie" }) // Mark as inactive active = Boolean.Builder().apply { value = false } }.build() check(updated.id == original.id) check(updated.active?.value == false) check(updated.name.first().given.size == original.name.first().given.size + 1) ``` -------------------------------- ### Handle FHIR Choice Types with Kotlin Sealed Interfaces Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Use Kotlin's sealed interfaces and `when` expressions for type-safe access to FHIR choice types like `deceased[x]` and `value[x]`. This approach avoids manual casting and ensures exhaustive handling of all possible types. ```kotlin import dev.ohs.fhir.model.r4.Patient val patient: Patient = FhirR4Json().decodeFromString(jsonString) as Patient // Handling deceased[x]: Boolean or DateTime when (val deceased = patient.deceased) { is Patient.Deceased.Boolean -> println("Deceased flag: ${deceased.value.value}") is Patient.Deceased.DateTime -> println("Died on: ${deceased.value.value}") null -> println("Alive (or status unknown)") } // Handling multipleBirth[x]: Boolean or Integer when (val mb = patient.multipleBirth) { is Patient.MultipleBirth.Boolean -> println("Multiple birth: ${mb.value.value}") is Patient.MultipleBirth.Integer -> println("Birth order: ${mb.value.value}") null -> {} } // Observation value[x] — similar pattern in R4 import dev.ohs.fhir.model.r4.Observation val obs: Observation = FhirR4Json().decodeFromString(obsJson) as Observation when (val v = obs.value) { is Observation.Value.Quantity -> println("Qty: ${v.value.value?.value} ${v.value.unit?.value}") is Observation.Value.CodeableConcept -> println("Concept: ${v.value.text?.value}") is Observation.Value.String -> println("Text: ${v.value.value}") else -> println("Other value type: ${v?.let { it::class.simpleName }}") } ``` -------------------------------- ### Work with Typed FHIR Enumerations in Kotlin Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Use `Enumeration` to represent FHIR codes bound to ValueSets, where `T` is a generated enum. Access code, system, and display properties, and use `fromCode()` to convert back from a code string. This is useful for resource field access after deserialization. ```kotlin import dev.ohs.fhir.model.r4.Enumeration import dev.ohs.fhir.model.r4.terminologies.AdministrativeGender // Creating an Enumeration from an enum constant val gender = Enumeration(value = AdministrativeGender.Female) println(gender.value?.getCode()) // female println(gender.value?.getSystem()) // http://hl7.org/fhir/administrative-gender println(gender.value?.getDisplay()) // Female // Round-tripping from a code string val fromCode = AdministrativeGender.fromCode("male") println(fromCode) // male (toString() returns the code) check(fromCode == AdministrativeGender.Male) // Using in resource field access after deserialization val patient: Patient = FhirR4Json().decodeFromString(patientJson) as Patient when (patient.gender?.value) { AdministrativeGender.Male -> println("Patient is male") AdministrativeGender.Female -> println("Patient is female") AdministrativeGender.Other -> println("Patient: other gender") AdministrativeGender.Unknown -> println("Patient: unknown gender") null -> println("Gender not recorded") } ``` -------------------------------- ### Add Kotlin FHIR Dependency Source: https://context7.com/ohs-foundation/kotlin-fhir/llms.txt Include the `fhir-model` artifact in your Kotlin Multiplatform or Android project by adding the Maven Central repository and the dependency to your Gradle build files. ```kotlin // settings.gradle.kts / build.gradle.kts (project root) repositories { mavenCentral() } // Kotlin Multiplatform – shared/build.gradle.kts kotlin { sourceSets { commonMain.dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } } } // Android-only – app/build.gradle.kts dependencies { implementation("dev.ohs.fhir:fhir-model:1.0.0-beta03") } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.