-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgohl7.go
68 lines (52 loc) · 1.77 KB
/
gohl7.go
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
package gohl7
// Function CleanRawValue return the escaped field free value of a simple field
// this function does create a copy of the value, if an in-place escape char replacement is
// need it (for saving memory) use RawValue+Encoding.Clean.
// this function could have receive a *SimpleField instead of a Field, but to free the caller
// of the type assertion we are doing it here.
func CleanRawValue(simple Field, enc *Encoding) ([]byte, error) {
if simple == nil {
return []byte{}, nil
}
s, ok := simple.(*SimpleField)
if !ok {
//error defined on utils.go
return nil, ErrNonInternalImplementation
}
l, j := len(s.v), 0
escapedValue := make([]byte, l)
for i := 0; i < l; j++ {
if s.v[i] == enc.Escaping {
i++
//not checking for out of bounds because this will imply a wrongly formatted
//field, which should have be cought be the parser
}
escapedValue[j] = s.v[i]
i++
}
return escapedValue[:j], nil
}
// Function RawValue gives you raw byte of a simple field.
// this function could have been a *SimpleField method but to discorage
// it overuse, it has been done similar to CleanRawValue.
func RawValue(simple *SimpleField) []byte {
if simple == nil {
return []byte{}
}
return simple.v
}
// Function ContainerFieldChildren returns the list of children from a ContainerField
// Given the fact this function is tied to the ComplexField implementation, it could have
// have been implemented as a method but to keep things consistent with SimpleField is done
// as a separte method
func ContainerFieldChildren(container Field) ([]Field, error) {
if container == nil {
return nil, nil
}
complexF, ok := container.(*ComplexField)
if !ok {
//error defined on utils.go
return nil, ErrNonInternalImplementation
}
return complexF.children, nil
}