-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccessor.go
More file actions
326 lines (296 loc) · 8.5 KB
/
accessor.go
File metadata and controls
326 lines (296 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2026 Joshua Jones <joshua.jones.software@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hl7
import (
"strconv"
"unsafe"
)
// Location represents a specific position in an HL7 message hierarchy.
// All indices are 0-based internally. The string representation uses
// 1-based field/component/subcomponent indices for HL7 convention.
type Location struct {
Segment string // 3-char segment type, e.g., "PID"
SegmentIndex int // 0-based: which occurrence of this segment type
Field int // 1-based field number (0 = segment type)
Repetition int // 0-based repetition index
Component int // 1-based component number (0 = not specified)
SubComponent int // 1-based subcomponent number (0 = not specified)
}
// String returns the terser-style string representation of the location.
// This is the inverse of ParseLocation.
//
// Examples:
//
// Location{Segment: "MSH", Field: 9}.String() // "MSH-9"
// Location{Segment: "PID", Field: 3, Component: 1}.String() // "PID-3.1"
// Location{Segment: "OBX", SegmentIndex: 1, Field: 5}.String() // "OBX(1)-5"
// Location{Segment: "PID", Field: 3, Repetition: 1}.String() // "PID-3[1]"
func (loc Location) String() string {
b := make([]byte, len(loc.Segment), 110)
copy(b, []byte(loc.Segment))
if loc.SegmentIndex > 0 {
b = append(b, '(')
b = strconv.AppendInt(b, int64(loc.SegmentIndex), 10)
b = append(b, ')')
}
b = append(b, '-')
b = strconv.AppendInt(b, int64(loc.Field), 10)
if loc.Repetition > 0 {
b = append(b, '[')
b = strconv.AppendInt(b, int64(loc.Repetition), 10)
b = append(b, ']')
}
if loc.Component > 0 {
b = append(b, '.')
b = strconv.AppendInt(b, int64(loc.Component), 10)
}
if loc.SubComponent > 0 {
b = append(b, '.')
b = strconv.AppendInt(b, int64(loc.SubComponent), 10)
}
return unsafe.String(unsafe.SliceData(b), len(b))
}
// ParseLocation parses a terser-style location string into a Location.
//
// Supported formats:
//
// "MSH-9" segment-field
// "MSH-9.1" segment-field.component
// "MSH-9.1.2" segment-field.component.subcomponent
// "OBX(1)-5" segment(segIndex)-field (0-based segment index)
// "PID-3[1]" segment-field[repIndex] (0-based repetition index)
// "PID-3[1].4.2" full specification
//
// Returns ErrInvalidLocation if the string cannot be parsed.
func ParseLocation(s string) (Location, error) {
var loc Location
n := len(s)
if n == 0 {
return loc, ErrInvalidLocation
}
i := 0
// Segment type: scan until '(' or '-'.
start := i
for i < n && s[i] != '(' && s[i] != '-' {
i++
}
loc.Segment = s[start:i]
if len(loc.Segment) < 2 || len(loc.Segment) > 3 {
return loc, ErrInvalidLocation
}
// Optional segment index: '(' digits ')'.
if i < n && s[i] == '(' {
i++ // skip '('
start = i
for i < n && s[i] != ')' {
i++
}
if i >= n || i == start {
return loc, ErrInvalidLocation
}
num, err := strconv.Atoi(s[start:i])
if err != nil {
return loc, ErrInvalidLocation
}
loc.SegmentIndex = num
i++ // skip ')'
}
// Expect '-'.
if i >= n || s[i] != '-' {
return loc, ErrInvalidLocation
}
i++ // skip '-'
if i >= n {
return loc, ErrInvalidLocation
}
// Field number: scan digits.
start = i
for i < n && s[i] >= '0' && s[i] <= '9' {
i++
}
if i == start {
return loc, ErrInvalidLocation
}
num, err := strconv.Atoi(s[start:i])
if err != nil {
return loc, ErrInvalidLocation
}
loc.Field = num
if i >= n {
return loc, nil
}
// Optional repetition: '[' digits ']'.
if s[i] == '[' {
i++ // skip '['
start = i
for i < n && s[i] != ']' {
i++
}
if i >= n || i == start {
return loc, ErrInvalidLocation
}
num, err := strconv.Atoi(s[start:i])
if err != nil {
return loc, ErrInvalidLocation
}
loc.Repetition = num
i++ // skip ']'
}
if i >= n {
return loc, nil
}
// Optional component: '.' digits.
if s[i] != '.' {
return loc, ErrInvalidLocation
}
i++ // skip '.'
start = i
for i < n && s[i] >= '0' && s[i] <= '9' {
i++
}
if i > start {
num, err := strconv.Atoi(s[start:i])
if err != nil {
return loc, ErrInvalidLocation
}
loc.Component = num
}
if i >= n {
return loc, nil
}
// Optional subcomponent: '.' digits.
if s[i] != '.' {
return loc, ErrInvalidLocation
}
i++ // skip '.'
start = i
for i < n && s[i] >= '0' && s[i] <= '9' {
i++
}
if i > start {
num, err := strconv.Atoi(s[start:i])
if err != nil {
return loc, ErrInvalidLocation
}
loc.SubComponent = num
}
// Reject trailing characters.
if i != n {
return loc, ErrInvalidLocation
}
return loc, nil
}
// Value holds the raw bytes at a terser-style location, returned by Get.
// It is a lightweight value type (raw []byte + Delimiters), consistent with
// Field, Repetition, Component, and Subcomponent.
//
// A zero Value (nil raw bytes) is returned when the location is invalid or
// the addressed element is not present in the message. A zero Value is empty:
// IsEmpty() returns true, String() returns "", and Bytes() returns nil.
type Value struct {
raw []byte
delims Delimiters
}
// String returns the unescaped string value. Returns an empty string for a
// zero (not-found) Value.
func (v Value) String() string {
return string(v.Bytes())
}
// Bytes returns the unescaped bytes of this value. Equivalent to calling
// String() and converting, but avoids the string allocation for binary use.
// Returns nil for a zero (not-found) Value.
func (v Value) Bytes() []byte {
return Unescape(v.raw, v.delims)
}
// Raw returns the raw, possibly-escaped bytes of this value as they appear
// in the HL7 message (before escape processing). Returns nil for a zero Value.
func (v Value) Raw() []byte {
return v.raw
}
// IsEmpty returns true if the value was not present (nil raw bytes).
func (v Value) IsEmpty() bool {
return len(v.raw) == 0
}
// IsNull returns true if the value is the HL7 explicit null, represented by
// two double-quote characters ("").
func (v Value) IsNull() bool {
return len(v.raw) == 2 && v.raw[0] == '"' && v.raw[1] == '"'
}
// HasValue returns true if the value is neither empty nor null.
func (v Value) HasValue() bool {
return !v.IsEmpty() && !v.IsNull()
}
// Get retrieves the value at the given terser-style location.
//
// Returns a zero Value if the location string is invalid or the addressed
// element is not present — consistent with how Field(n), Rep(n), Component(n),
// and SubComponent(n) return zero values for out-of-range indices.
//
// Examples:
//
// msg.Get("MSH-9").String() // Message type field (full value, unescaped)
// msg.Get("MSH-9.1").String() // Message code (e.g., "ADT")
// msg.Get("MSH-9.2").String() // Trigger event (e.g., "A01")
// msg.Get("PID-3.1").String() // Patient ID
// msg.Get("PID-5.1").String() // Family name
// msg.Get("OBX(0)-5").String() // First OBX segment, field 5
// msg.Get("PID-3.1").Bytes() // unescaped bytes
// msg.Get("PID-3.1").Raw() // raw bytes without unescaping
func (m *Message) Get(location string) Value {
loc, err := ParseLocation(location)
if err != nil {
return Value{}
}
return m.getByLocation(loc)
}
// getByLocation navigates the message hierarchy to the specified location.
func (m *Message) getByLocation(loc Location) Value {
// Find the matching segment.
matchIdx := 0
var seg *Segment
for i := range m.segments {
if m.segments[i].Type() == loc.Segment {
if matchIdx == loc.SegmentIndex {
seg = &m.segments[i]
break
}
matchIdx++
}
}
if seg == nil {
return Value{}
}
field := seg.Field(loc.Field)
if field.IsEmpty() {
return Value{}
}
rep := field.Rep(loc.Repetition)
if rep.IsEmpty() {
return Value{}
}
// If no component specified, return the repetition.
if loc.Component == 0 {
return rep.Value
}
comp := rep.Component(loc.Component)
if comp.IsEmpty() && loc.SubComponent == 0 {
return Value{}
}
// If no subcomponent specified, return the component.
if loc.SubComponent == 0 {
return comp.Value
}
sub := comp.SubComponent(loc.SubComponent)
return sub.Value
}