7
7
"bytes"
8
8
"errors"
9
9
"fmt"
10
+ "math"
10
11
"os"
11
12
"slices"
12
13
"strings"
@@ -52,7 +53,8 @@ type Bodkin struct {
52
53
new * fieldPos
53
54
knownFields * omap.OrderedMap [string , * fieldPos ]
54
55
untypedFields * omap.OrderedMap [string , * fieldPos ]
55
- unificationCount int
56
+ unificationCount int64
57
+ maxCount int64
56
58
inferTimeUnits bool
57
59
quotedValuesAreStrings bool
58
60
typeConversion bool
@@ -90,7 +92,7 @@ func newBodkin(m map[string]any, opts ...Option) (*Bodkin, error) {
90
92
f := newFieldPos (b )
91
93
mapToArrow (f , m )
92
94
b .old = f
93
-
95
+ b . maxCount = int64 ( math . MaxInt64 )
94
96
return b , err
95
97
}
96
98
@@ -165,7 +167,24 @@ func (u *Bodkin) Err() []Field {
165
167
func (u * Bodkin ) Changes () error { return u .changes }
166
168
167
169
// Count returns the number of datum evaluated for schema to date.
168
- func (u * Bodkin ) Count () int { return u .unificationCount }
170
+ func (u * Bodkin ) Count () int64 { return u .unificationCount }
171
+
172
+ // MaxCount returns the maximum number of datum to be evaluated for schema.
173
+ func (u * Bodkin ) MaxCount () int64 { return u .unificationCount }
174
+
175
+ // ResetCount resets the count of datum evaluated for schema to date.
176
+ func (u * Bodkin ) ResetCount () int64 {
177
+ u .unificationCount = 0
178
+ return u .unificationCount
179
+ }
180
+
181
+ // ResetMaxCount resets the maximum number of datam to be evaluated for schema
182
+ // to maxInt64.
183
+ // ResetCount resets the count of datum evaluated for schema to date.
184
+ func (u * Bodkin ) ResetMaxCount () int64 {
185
+ u .maxCount = int64 (math .MaxInt64 )
186
+ return u .unificationCount
187
+ }
169
188
170
189
// Paths returns a slice of dotpaths of fields successfully evaluated to date.
171
190
func (u * Bodkin ) Paths () []Field {
@@ -274,11 +293,14 @@ func (u *Bodkin) ImportSchema(importPath string) (*arrow.Schema, error) {
274
293
275
294
// Unify merges structured input's column definition with the previously input's schema.
276
295
// Any uppopulated fields, empty objects or empty slices in JSON input are skipped.
277
- func (u * Bodkin ) Unify (a any ) {
296
+ func (u * Bodkin ) Unify (a any ) error {
297
+ if u .unificationCount > u .maxCount {
298
+ return fmt .Errorf ("maxcount exceeded" )
299
+ }
278
300
m , err := InputMap (a )
279
301
if err != nil {
280
302
u .err = fmt .Errorf ("%v : %v" , ErrInvalidInput , err )
281
- return
303
+ return fmt . Errorf ( "%v : %v" , ErrInvalidInput , err )
282
304
}
283
305
284
306
f := newFieldPos (u )
@@ -288,13 +310,17 @@ func (u *Bodkin) Unify(a any) {
288
310
u .merge (field , nil )
289
311
}
290
312
u .unificationCount ++
313
+ return nil
291
314
}
292
315
293
316
// Unify merges structured input's column definition with the previously input's schema,
294
317
// using a specified valid path as the root. An error is returned if the mergeAt path is
295
318
// not found.
296
319
// Any uppopulated fields, empty objects or empty slices in JSON input are skipped.
297
320
func (u * Bodkin ) UnifyAtPath (a any , mergeAt string ) error {
321
+ if u .unificationCount > u .maxCount {
322
+ return fmt .Errorf ("maxcount exceeded" )
323
+ }
298
324
mergePath := make ([]string , 0 )
299
325
if ! (len (mergeAt ) == 0 || mergeAt == "$" ) {
300
326
mergePath = strings .Split (strings .TrimPrefix (mergeAt , "$" ), "." )
0 commit comments