-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompose.go
101 lines (85 loc) · 2.28 KB
/
compose.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package mseedio
import (
"reflect"
"time"
)
// f.Compose() compose a fixed section to bytes
func (f *fixedSection) Compose(bitOrder int) ([]byte, error) {
var (
result []byte
dataBytes []byte
values = reflect.ValueOf(f).Elem()
)
for i, j := 0, 0; i < FIXED_SECTION_LENGTH; j++ {
var (
field = fixedSectionMap[j]
fieldName = field.FieldName
)
var (
fieldSize = field.FieldSize
filedValue, err = getStructFieldValue(values, fieldName)
)
if err != nil && fieldName != "Reserved" {
return nil, err
}
i += fieldSize
switch field.FieldType {
case "int32":
// "int32" bit width should actually be determined by fieldSize
result = disassembleInt(filedValue.(int32), fieldSize, bitOrder)
case "string":
result = disassembleString(filedValue.(string), fieldSize, ' ')
case "time.Time":
result = disassembleTime(filedValue.(time.Time), bitOrder)
}
// Space as reserved field padding
if fieldName == "Reserved" {
result = []byte{' '}
}
dataBytes = append(dataBytes, result...)
}
return dataBytes, nil
}
// b.Compose() compose an 1000-blockette section to bytes
func (b *blocketteSection) Compose(bitOrder int) ([]byte, error) {
var blocketteLength int
for _, field := range blockette1000SectionMap {
blocketteLength += field.FieldSize
}
var (
result []byte
dataBytes []byte
values = reflect.ValueOf(b).Elem()
)
for i, j := 0, 0; i < blocketteLength; j++ {
var (
field = blockette1000SectionMap[j]
fieldName = field.FieldName
)
var (
fieldSize = field.FieldSize
filedValue, err = getStructFieldValue(values, fieldName)
)
if err != nil && fieldName != "Reserved" {
return nil, err
}
i += fieldSize
switch field.FieldType {
case "int32":
// "int32" bit width should actually be determined by fieldSize
result = disassembleInt(filedValue.(int32), fieldSize, bitOrder)
case "string":
result = disassembleString(filedValue.(string), fieldSize, ' ')
}
// Space as reserved field padding
if fieldName == "Reserved" {
result = []byte{0}
}
dataBytes = append(dataBytes, result...)
}
// Padding reserved field with 0 (56:64)
for i := 0; i < BLOCKETTE100X_SECTION_LENGTH-blocketteLength; i++ {
dataBytes = append(dataBytes, 0)
}
return dataBytes, nil
}