-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
80 lines (63 loc) · 1.88 KB
/
utils.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
69
70
71
72
73
74
75
76
77
78
79
80
package gohl7
import (
"errors"
"fmt"
)
var (
errNoMatchingTypes = errors.New("No matching type")
errNoEqualValue = errors.New("No equal simple value")
ErrNonInternalImplementation = errors.New("Provided fields do not match internal implementation")
errNonMatchingChildren = errors.New("Different amount of childrens")
)
//function newSimpleStr is an internal utility function
//mostly for testing
func newSimpleStr(s string) *SimpleField {
return NewSimpleField([]byte(s))
}
// function newComplexFieldWithChildren is an internal utility function
// mostly for testing
//error code is ignore on purpose as its assume the call is internal
func newComplexFieldWithChildren(t FieldType, v func(Field, Field) error, children ...Field) *ComplexField {
complexF := NewComplexField(t, v)
for _, child := range children {
_ = complexF.Push(child)
}
return complexF
}
//function deepEqual compare two fields recursivly for equality
//a return value of nil implies equal a non nil implies difference
func deepEqual(l, r Field) error {
if l.Type() != r.Type() {
return errNoMatchingTypes
}
//handling simple case
if l.Type() == Simple {
simpleL, okL := l.(*SimpleField)
simpleR, okR := r.(*SimpleField)
if !okL || !okR {
return ErrNonInternalImplementation
}
if string(simpleL.v) != string(simpleR.v) {
return errNoEqualValue
}
return nil
}
//handling complex case
complexL, okL := l.(*ComplexField)
complexR, okR := r.(*ComplexField)
if !okL || !okR {
return ErrNonInternalImplementation
}
childrenL, childrenR := complexL.children, complexR.children
if len(childrenL) != len(childrenR) {
//return errNonMatchingChildren
return fmt.Errorf("Different amount of childrens \n%s\n %s\n", l, r)
}
for i := 0; i < len(childrenL); i++ {
err := deepEqual(childrenL[i], childrenR[i])
if err != nil {
return err
}
}
return nil
}