Documentation
¶
Overview ¶
Package proto implements haistack-proto, an adapter layer between provider-specific FHIR protobuf messages and the haistack runtime.
haistack-proto wraps generated FHIR protos (initially Google FHIR Go R4) behind stable interfaces so downstream packages can parse, transform, and validate resources without importing provider module paths or version-specific message types. Callers work with any for proto values and *types.ResourceEnvelope for runtime containers.
This package is an adapter, not a source of truth:
- Canonical FHIR JSON remains the primary stored form (resource_json).
- types.ResourceEnvelope is the shared runtime abstraction across packages.
- Proto values are optional typed companions for validation, transformation, and (eventually) binary storage — never a replacement for JSON.
ProtoCodec ¶
ProtoCodec is the provider-agnostic boundary. Implementations convert between FHIR JSON, provider protobuf messages, and types.ResourceEnvelope:
- ParseJSONToProto validates the payload resourceType, parses JSON into a proto message, and returns it as any.
- ParseJSONToEnvelope parses JSON into proto and returns a fully populated ResourceEnvelope in one step (ParseJSONToProto followed by ProtoToEnvelope).
- ProtoToJSON accepts a supported proto value, serializes to FHIR JSON, and re-normalizes through types.NormalizeJSON so output matches the runtime's canonical JSON rules.
- ProtoToEnvelope converts proto to JSON, parses via types.NewJSONCodec().ParseJSON, attaches the original proto to envelope.Proto, and returns a fully populated envelope with Hash, VersionID, and LastUpdated derived the same way as JSON-only resources.
When resourceType is non-empty on any method, it must match the actual resource type carried by the payload or proto value; mismatches return an error.
GoogleR4Codec ¶
GoogleR4Codec is the default ProtoCodec implementation. It uses github.com/google/fhir/go (pinned to v0.7.4) jsonformat support and R4 generated protos.
ParseJSONToProto uses UnmarshalR4 and returns a Google R4 ContainedResource as any. ContainedResource is preferred internally so arbitrary resource types share one parse path.
ProtoToJSON accepts either a ContainedResource or an individual R4 resource message (for example *patient_go_proto.Patient). Individual messages are marshaled via MarshalResource; ContainedResource values use Marshal.
NewGoogleR4Codec constructs a codec with a UTC timezone unmarshaller and a compact (non-indented) marshaller. Initialization panics only on programmer error (invalid jsonformat configuration).
Integration with types.ResourceEnvelope ¶
ProtoToEnvelope always routes metadata through types.JSONCodec rather than reading proto fields directly. That guarantees envelope.Hash, envelope.VersionID, and envelope.LastUpdated match what pkg/types would produce from the same canonical JSON. envelope.Proto holds the caller's original proto value (ContainedResource or individual resource) for later typed access within pkg/proto or provider-aware code paths.
Downstream storage and APIs should continue to persist and exchange envelope.JSON; Proto is an in-memory companion only in MVP (no resource_proto_blob APIs).
Resource registry ¶
google_r4_registry.go builds an internal r4ResourceRegistry from the R4 ContainedResource protobuf oneof at init time. The registry maps FHIR resource type names (for example Patient) to oneof field descriptors and message full names. It powers ResourceTypeOfProto, IsProtoResource, wrapR4Resource, and future provider-specific lookups without exposing Google types in the public API.
Convenience helpers ¶
IsProtoResource reports whether a value is a supported Google FHIR R4 protobuf resource (ContainedResource with a set branch, or a known individual R4 resource message).
ResourceTypeOfProto returns the FHIR resource type string for a supported R4 proto value.
Both helpers currently target R4 only. When additional providers or FHIR versions are added, they will dispatch by message descriptor (see google_r5.go).
ToEnvelope, ToJSON, ResourceType, and ParseJSONToEnvelope are concise R4 helpers using the default GoogleR4Codec. Applications can construct resources with aliases from github.com/degoke/haistack/pkg/proto/r4, for example:
patient := r4.NewPatient("pat-1")
envelope, err := proto.ToEnvelope(patient)
AsContainedResource and ContainedResourceFromEnvelope unwrap envelope.Proto values for typed access to the inner ContainedResource without manual type assertions.
JSON round-trip limitations ¶
JSON→proto→JSON conversion uses Google's jsonformat layer. Fields not modeled in the Google R4 protobuf schema are not preserved on the proto path: ParseJSONToProto and ParseJSONToEnvelope return an error when the JSON contains unknown fields. Successful round-trips emit only schema-modeled fields in envelope.JSON. Use pkg/types JSON-only helpers when arbitrary or vendor-specific payload fields must be retained without proto.
Public API rules ¶
Exported codec signatures use any and *types.ResourceEnvelope. The r4 subpackage exposes aliases for the pinned Google-generated R4 messages; callers need not import the long github.com/google/fhir/go paths directly.
Storage APIs for proto blobs, profile-aware validation, and proto diffing are out of MVP scope.
Conversion flows ¶
JSON ingest:
FHIR JSON → ParseJSONToProto → ContainedResource (any)
FHIR JSON → ParseJSONToEnvelope → ResourceEnvelope { JSON, Hash, Proto, ... }
JSON egress:
ContainedResource (any) → ProtoToJSON → canonical JSON bytes individual R4 resource (any) → ProtoToJSON → canonical JSON bytes
Envelope construction:
proto (any) → ProtoToJSON → types.JSONCodec.ParseJSON → envelope + envelope.Proto = proto
Future extension points ¶
The following seams are documented in stub files and are not implemented in MVP:
- google_r5.go — GoogleR5Codec implementing ProtoCodec for R5 ContainedResource
- binary.go — wire-format marshal/unmarshal for optional proto blob storage
- validation.go — typed structural validation beyond jsonformat ingest checks
- codegen.go — generated registry and adapter boilerplate for new providers/versions
Adding a new provider should implement ProtoCodec, keep provider imports internal, reuse types.JSONCodec for envelope normalization, and extend the contract tests in proto_test.go.
Out of scope (MVP) ¶
R5 support, binary proto storage helpers, typed profile validation, code generation scripts, proto diffing, and persistence of proto blobs alongside resource_json.
Index ¶
- func AsContainedResource(v any) (*r4.ContainedResource, error)
- func ContainedResourceFromEnvelope(envelope *types.ResourceEnvelope) (*r4.ContainedResource, error)
- func IsProtoResource(v any) bool
- func KnownR4ResourceTypes() map[string]struct{}
- func ParseJSONToEnvelope(resourceType string, data []byte) (*types.ResourceEnvelope, error)
- func ResourceType(resource any) (string, error)
- func ResourceTypeOfProto(v any) (string, error)
- func ToEnvelope(resource any) (*types.ResourceEnvelope, error)
- func ToJSON(resource any) ([]byte, error)
- type GoogleR4Codec
- func (c *GoogleR4Codec) ParseJSONToEnvelope(resourceType string, data []byte) (*types.ResourceEnvelope, error)
- func (c *GoogleR4Codec) ParseJSONToProto(resourceType string, data []byte) (any, error)
- func (c *GoogleR4Codec) ProtoToEnvelope(resourceType string, protoVal any) (*types.ResourceEnvelope, error)
- func (c *GoogleR4Codec) ProtoToJSON(resourceType string, protoVal any) ([]byte, error)
- type ProtoCodec
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AsContainedResource ¶
func AsContainedResource(v any) (*r4.ContainedResource, error)
AsContainedResource returns v as a Google R4 ContainedResource. Values already stored as ContainedResource are returned as-is. Individual R4 resource messages (for example *r4.Patient) are wrapped automatically.
func ContainedResourceFromEnvelope ¶
func ContainedResourceFromEnvelope(envelope *types.ResourceEnvelope) (*r4.ContainedResource, error)
ContainedResourceFromEnvelope returns envelope.Proto as a ContainedResource. It errors when envelope is nil, Proto is unset, or Proto is not a supported R4 value.
func IsProtoResource ¶
IsProtoResource reports whether v is a supported Google FHIR R4 protobuf resource.
func KnownR4ResourceTypes ¶
func KnownR4ResourceTypes() map[string]struct{}
KnownR4ResourceTypes returns the FHIR resource types supported by GoogleR4Codec.
func ParseJSONToEnvelope ¶
func ParseJSONToEnvelope(resourceType string, data []byte) (*types.ResourceEnvelope, error)
ParseJSONToEnvelope parses FHIR JSON and returns a fully populated envelope with Proto attached.
func ResourceType ¶
ResourceType returns the FHIR resource type carried by a typed R4 resource.
func ResourceTypeOfProto ¶
ResourceTypeOfProto returns the FHIR resource type for a supported Google FHIR R4 proto value.
func ToEnvelope ¶
func ToEnvelope(resource any) (*types.ResourceEnvelope, error)
ToEnvelope converts a typed Google FHIR R4 resource to a canonical envelope. The original protobuf value is retained in the returned envelope's Proto field.
Types ¶
type GoogleR4Codec ¶
type GoogleR4Codec struct {
// contains filtered or unexported fields
}
GoogleR4Codec implements ProtoCodec using Google FHIR Go R4 protobufs.
func NewGoogleR4Codec ¶
func NewGoogleR4Codec() *GoogleR4Codec
NewGoogleR4Codec returns a GoogleR4Codec ready for use.
func (*GoogleR4Codec) ParseJSONToEnvelope ¶
func (c *GoogleR4Codec) ParseJSONToEnvelope(resourceType string, data []byte) (*types.ResourceEnvelope, error)
ParseJSONToEnvelope parses FHIR JSON into proto and returns a fully populated envelope.
func (*GoogleR4Codec) ParseJSONToProto ¶
func (c *GoogleR4Codec) ParseJSONToProto(resourceType string, data []byte) (any, error)
ParseJSONToProto validates resourceType and parses FHIR JSON into a Google R4 ContainedResource.
func (*GoogleR4Codec) ProtoToEnvelope ¶
func (c *GoogleR4Codec) ProtoToEnvelope(resourceType string, protoVal any) (*types.ResourceEnvelope, error)
ProtoToEnvelope converts proto to JSON, parses through types.JSONCodec, and attaches the proto value.
func (*GoogleR4Codec) ProtoToJSON ¶
func (c *GoogleR4Codec) ProtoToJSON(resourceType string, protoVal any) ([]byte, error)
ProtoToJSON converts a supported Google R4 proto resource to canonical FHIR JSON.
type ProtoCodec ¶
type ProtoCodec interface {
ParseJSONToProto(resourceType string, data []byte) (any, error)
ParseJSONToEnvelope(resourceType string, data []byte) (*types.ResourceEnvelope, error)
ProtoToJSON(resourceType string, proto any) ([]byte, error)
ProtoToEnvelope(resourceType string, proto any) (*types.ResourceEnvelope, error)
}
ProtoCodec converts between FHIR JSON, provider-specific protobuf messages, and types.ResourceEnvelope values.