bundle

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Overview

Package bundle provides utilities for working with FHIR R4 Bundle proto definitions. This includes functionality for constructing/wrapping and unwrapping bundle/entry objects.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidIdentity = fmt.Errorf("invaild resource identity")
	ErrInvalidPayload  = fmt.Errorf("invalid payload")
	ErrMissingPayload  = fmt.Errorf("%w: nil or empty payload", ErrInvalidPayload)
)
View Source
var (
	ErrNoLocation = errors.New("bundle entry response missing location")
)

Functions

func EntryReference

func EntryReference(entry *bcrpb.Bundle_Entry) *dtpb.Reference

EntryReference generates a FHIR Reference proto pointing to the Entry's resource.

func Extend

func Extend(bundle *bcrpb.Bundle, opts ...Option) *bcrpb.Bundle

Extend extends an existing bundle with the provided bundle options. The options will be extended in-place; the return value is not necessary to be looked at, but is available for convenience when used in fluent APIs.

This decision was made to avoid cloning the bundle per-invocation, since in a loop this would grow the cost involved with calling this function substantially.

func IdentityOfResponse

func IdentityOfResponse(response *bcrpb.Bundle_Entry_Response) (*resource.Identity, error)

IdentityOfResponse returns a complete Identity (Type and ID always set, VersionID set if applicable) representing the location contained in the given bundle entry response.

func New

func New(bundleType cpb.BundleTypeCode_Value, options ...Option) *bcrpb.Bundle

New creates a new New by building it from the bundle options. Use of this function directly is discouraged; prefer to use the various *New functions instead for the explicit types.

func NewBatch

func NewBatch(options ...Option) *bcrpb.Bundle

NewBatch is a helper function for building a batch bundle.

func NewCollection

func NewCollection(options ...Option) *bcrpb.Bundle

NewCollection is a helper function for building a collection bundle.

func NewCollectionEntry

func NewCollectionEntry(res fhir.Resource) *bcrpb.Bundle_Entry

NewCollectionEntry takes in a FHIR Resource and creates a BundleEntry for the resource.

func NewDeleteEntry

func NewDeleteEntry(typeName resource.Type, id string, opts ...EntryOption) *bcrpb.Bundle_Entry

NewDeleteEntry constructs a delete resource operation via a DELETE request.

For use within a batch or transaction bundle.

func NewGetEntry

func NewGetEntry(typeName resource.Type, id string, opts ...EntryOption) *bcrpb.Bundle_Entry

NewGetEntry constructs a bundle entry with a GET request for the head value of a resource. This is the FHIR "read" interaction.

For use within a batch or transaction bundle.

Clients should not request a versioned resource by crafting a special id argument; instead client should use VersionedGetEntry() below.

func NewHistory

func NewHistory(options ...Option) *bcrpb.Bundle

NewHistory is a helper function for building a history bundle.

func NewPostEntry

func NewPostEntry(res fhir.Resource, opts ...EntryOption) *bcrpb.Bundle_Entry

NewPostEntry wraps a FHIR Resource for a transaction bundle to be written to storage via a POST request.

This is based on GCP documentation here: https://cloud.google.com/healthcare-api/docs/how-tos/fhir-bundles#resolving_references_to_resources_created_in_a_bundle

func NewPutEntry

func NewPutEntry(res fhir.Resource, opts ...EntryOption) *bcrpb.Bundle_Entry

NewPutEntry wraps a FHIR Resource for a transaction bundle to be written to storage via a PUT request. BundleEntry.fullUrl is not populated.

This is based on GCP documentation here: https://cloud.google.com/healthcare-api/docs/how-tos/fhir-bundles#resolving_references_to_resources_created_in_a_bundle

func NewSearchset

func NewSearchset(options ...Option) *bcrpb.Bundle

NewSearchset is a helper function for building a searchset bundle.

func NewTransaction

func NewTransaction(options ...Option) *bcrpb.Bundle

NewTransaction is a helper function for building a transaction bundle.

func NewVersionedGetEntry

func NewVersionedGetEntry(typeName resource.Type, id string, version string, opts ...EntryOption) *bcrpb.Bundle_Entry

NewVersionedGetEntry constructs a bundle entry with a GET request for the versioned value of a resource. This is the FHIR "vread" interaction.

For use within a batch or transaction bundle.

If version is empty, the returned entry requests the head version (a simple GET).

The Bundle_Entry.FullUrl element is set to the requested resource's relative URL (without version). This is "good enough", despite questionable compliance with the standard. Clients may override this using WithFullUrl().

This function does NOT support the "history-*" (eg "history-instance") interactions. Those interaction return all the historical versions for one or more resources, and are not supported within a batch or transaction bundle.

func PatchEntryFromBytes

func PatchEntryFromBytes(identity *resource.Identity, payload []byte) (*bcrpb.Bundle_Entry, error)

PatchEntryFromBytes takes in a Resource Identity and a byte array payload, and returns a BundleEntry corresponding to a JSON Patch for the resource.

See https://cloud.google.com/healthcare-api/docs/how-tos/fhir-resources#executing_a_patch_request_in_a_fhir_bundle for the details of how PATCH payload can be generated for known fields.

For PATCH operations generation consider using one of the Go jsonpatch libraries from https://jsonpatch.com/#go. See the ./bundle_example_test.go/TestPatchEntry*() for the list of usage examples.

Example (DiffPatch)

ExamplePatchEntryFromBytes_diffPatch creates a patch for the diff of two given resources diff.

package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/google/fhir/go/fhirversion"
	"github.com/google/fhir/go/jsonformat"

	dtpb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/datatypes_go_proto"

	r4pb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/bundle_and_contained_resource_go_proto"

	ppb "github.com/google/fhir/go/proto/google/fhir/proto/r4/core/resources/patient_go_proto"
	"github.com/mattbaird/jsonpatch"
	"github.com/verily-src/fhirpath-go/internal/bundle"
	"github.com/verily-src/fhirpath-go/internal/resource"
	"google.golang.org/protobuf/proto"
)

var resIdentity, _ = resource.NewIdentity("Patient", "123", "")

func main() {
	res := &r4pb.ContainedResource{
		OneofResource: &r4pb.ContainedResource_Patient{
			Patient: &ppb.Patient{
				Id: &dtpb.Id{
					Value: "123",
				},
				Active: &dtpb.Boolean{
					Value: false,
				},
			},
		},
	}
	newRes := proto.Clone(res).(*r4pb.ContainedResource)
	newRes.GetPatient().Active = &dtpb.Boolean{Value: true}

	m, err := jsonformat.NewMarshaller(false, "", "", fhirversion.R4)
	if err != nil {
		log.Fatal(err)
	}
	resB, err := m.Marshal(res)
	if err != nil {
		log.Fatal(err)
	}
	newResB, err := m.Marshal(newRes)
	if err != nil {
		log.Fatal(err)
	}

	patch, err := jsonpatch.CreatePatch(resB, newResB)
	if err != nil {
		log.Fatal(err)
	}
	pPayload, err := json.Marshal(patch)
	if err != nil {
		log.Fatal(err)
	}

	pEntry, err := bundle.PatchEntryFromBytes(resIdentity, pPayload)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("PatchEntry: %+v", pEntry)
}
Output:

Example (MapPatch)
package main

import (
	"encoding/json"
	"fmt"
	"log"

	"github.com/verily-src/fhirpath-go/internal/bundle"
	"github.com/verily-src/fhirpath-go/internal/resource"
)

var resIdentity, _ = resource.NewIdentity("Patient", "123", "")

func main() {
	patch := []map[string]interface{}{
		{
			"op":    "replace",
			"path":  "/active",
			"value": true,
		},
	}
	payload, err := json.Marshal(patch)
	if err != nil {
		log.Fatal(err)
	}
	pEntry, err := bundle.PatchEntryFromBytes(resIdentity, payload)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("PatchEntry: %+v", pEntry)
}
Output:

Example (StringPatch)
package main

import (
	"fmt"
	"log"

	"github.com/verily-src/fhirpath-go/internal/bundle"
	"github.com/verily-src/fhirpath-go/internal/resource"
)

var resIdentity, _ = resource.NewIdentity("Patient", "123", "")

func main() {
	patch := `[{"op":"add","path":"/active","value":true}]`
	pEntry, err := bundle.PatchEntryFromBytes(resIdentity, []byte(patch))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("PatchEntry: %+v", pEntry)
}
Output:

func SetEntryIfMatch

func SetEntryIfMatch(entry *bcrpb.Bundle_Entry)

SetEntryIfMatch sets the entry.Request.IfMatch based on entry.Resource.Meta.VersionId.

func Unwrap

func Unwrap(bundle *bcrpb.Bundle) []fhir.Resource

Unwrap unwraps a bundle into a slice of resources.

func UnwrapEntry

func UnwrapEntry(entry *bcrpb.Bundle_Entry) fhir.Resource

UnwrapEntry unwraps a bundle entry into a FHIR Resource.

If the bundle entry is nil, or if the entry does not contain a resource, this function will return nil.

func UnwrapMap

func UnwrapMap(bundle *bcrpb.Bundle) map[resource.Type][]fhir.Resource

UnwrapMap unwraps a bundle into a map indexed by resource type.

Types

type EntryOption

type EntryOption interface {
	// contains filtered or unexported methods
}

EntryOption is an option interface for constructing bundle entries from raw data.

func WithFullURL

func WithFullURL(url string) EntryOption

WithFullURL adds a FullUrl field to a BundleEntry.

func WithGeneratedFullURL

func WithGeneratedFullURL() EntryOption

WithGeneratedFullURL adds a randomly generated FullUrl field to a BundleEntry, taking the form urn:uuid:$UUID.

func WithIfNoneExist

func WithIfNoneExist(identifier *dtpb.Identifier) EntryOption

WithIfNoneExist adds an identifier to the If-None-Exist request header of a POST BundleEntry, in the format `identifier=system|value`

type Option

type Option = bundleopt.BundleOption

Option is an option interface for constructing bundles from raw data.

func WithEntries

func WithEntries(entries ...*bcrpb.Bundle_Entry) Option

WithEntries adds bundle entries to a bundle.

func WithTimestamp

func WithTimestamp(t time.Time) Option

WithTimestamp adds a given time to the bundle's timestamp.

func WithTimestampNow

func WithTimestampNow() Option

WithTimestampNow adds the current time to the bundle's timestamp.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL