-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.go
286 lines (253 loc) · 8 KB
/
common.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package lasgo
import (
"context"
"errors"
"fmt"
"reflect"
"regexp"
"runtime"
"strings"
"github.com/mitchellh/mapstructure"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
)
// StructorConfig is used to expose a subset of the configuration options
// provided by the mapstructure package.
//
// See: https://godoc.org/github.com/mitchellh/mapstructure#DecoderConfig
type StructorConfig struct {
// DecodeHook, if set, will be called before any decoding and any
// type conversion (if WeaklyTypedInput is on). This lets you modify
// the values before they're set down onto the resulting struct.
//
// If an error is returned, the entire decode will fail with that
// error.
DecodeHook mapstructure.DecodeHookFunc
// If WeaklyTypedInput is true, the decoder will make the following
// "weak" conversions:
//
// - bools to string (true = "1", false = "0")
// - numbers to string (base 10)
// - bools to int/uint (true = 1, false = 0)
// - strings to int/uint (base implied by prefix)
// - int to bool (true if value != 0)
// - string to bool (accepts: 1, t, T, TRUE, true, True, 0, f, F,
// FALSE, false, False. Anything else is an error)
// - empty array = empty map and vice versa
// - negative numbers to overflowed uint values (base 10)
// - slice of maps to a merged map
// - single values are converted to slices if required. Each
// element is weakly decoded. For example: "4" can become []int{4}
// if the target type is an int slice.
//
WeaklyTypedInput bool
}
// PostUnmarshaler allows you to further modify all results after unmarshaling.
// The ConcreteStruct pointer must implement this interface to make use of this feature.
type PostUnmarshaler interface {
// PostUnmarshal is called for each row after all results have been fetched.
// You can use it to further modify the values of each ConcreteStruct.
PostUnmarshal(ctx context.Context, row, count int) error
}
// DataOptions is used to modify the default behavior.
type DataOptions struct {
// ConcreteStruct can be set to any concrete struct (not a pointer).
// When set, the mapstructure package is used to convert the returned
// results automatically from a map to a struct. The `dbq` struct tag
// can be used to map column names to the struct's fields.
//
// See: https://godoc.org/github.com/mitchellh/mapstructure
ConcreteStruct interface{}
// DecoderConfig is used to configure the decoder used by the mapstructure
// package. If it's not supplied, a default StructorConfig is assumed. This means
// WeaklyTypedInput is set to true and no DecodeHook is provided.
//
// See: https://godoc.org/github.com/mitchellh/mapstructure
DecoderConfig *StructorConfig
// ConcurrentPostUnmarshal can be set to true if PostUnmarshal must be called concurrently.
ConcurrentPostUnmarshal bool
}
// index returns the position of an element in a slice of strings
func index(slice []string, item string) int {
for i := range slice {
if slice[i] == item {
return i
}
}
return -1
}
func removeComment(str string) []string {
trimmedStr := strings.TrimSpace(str)
strVec := strings.Split(trimmedStr, "\n")
var result []string
for _, line := range strVec {
fStr := strings.TrimSpace(line)
if !strings.HasPrefix(fStr, "#") && len(fStr) > 0 {
result = append(result, fStr)
}
}
return result
}
func pattern(str string) *regexp.Regexp {
return regexp.MustCompile(str)
}
func chunk(s []string, n int) (store [][]string) {
for i := 0; i < len(s); i += n {
if i+n >= len(s) {
store = append(store, s[i:])
} else {
store = append(store, s[i:i+n])
}
}
return
}
// metadata - picks out version and wrap state of the file
func metadata(str string) (version string, wrap bool) {
sB := strings.Split(pattern("~V(?:\\w*\\s*)*\n\\s*").Split(str, 2)[1], "~")[0]
sw := removeComment(sB)
accum := [][]string{}
for _, val := range sw {
current := pattern("\\s{2,}|\\s*:").Split(val, -1)[0:2]
accum = append(accum, current)
}
version = accum[0][1]
if strings.ToLower(accum[1][1]) == "yes" {
wrap = true
} else {
wrap = false
}
return
}
func property(str string, key string) (property map[string]WellProps, err error) {
err = errors.New("property cannot be found")
property = make(map[string]WellProps)
regDict := map[string]string{
"curve": "~C(?:\\w*\\s*)*\\n\\s*",
"param": "~P(?:\\w*\\s*)*\\n\\s*",
"well": "~W(?:\\w*\\s*)*\\n\\s*",
}
prop, ok := regDict[key]
if !ok {
return
}
substr := pattern(prop).Split(str, 2)
var sw []string
if len(substr) > 1 {
sw = removeComment(strings.Split(substr[1], "~")[0])
}
if len(sw) > 0 {
for _, val := range sw {
root := pattern("\\s*[.]\\s+").ReplaceAllString(val, " none ")
title := pattern("[.]|\\s+").Split(root, 2)[0]
unit := pattern("\\s+").Split(pattern("^\\w+\\s*[.]*s*").Split(root, 2)[1], 2)[0]
desc := strings.TrimSpace(strings.Split(root, ":")[1])
desc = pattern("\\d+\\s*").ReplaceAllString(desc, "")
if len(desc) < 1 {
desc = "none"
}
vD := pattern("\\s{2,}\\w*\\s{2,}").Split(strings.Split(root, ":")[0], -1)
var value string
if len(vD) > 2 && len(vD[len(vD)-1]) > 0 {
value = strings.TrimSpace(vD[len(vD)-2])
} else {
value = strings.TrimSpace(vD[len(vD)-1])
}
property[title] = WellProps{unit, desc, value}
}
return property, nil
}
return
}
func structConvert(ctx context.Context, vals *[][]string, header []string, o *DataOptions) ([]interface{}, error) {
var (
outStruct = []interface{}{}
)
for _, row := range *vals {
// map header to row value
rowMap := map[string]interface{}{}
if len(header) != len(row) {
return nil, fmt.Errorf("length of each row must be same as length of header")
}
for idx, field := range row {
headerI := strings.ToLower(header[idx])
rowMap[headerI] = field
}
res := reflect.New(reflect.TypeOf(o.ConcreteStruct)).Interface()
if o.DecoderConfig != nil {
dc := &mapstructure.DecoderConfig{
DecodeHook: o.DecoderConfig.DecodeHook,
ZeroFields: true,
TagName: "las",
WeaklyTypedInput: o.DecoderConfig.WeaklyTypedInput,
Result: res,
}
decoder, err := mapstructure.NewDecoder(dc)
if err != nil {
return nil, err
}
err = decoder.Decode(rowMap)
if err != nil {
return nil, err
}
} else {
dc := &mapstructure.DecoderConfig{
ZeroFields: true,
TagName: "las",
WeaklyTypedInput: true,
Result: res,
}
decoder, err := mapstructure.NewDecoder(dc)
if err != nil {
return nil, err
}
err = decoder.Decode(rowMap)
if err != nil {
return nil, err
}
}
outStruct = append(outStruct, res)
}
// PostUnmarshal code
if len(outStruct) > 0 {
csTyp := reflect.TypeOf(reflect.New(reflect.TypeOf(o.ConcreteStruct)).Interface())
ics := reflect.TypeOf((*PostUnmarshaler)(nil)).Elem()
if csTyp.Implements(ics) {
rows := reflect.ValueOf(outStruct)
count := rows.Len()
if o.ConcurrentPostUnmarshal && runtime.GOMAXPROCS(0) > 1 {
g, newCtx := errgroup.WithContext(ctx)
for i := 0; i < count; i++ {
i := i
g.Go(func() error {
if err := newCtx.Err(); err != nil {
return err
}
row := reflect.ValueOf(rows.Index(i).Interface())
retVals := row.MethodByName("PostUnmarshal").Call([]reflect.Value{reflect.ValueOf(newCtx), reflect.ValueOf(i), reflect.ValueOf(count)})
err := retVals[0].Interface()
if err != nil {
return xerrors.Errorf("lasData.PostUnmarshal @ row %d: %w", i, err)
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
} else {
for i := 0; i < count; i++ {
if err := ctx.Err(); err != nil {
return nil, err
}
row := reflect.ValueOf(rows.Index(i).Interface())
retVals := row.MethodByName("PostUnmarshal").Call([]reflect.Value{reflect.ValueOf(ctx), reflect.ValueOf(i), reflect.ValueOf(count)})
err := retVals[0].Interface()
if err != nil {
return nil, xerrors.Errorf("lasData.PostUnmarshal @ row %d: %w", i, err)
}
}
}
}
}
return outStruct, nil
}