Skip to content

Commit f51204c

Browse files
committed
Fix InferString
1 parent 46856ca commit f51204c

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

inferrer.go

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package jtdinfer
22

33
import (
44
"encoding/json"
5-
"strconv"
65
)
76

87
// Inferrer represents the `InferredSchema` with its state combined with the
@@ -34,37 +33,18 @@ func (i *Inferrer) IntoSchema() Schema {
3433
}
3534

3635
// InferStrings accepts a slice of strings and will try to JSON unmarshal each
37-
// row to the type that the first row looks like. If an error occurs the
38-
// inferrer will return with the state it had when the error occurred.
39-
// If you already have the type of your data such as a slice of numbers or a map
40-
// of strings you can pass them directly to `Infer`. This is just a convenience
41-
// method if all you got is strings.
36+
// row. If an error occurs the inferrer will return with the state it had when
37+
// the error occurred. If you already have the type of your data such as a slice
38+
// of numbers or a map of strings you can pass them directly to `Infer`. This is
39+
// just a convenience method if all you got is strings.
4240
func InferStrings(rows []string, hints *Hints) *Inferrer {
4341
inferrer := NewInferrer(hints)
4442
if len(rows) == 0 {
4543
return inferrer
4644
}
4745

48-
var (
49-
firstRow = rows[0]
50-
getToInfer func() any
51-
)
52-
53-
switch {
54-
case isBool(firstRow):
55-
getToInfer = func() any { return false }
56-
case isObject(firstRow):
57-
getToInfer = func() any { return make(map[string]any) }
58-
case isArray(firstRow):
59-
getToInfer = func() any { return make([]any, 0) }
60-
case isNumber(firstRow):
61-
getToInfer = func() any { return 0.0 }
62-
default:
63-
getToInfer = func() any { return "" }
64-
}
65-
6646
for _, row := range rows {
67-
toInfer := getToInfer()
47+
var toInfer any
6848
if err := json.Unmarshal([]byte(row), &toInfer); err != nil {
6949
return inferrer
7050
}
@@ -74,22 +54,3 @@ func InferStrings(rows []string, hints *Hints) *Inferrer {
7454

7555
return inferrer
7656
}
77-
78-
func isBool(value string) bool {
79-
return value == "true" || value == "false"
80-
}
81-
82-
func isObject(value string) bool {
83-
var m map[string]any
84-
return json.Unmarshal([]byte(value), &m) == nil
85-
}
86-
87-
func isArray(value string) bool {
88-
var a []any
89-
return json.Unmarshal([]byte(value), &a) == nil
90-
}
91-
92-
func isNumber(value string) bool {
93-
_, err := strconv.ParseFloat(value, 64)
94-
return err == nil
95-
}

0 commit comments

Comments
 (0)