-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from fastenhealth/ndjson_phr_support
- Loading branch information
Showing
16 changed files
with
2,623 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package manual | ||
|
||
import ( | ||
"fmt" | ||
"github.com/fastenhealth/fasten-sources/clients/internal/base" | ||
"github.com/fastenhealth/fasten-sources/clients/models" | ||
"github.com/fastenhealth/fasten-sources/pkg" | ||
"github.com/fastenhealth/gofhir-models/fhir401" | ||
fhir401utils "github.com/fastenhealth/gofhir-models/fhir401/utils" | ||
"github.com/fastenhealth/gofhir-models/fhir430" | ||
fhir430utils "github.com/fastenhealth/gofhir-models/fhir430/utils" | ||
"github.com/samber/lo" | ||
"io" | ||
"os" | ||
) | ||
|
||
func extractPatientIdBundle(bundleFile *os.File) (string, pkg.FhirVersion, error) { | ||
defer bundleFile.Seek(0, io.SeekStart) | ||
|
||
// TODO: find a way to correctly detect bundle version | ||
|
||
bundleType := pkg.FhirVersion401 | ||
patientIds, err := parse401Bundle(bundleFile) | ||
bundleFile.Seek(0, io.SeekStart) | ||
|
||
//fallback to 430 bundle | ||
if err != nil || patientIds == nil || len(patientIds) == 0 { | ||
bundleType = pkg.FhirVersion430 | ||
|
||
patientIds, err = parse430Bundle(bundleFile) | ||
|
||
} | ||
if err != nil { | ||
//failed to parse the bundle as 401 and 430, return an error | ||
return "", "", fmt.Errorf("could not determine bundle version: %v", err) | ||
} else if patientIds == nil || len(patientIds) == 0 { | ||
return "", "", fmt.Errorf("could not determine patient id") | ||
} else { | ||
return cleanPatientIdPrefix(patientIds[0]), bundleType, nil | ||
} | ||
} | ||
|
||
//TODO: find a better, more generic way to do this. | ||
|
||
func parse401Bundle(bundleFile *os.File) ([]string, error) { | ||
bundle401Data := fhir401.Bundle{} | ||
//try parsing the bundle as a 401 bundle | ||
if err := base.UnmarshalJson(bundleFile, &bundle401Data); err == nil { | ||
patientIds := lo.FilterMap[fhir401.BundleEntry, string](bundle401Data.Entry, func(bundleEntry fhir401.BundleEntry, _ int) (string, bool) { | ||
parsedResource, err := fhir401utils.MapToResource(bundleEntry.Resource, false) | ||
if err != nil { | ||
return "", false | ||
} | ||
typedResource := parsedResource.(models.ResourceInterface) | ||
resourceType, resourceId := typedResource.ResourceRef() | ||
|
||
if resourceId == nil || len(*resourceId) == 0 { | ||
return "", false | ||
} | ||
return *resourceId, resourceType == fhir430.ResourceTypePatient.String() | ||
}) | ||
|
||
return patientIds, nil | ||
} else { | ||
return nil, err | ||
} | ||
|
||
} | ||
|
||
func parse430Bundle(bundleFile *os.File) ([]string, error) { | ||
bundle430Data := fhir430.Bundle{} | ||
//try parsing the bundle as a 430 bundle | ||
if err := base.UnmarshalJson(bundleFile, &bundle430Data); err == nil { | ||
patientIds := lo.FilterMap[fhir430.BundleEntry, string](bundle430Data.Entry, func(bundleEntry fhir430.BundleEntry, _ int) (string, bool) { | ||
parsedResource, err := fhir430utils.MapToResource(bundleEntry.Resource, false) | ||
if err != nil { | ||
return "", false | ||
} | ||
typedResource := parsedResource.(models.ResourceInterface) | ||
resourceType, resourceId := typedResource.ResourceRef() | ||
|
||
if resourceId == nil || len(*resourceId) == 0 { | ||
return "", false | ||
} | ||
return *resourceId, resourceType == fhir430.ResourceTypePatient.String() | ||
}) | ||
return patientIds, nil | ||
} else { | ||
return nil, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package manual | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/fastenhealth/fasten-sources/clients/models" | ||
"github.com/fastenhealth/fasten-sources/pkg" | ||
fhir401utils "github.com/fastenhealth/gofhir-models/fhir401/utils" | ||
"io" | ||
"os" | ||
) | ||
|
||
func extractPatientIdNDJson(bundleFile *os.File) (string, pkg.FhirVersion, error) { | ||
// TODO: find a way to correctly detect bundle version | ||
defer bundleFile.Seek(0, io.SeekStart) | ||
|
||
patientIds := []string{} | ||
d := json.NewDecoder(bundleFile) | ||
for { | ||
|
||
var resource json.RawMessage | ||
err := d.Decode(&resource) | ||
if err != nil { | ||
// io.EOF is expected at end of stream. | ||
if err == io.EOF { | ||
break //we're done | ||
} else { | ||
continue //skip this document, invalid json | ||
} | ||
} | ||
|
||
resourceObj, err := fhir401utils.MapToResource(resource, false) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
resourceObjTyped := resourceObj.(models.ResourceInterface) | ||
currentResourceType, currentResourceId := resourceObjTyped.ResourceRef() | ||
|
||
if currentResourceType == "Patient" { | ||
patientIds = append(patientIds, *currentResourceId) | ||
} | ||
} | ||
|
||
if len(patientIds) == 0 { | ||
return "", "", fmt.Errorf("could not determine patient id") | ||
} else { | ||
return cleanPatientIdPrefix(patientIds[0]), pkg.FhirVersion401, nil | ||
} | ||
} |
Oops, something went wrong.