generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
164 lines (127 loc) · 3.33 KB
/
table.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
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
package dbsteps
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/swaggest/form/v5"
)
const null = "NULL"
// TableMapper maps data from Go value to string and back.
type TableMapper struct {
Decoder *form.Decoder
Encoder *form.Encoder
}
func isNil(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr && rv.IsZero() {
return true
}
return false
}
// Encode converts Go value to string.
func (m *TableMapper) Encode(v interface{}) (string, error) {
if m.Encoder == nil {
m.Encoder = form.NewEncoder()
}
if isNil(v) {
return null, nil
}
vv, err := m.Encoder.Encode(v)
if err != nil {
return "", fmt.Errorf("failed to stringify variable value of type %T: %w", v, err)
}
return vv[""][0], nil
}
// SliceFromTable creates a slice from gherkin table, item type is used as slice element type.
func (m *TableMapper) SliceFromTable(data [][]string, item interface{}) (interface{}, error) {
itemType := reflect.TypeOf(item)
if itemType == nil {
return nil, errNilItemStruct
}
if itemType.Kind() == reflect.Ptr {
itemType = itemType.Elem()
}
result := reflect.MakeSlice(reflect.SliceOf(itemType), len(data)-1, len(data)-1)
err := m.IterateTable(IterateConfig{
Data: data, Item: item,
ReceiveRow: func(index int, row interface{}, colNames []string, rawValues []string) error {
result.Index(index).Set(reflect.Indirect(reflect.ValueOf(row)))
return nil
},
})
if err != nil {
return nil, err
}
return result.Interface(), nil
}
// IterateConfig controls behavior of TableMapper.IterateTable.
type IterateConfig struct {
Data [][]string
SkipDecode func(column, value string) bool
Item interface{}
Replaces map[string]string
ReceiveRow func(index int, row interface{}, colNames []string, rawValues []string) error
}
var (
errNilItemStruct = errors.New("nil item struct received")
errRowRequired = errors.New("header and at least one row required in table")
)
func itemType(v interface{}) (reflect.Type, error) {
itemType := reflect.TypeOf(v)
if itemType == nil {
return nil, errNilItemStruct
}
if itemType.Kind() == reflect.Ptr {
itemType = itemType.Elem()
}
return itemType, nil
}
// IterateTable walks gherkin table calling row receiver with mapped row.
// If receiver returns error iteration stops and error is propagated.
func (m *TableMapper) IterateTable(c IterateConfig) error {
if m.Decoder == nil {
m.Decoder = form.NewDecoder()
}
if len(c.Data) < 2 {
return errRowRequired
}
colNames := c.Data[0]
itemType, err := itemType(c.Item)
if err != nil {
return err
}
values := make(map[string][]string, len(colNames))
for rowIndex, row := range c.Data[1:] {
itemBuf := reflect.New(itemType)
raw := make([]string, 0, len(colNames))
for i, cell := range row {
raw = append(raw, cell)
if c.SkipDecode != nil && c.SkipDecode(colNames[i], cell) {
continue
}
cell = strings.TrimSuffix(cell, "::string")
if v, found := c.Replaces[cell]; found {
cell = v
}
if cell != null {
values[colNames[i]] = []string{cell}
} else {
delete(values, colNames[i])
}
}
val := itemBuf.Interface()
err := m.Decoder.Decode(val, values)
if err != nil {
return err
}
err = c.ReceiveRow(rowIndex, itemBuf.Interface(), colNames, raw)
if err != nil {
return err
}
}
return nil
}