hl7

package module
v0.0.0-...-abdfab5 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 11 Imported by: 0

README

Go Level 7 GoDoc

Overview

Go Level 7 is a decoder / encoder for HL7.

Health Level-7 or HL7 refers to a set of international standards for transfer of clinical and administrative data between software applications used by various healthcare providers. These standards focus on the application layer, which is "layer 7" in the OSI model. -Wikipedia

Features

  • Decode HL7 messages
  • Multiple message support with Split
  • Unmarshal into Go structs
  • Simple query syntax
  • Message validation

Note: Message building is not currently working for MSH segments. Coming soon...

Installation

go get github.com/lenaten/hl7

Usage

Data Location Syntax
segment-name.field-sequence-number.component.subcomponent
Segments are specified using the three letter name (MSH)
Fields are specifified by the sequence number of the HL7 specification (Field 0 is always the segment name)
Components and Subcomponents are 0 based indexes
"" returns the message
"PID" returns the PID segment
"PID.5" returns the 5th field of the PID segment
"PID.5.1" returns the 1st component of the 5th field of the PID segment
"PID.5.1.2" returns the 2nd subcomponent of the 1st component of the 5th field of the PID
Data Extraction / Unmarshal
data := []byte(...) // raw message
type my7 struct {
	FirstName string `hl7:"PID.5.1"`
	LastName  string `hl7:"PID.5.0"`
}
st := my7{}

err := hl7.Unmarshal(data, &st)

// from an io.Reader

hl7.NewDecoder(reader).Decode(&st)
Message Query
msg, err := hl7.NewDecoder(reader).Message()

// First matching value
val, err := msg.Find("PID.5.1")

// All matching values
vals, err := msg.FindAll("PID.11.1")
Message building
	type aMsg struct {
		FirstName string `hl7:"PID.5.1"`
		LastName  string `hl7:"PID.5.0"`
	}
	mi := hl7.MsgInfo{
		SendingApp:        "MyApp",
		SendingFacility:   "MyPlace",
		ReceivingApp:      "EMR",
		ReceivingFacility: "MedicalPlace",
		MessageType:       "ORM^001",
	}
	msg, err := hl7.StartMessage(mi)
	am := aMsg{FirstName: "Davin", LastName: "Hills"}
	bstr, err = hl7.Marshal(msg, &am)

	// Manually

	type MyHL7Message struct {
		SendingApp        string `hl7:"MSH.3"`
		SendingFacility   string `hl7:"MSH.4"`
		ReceivingApp      string `hl7:"MSH.5"`
		ReceivingFacility string `hl7:"MSH.6"`
		MsgDate           string `hl7:"MSH.7"`
		MessageType       string `hl7:"MSH.9"`
		ControlID         string `hl7:"MSH.10"`
		ProcessingID      string `hl7:"MSH.11"`
		VersionID         string `hl7:"MSH.12"`
		FirstName         string `hl7:"PID.5.1"`
		LastName          string `hl7:"PID.5.0"`
	}

	my := MyHL7Message{
		SendingApp:        "MyApp",
		SendingFacility:   "MyPlace",
		ReceivingApp:      "EMR",
		ReceivingFacility: "MedicalPlace",
		MessageType:       "ORM^001",
		MsgDate:           "20151209154606",
		ControlID:         "MSGID1",
		ProcessingID:      "P",
		VersionID:         "2.4",
		FirstName:         "Davin",
		LastName:          "Hills",
	}
	err := hl7.NewEncoder(writer).Encode(&my)
Message Validation

Message validation is accomplished using the IsValid function. Create a slice of Validation structs and pass them, with the message, to the IsValid function. The first return value is a pass / fail bool. The second return value returns the Validation structs that failed.

A number of validation slices are already defined and can be combined to build custom validation criteria. The NewValidMSH24() function is one example. It returns a set of validations for the MSH segment for version 2.4 of the HL7 specification.

val := []hl7.Validation{
	Validation{Location: "MSH.0", VCheck: SpecificValue, Value: "MSH"},
	Validation{Location: "MSH.1", VCheck: HasValue},
	Validation{Location: "MSH.2", VCheck: HasValue},
}
msg, err := hl7.NewDecoder(reader).Message()
valid, failures := msg.IsValid(val)

To Do

  • Better handling of repeating fields for marshal and unmarshal
  • Better handling of repeating segments for marshal and unmarshal

Alternatives

License

Copyright 2015, 2016 Davin Hills. All rights reserved. MIT license. License details can be found in the LICENSE file.

Documentation

Index

Constants

View Source
const (
	HasValue = iota
	SpecificValue
)

VCheck values

Variables

View Source
var (
	ErrSegmentNotFound     = fmt.Errorf("Segment not found")
	ErrFieldNotFound       = fmt.Errorf("Field not found")
	ErrComponentOutOfRange = fmt.Errorf("Component out of range")
)

Functions

func Marshal

func Marshal(m *Message, it interface{}) ([]byte, error)

Marshal will insert values into a message It will panic if interface{} is not a pointer to a struct

func Split

func Split(buf []byte) [][]byte

Split will split a set of HL7 messages

\x0b MESSAGE \x1c\x0d

Types

type ACK

type ACK struct {
	Code         string `hl7:"MSA.1"`
	OrgControlID string `hl7:"MSA.2"`
	ErrMsg       string `hl7:"MSA.3"`
}

ACK is struct for an ack message

type Component

type Component struct {
	SubComponents []SubComponent
	Value         []rune
}

Component is an HL7 component

func (*Component) Get

func (c *Component) Get(l *Location) (string, error)

Get returns the value specified by the Location

func (*Component) Set

func (c *Component) Set(l *Location, val string, seps *Delimeters) error

Set will insert a value into a message at Location

func (*Component) String

func (c *Component) String() string

func (*Component) SubComponent

func (c *Component) SubComponent(i int) (*SubComponent, error)

SubComponent returns the subcomponent i

type Decoder

type Decoder struct {
	// contains filtered or unexported fields
}

Decoder reades hl7 messages from a stream

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new Decoder that reades from from stream r Assumes one message per stream

func (*Decoder) Messages

func (d *Decoder) Messages() ([]*Message, error)

Messages returns a new Message slice parsed from stream r

type Delimeters

type Delimeters struct {
	DelimeterField string
	Field          rune
	Component      rune
	Repetition     rune
	Escape         rune
	SubComponent   rune
	Truncate       rune
	LFTermMsg      bool
}

Delimeters holds the list of hl7 message delimeters

func NewDelimeters

func NewDelimeters() *Delimeters

NewDelimeters returns the default set of HL7 delimeters

type Encoder

type Encoder struct {
	// contains filtered or unexported fields
}

Encoder writes hl7 messages to a stream

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new Encoder that writes to stream w

func (*Encoder) Encode

func (e *Encoder) Encode(it interface{}) error

Encode writes the encoding of it to the stream It will panic if interface{} is not a pointer to a struct

type Field

type Field struct {
	SeqNum     int
	Components []Component
	Value      []rune
}

Field is an HL7 field

func (*Field) Component

func (f *Field) Component(i int) (*Component, error)

Component returns the component i

func (*Field) Get

func (f *Field) Get(l *Location) (string, error)

Get returns the value specified by the Location

func (*Field) Set

func (f *Field) Set(l *Location, val string, seps *Delimeters) error

Set will insert a value into a message at Location

func (*Field) String

func (f *Field) String() string

type Location

type Location struct {
	Segment  string
	FieldSeq int
	Comp     int
	SubComp  int
}

Location specifies a value or values in an Message

func NewLocation

func NewLocation(l string) *Location

NewLocation creates a Location struct based on location string syntax

type Message

type Message struct {
	Segments   []Segment
	Value      []rune
	Delimeters Delimeters
}

Message is an HL7 message

func Acknowledge

func Acknowledge(mi MsgInfo, st error) (*Message, []byte)

Acknowledge generates an ACK message based on the MsgInfo struct st can be nil for success or to send an AE code

func NewMessage

func NewMessage(v []byte) *Message

NewMessage returns a new message with the v byte value

func StartMessage

func StartMessage(info MsgInfo) (*Message, error)

StartMessage returns a Message with an MSH segment based on the MsgInfo struct

func (*Message) AllSegments

func (m *Message) AllSegments(s string) ([]*Segment, error)

AllSegments returns the first matching segmane with name s

func (*Message) Find

func (m *Message) Find(loc string) (string, error)

Find gets a value from a message using location syntax finds the first occurence of the segment and first of repeating fields if the loc is not valid an error is returned

func (*Message) FindAll

func (m *Message) FindAll(loc string) ([]string, error)

FindAll gets all values from a message using location syntax finds all occurences of the segments and all repeating fields if the loc is not valid an error is returned

func (*Message) Get

func (m *Message) Get(l *Location) (string, error)

Get returns the first value specified by the Location

func (*Message) GetAll

func (m *Message) GetAll(l *Location) ([]string, error)

GetAll returns all values specified by the Location

func (*Message) Info

func (m *Message) Info() (MsgInfo, error)

Info returns the MsgInfo for the message

func (*Message) IsValid

func (m *Message) IsValid(val []Validation) (bool, []Validation)

IsValid checks a message for validity based on a set of criteria it returns valid and any failed validation rules

func (*Message) Segment

func (m *Message) Segment(s string) (*Segment, error)

Segment returns the first matching segmane with name s

func (*Message) Set

func (m *Message) Set(l *Location, val string) error

Set will insert a value into a message at Location

func (*Message) String

func (m *Message) String() string

func (*Message) Unmarshal

func (m *Message) Unmarshal(it interface{}) error

Unmarshal fills a structure from an HL7 message It will panic if interface{} is not a pointer to a struct Unmarshal will decode the entire message before trying to set values it will set the first matching segment / first matching field repeating segments and fields is not well suited to this for the moment all unmarshal target fields must be strings

type MsgInfo

type MsgInfo struct {
	SendingApp        string `hl7:"MSH.3"`
	SendingFacility   string `hl7:"MSH.4"`
	ReceivingApp      string `hl7:"MSH.5"`
	ReceivingFacility string `hl7:"MSH.6"`
	MsgDate           string `hl7:"MSH.7"`  // if blank will generate
	MessageType       string `hl7:"MSH.9"`  // Required example ORM^001
	ControlID         string `hl7:"MSH.10"` // if blank will generate
	ProcessingID      string `hl7:"MSH.11"` // default P
	VersionID         string `hl7:"MSH.12"` // default 2.4
}

MsgInfo describes the basic message fields

func NewMsgInfo

func NewMsgInfo() *MsgInfo

NewMsgInfo returns a MsgInfo with controlID, message date, Processing Id, and Version set Version = 2.4 ProcessingID = P

func NewMsgInfoAck

func NewMsgInfoAck(mi *MsgInfo) *MsgInfo

NewMsgInfoAck returns a MsgInfo ACK based on the MsgInfo passed in

type Segment

type Segment struct {
	Fields []Field
	Value  []rune
	// contains filtered or unexported fields
}

Segment is an HL7 segment

func (*Segment) AllFields

func (s *Segment) AllFields(i int) ([]*Field, error)

AllFields returns all fields with sequence number i

func (*Segment) Field

func (s *Segment) Field(i int) (*Field, error)

Field returns the field with sequence number i

func (*Segment) Get

func (s *Segment) Get(l *Location) (string, error)

Get returns the first value specified by the Location

func (*Segment) GetAll

func (s *Segment) GetAll(l *Location) ([]string, error)

GetAll returns all values specified by the Location

func (*Segment) Set

func (s *Segment) Set(l *Location, val string, seps *Delimeters) error

Set will insert a value into a message at Location

func (*Segment) String

func (s *Segment) String() string

type SubComponent

type SubComponent struct {
	Value []rune
}

SubComponent is an HL7 subcomponent

type VCheck

type VCheck int

VCheck is the type validity check to be done

type Validation

type Validation struct {
	Location string // Query syntax
	VCheck   VCheck // What to check
	Value    string // Matching value for SpecificValue
	Err      error  // error to use
}

Validation contains information to validate a message value

func NewValidMSH24

func NewValidMSH24() []Validation

NewValidMSH24 is the validation for the MSH segment for version 2.4

func NewValidODS24

func NewValidODS24() []Validation

NewValidODS24 is the validation for the ODS segment for version 2.4

func NewValidORC24

func NewValidORC24() []Validation

NewValidORC24 is the validation for the ORC segment for version 2.4

func NewValidORMDietaryOrder24

func NewValidORMDietaryOrder24() []Validation

NewValidORMDietaryOrder24 is an example of validating a ORM^001 message

func NewValidPID24

func NewValidPID24() []Validation

NewValidPID24 is the validation for the PID segment for version 2.4

func NewValidPV124

func NewValidPV124() []Validation

NewValidPV124 is the validation for the PV1 segment for version 2.4

Jump to

Keyboard shortcuts

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