-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline_column.go
101 lines (85 loc) · 2.68 KB
/
inline_column.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 csvlib
import (
"reflect"
)
type inlineColumnStructType int8
const (
inlineColumnStructDynamic = inlineColumnStructType(1)
inlineColumnStructFixed = inlineColumnStructType(2)
dynamicInlineColumnHeader = "Header"
dynamicInlineColumnValues = "Values"
)
// InlineColumn represents inline columns of type `T`
type InlineColumn[T any] struct {
Header []string
Values []T
}
// inlineColumnMeta metadata of inline columns
type inlineColumnMeta struct {
headerText []string
inlineType inlineColumnStructType
targetField reflect.StructField
dataType reflect.Type
// columnCurrIndex current processing column (used for dynamic inline columns)
columnCurrIndex int
}
func (m *inlineColumnMeta) decodePrepareForNextRow() {
m.columnCurrIndex = -1
}
func (m *inlineColumnMeta) decodeInitInlineStruct(inlineStruct reflect.Value) {
if inlineStruct.Kind() == reflect.Pointer {
inlineStruct.Set(reflect.New(inlineStruct.Type().Elem())) // No need to check Nil as this is decoding
inlineStruct = inlineStruct.Elem()
}
switch m.inlineType {
case inlineColumnStructFixed:
// Do nothing
case inlineColumnStructDynamic:
if m.columnCurrIndex != -1 {
return
}
numCols := len(m.headerText)
inlineStruct.FieldByName(dynamicInlineColumnHeader).Set(reflect.ValueOf(m.headerText))
columnValues := inlineStruct.Field(m.targetField.Index[0])
columnValues.Set(reflect.MakeSlice(reflect.SliceOf(m.dataType), numCols, numCols))
m.columnCurrIndex = 0
}
}
func (m *inlineColumnMeta) decodeGetColumnValue(inlineStruct reflect.Value) reflect.Value {
if inlineStruct.Kind() == reflect.Pointer {
inlineStruct.Set(reflect.New(inlineStruct.Type().Elem())) // No need to check Nil as this is decoding
inlineStruct = inlineStruct.Elem()
}
switch m.inlineType {
case inlineColumnStructFixed:
return inlineStruct.Field(m.targetField.Index[0])
case inlineColumnStructDynamic:
if m.columnCurrIndex == -1 {
m.decodeInitInlineStruct(inlineStruct)
}
colVal := inlineStruct.Field(m.targetField.Index[0]).Index(m.columnCurrIndex)
m.columnCurrIndex++
return colVal
}
return reflect.Value{}
}
func (m *inlineColumnMeta) encodePrepareForNextRow() {
m.columnCurrIndex = 0
}
func (m *inlineColumnMeta) encodeGetColumnValue(inlineStruct reflect.Value) reflect.Value {
if inlineStruct.Kind() == reflect.Pointer {
inlineStruct = inlineStruct.Elem()
if !inlineStruct.IsValid() {
return reflect.Value{}
}
}
switch m.inlineType {
case inlineColumnStructFixed:
return inlineStruct.Field(m.targetField.Index[0])
case inlineColumnStructDynamic:
colVal := inlineStruct.Field(m.targetField.Index[0]).Index(m.columnCurrIndex)
m.columnCurrIndex++
return colVal
}
return reflect.Value{}
}