Skip to content

Commit

Permalink
Merge pull request #45 from wenyihu6/add-float64arr
Browse files Browse the repository at this point in the history
add scan implementation for float64, time.Duration, and []float64
  • Loading branch information
wenyihu6 authored Aug 1, 2023
2 parents be42291 + f4b8e52 commit e384cf4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
24 changes: 24 additions & 0 deletions datadriven.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/pmezard/go-difflib/difflib"
)
Expand Down Expand Up @@ -651,7 +652,18 @@ func (arg CmdArg) scanAllErr(dest interface{}) error {
(*dest)[i] = uint64(n)
}
return nil
case *[]float64:
*dest = make([]float64, len(arg.Vals))
for i := 0; i < len(arg.Vals); i++ {
n, err := strconv.ParseFloat(arg.Vals[i], 64)
if err != nil {
return fmt.Errorf("arg %d: %w", i, err)
}
(*dest)[i] = float64(n)
}
return nil
}

// If there's a single value and `dest` is a supported scalar type, we might
// still be able to scan it.
if len(arg.Vals) == 1 {
Expand Down Expand Up @@ -693,6 +705,18 @@ func (arg CmdArg) scanScalarErr(i int, dest interface{}) error {
return err
}
*dest = b
case *float64:
t, err := strconv.ParseFloat(val, 64)
if err != nil {
return err
}
*dest = t
case *time.Duration:
t, err := time.ParseDuration(val)
if err != nil {
return err
}
*dest = t
default:
return fmt.Errorf("unsupported type %T for destination #%d (might be easy to add it)", dest, i+1)
}
Expand Down
25 changes: 25 additions & 0 deletions datadriven_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"sort"
"strings"
"testing"
"time"

"github.com/pmezard/go-difflib/difflib"
)
Expand Down Expand Up @@ -303,6 +304,18 @@ string vals=(foo)
bool vals=true
----
true
float64 vals=1.1
----
1.1
[]float64 vals=(1.1, 2.2, 3.3, 4.4)
----
[]float64{1.1, 2.2, 3.3, 4.4}
time.Duration vals=10.0m
----
10m0s
`, func(t *testing.T, d *TestData) string {
switch d.Cmd {
case "[]string":
Expand All @@ -317,6 +330,18 @@ true
var dest1, dest2 []uint64
checkScanEquivalence(d, &dest1, &dest2)
return fmt.Sprintf("%#v", dest1)
case "[]float64":
var dest1, dest2 []float64
checkScanEquivalence(d, &dest1, &dest2)
return fmt.Sprintf("%#v", dest1)
case "float64":
var dest1, dest2 float64
checkScanEquivalence(d, &dest1, &dest2)
return fmt.Sprintf("%#v", dest1)
case "time.Duration":
var dest1, dest2 time.Duration
checkScanEquivalence(d, &dest1, &dest2)
return fmt.Sprintf("%s", dest1)
case "string":
var dest1, dest2 string
checkScanEquivalence(d, &dest1, &dest2)
Expand Down

0 comments on commit e384cf4

Please sign in to comment.