#Datamapper array types are not expanded

1 messages · Page 1 of 1 (latest)

steel lagoon
#

Hi,

I'm trying to use ballerina datamapper and I'm trying to map a custom record to FHIR patient record[1]. There the array elements with a complex structure are not expanded. Refer to the following screenshot.

This is critical for a real world datamapping scenario for healthcare use cases. It seems this is something that needs to be fixed on the datamapper?

[1] - https://central.ballerina.io/ballerinax/health.fhir.r4.international401/1.2.0#Patient

versed bay
#

Looping in @grim parcel

#

Please check https://ballerina.io/learn/vs-code-extension/implement-the-code/data-mapper/#initialize-arrays-add-and-delete-elements. Once initialized, you can use + Add Element to populate member fields.

When you map data via the user interface, the Data Mapper generates the required Ballerina source code. Since the Ballerina source code is the single source of truth for the Visual Data Mapper, it also lets you open and edit the existing data mappings made via the source code without changing the user experience.

grim parcel
#

@steel lagoon as @versed bay mentioned you can initialize the array and add more elements. Hope it will address your use case. Please let us know if you need more help.

steel lagoon
#

Thanks for the responses. @grim parcel I'm not sure what is meant by initializing the array. In our use case it's not possible to initialize the array since we have to parse the incoming json message to the service, so we don't have a specific initialized message

grim parcel
#

Could you please share your sample source?

steel lagoon
#

This is a simplified sample:

import ballerina/http;
import ballerinax/health.fhir.r4.international401;

service / on new http:Listener(9090) {

resource function get Patient(PatientDetails patientDetails) returns international401:Patient {
    return transform(patientDetails);
}

}

type PatientDetails record {
string id?;
string firstname?;
string lastname?;
string nickname?;
};

//patientDetails.id should be mapped to identifier[0].value
function transform(PatientDetails patientDetails) returns international401:Patient => {
identifier: patientDetails.id
};

versed bay
#

The initialization is just to initialize the identifier field to an empty array. Then you can use Add Element to add a member and do the data mapping to the member of the identifier array.

#

If it is a different kind of mapping, is it possible to share the code for what the mapping would look like?

grim parcel
#

Adding more on @versed bay's answer then your code would look like

function transform(PatientDetails patientDetails) returns international401:Patient => {
    identifier: [
        {
            id: patientDetails.id
        }
    ]
};
steel lagoon
#

Thanks this worked. IMO datamapper documentation should have this information or steps on how to handle arrays?