nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: evcc-io/evcc
Pull request #9406 (2023-08-16 15:43:45)

Sessions: fix formatting of pointer values
Fix https://github.com/evcc-io/evcc/issues/9399#event-10105475242


Patch:
diff --git a/core/db/session.go b/core/db/session.go
index 7df35bd6..b75de6f6 100644
--- a/core/db/session.go
+++ b/core/db/session.go
@@ -72,6 +72,26 @@ func (t *Sessions) writeHeader(ctx context.Context, ww *csv.Writer) error {
 	return ww.Write(row)
 }
 
+func formatValue(mp *message.Printer, value any, digits int) string {
+	if rv := reflect.ValueOf(value); rv.Kind() == reflect.Pointer && rv.IsNil() {
+		return ""
+	}
+
+	switch v := value.(type) {
+	case float64:
+		return mp.Sprint(number.Decimal(v, number.NoSeparator(), number.MaxFractionDigits(digits)))
+	case *float64:
+		return mp.Sprint(number.Decimal(*v, number.NoSeparator(), number.MaxFractionDigits(digits)))
+	case time.Time:
+		if v.IsZero() {
+			return ""
+		}
+		return v.Local().Format("2006-01-02 15:04:05")
+	default:
+		return fmt.Sprintf("%v", value)
+	}
+}
+
 func (t *Sessions) writeRow(ww *csv.Writer, mp *message.Printer, r Session) error {
 	var row []string
 	for _, f := range structs.Fields(r) {
@@ -84,20 +104,7 @@ func (t *Sessions) writeRow(ww *csv.Writer, mp *message.Printer, r Session) erro
 			digits = 0
 		}
 
-		var val string
-
-		if rv := reflect.ValueOf(f.Value()); !(rv.Kind() == reflect.Pointer && rv.IsNil()) {
-			switch v := f.Value().(type) {
-			case float64, *float64:
-				val = mp.Sprint(number.Decimal(v, number.NoSeparator(), number.MaxFractionDigits(digits)))
-			case time.Time:
-				if !v.IsZero() {
-					val = v.Local().Format("2006-01-02 15:04:05")
-				}
-			default:
-				val = fmt.Sprintf("%v", f.Value())
-			}
-		}
+		val := formatValue(mp, f.Value(), digits)
 
 		row = append(row, val)
 	}


Test patch:
diff --git a/core/db/format_test.go b/core/db/format_test.go
new file mode 100644
index 00000000..21ea67fc
--- /dev/null
+++ b/core/db/format_test.go
@@ -0,0 +1,17 @@
+package db
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"golang.org/x/text/language"
+	"golang.org/x/text/message"
+)
+
+func TestFormatValue(t *testing.T) {
+	mp := message.NewPrinter(language.Make("en"))
+
+	f := 1.2345
+	assert.Equal(t, "1.234", formatValue(mp, f, 3))
+	assert.Equal(t, "1.234", formatValue(mp, &f, 3))
+}
Repository: evcc-io/evcc
Pull request #9998 (2023-09-22 06:47:20)

chore: handle NaN values in encoder


Patch:
diff --git a/server/socket_helper.go b/server/socket_helper.go
index f1a8c2e9..0cb3c6c3 100644
--- a/server/socket_helper.go
+++ b/server/socket_helper.go
@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"math"
+	"reflect"
 	"strings"
 	"time"
 
@@ -39,8 +40,33 @@ func encode(v interface{}) (string, error) {
 	return s, nil
 }
 
+func encodeSlice(v interface{}) (string, error) {
+	rv := reflect.ValueOf(v)
+	res := make([]string, rv.Len())
+
+	for i := 0; i < rv.Len(); i++ {
+		var err error
+		if res[i], err = encode(rv.Index(i).Interface()); err != nil {
+			return "", err
+		}
+	}
+
+	return fmt.Sprintf("[%s]", strings.Join(res, ",")), nil
+}
+
 func kv(p util.Param) string {
-	val, err := encode(p.Val)
+	var (
+		val string
+		err error
+	)
+
+	// unwrap slices
+	if p.Val != nil && reflect.TypeOf(p.Val).Kind() == reflect.Slice {
+		val, err = encodeSlice(p.Val)
+	} else {
+		val, err = encode(p.Val)
+	}
+
 	if err != nil {
 		panic(err)
 	}


Test patch:
diff --git a/server/socket_test.go b/server/socket_test.go
index ffe7368f..dd40b945 100644
--- a/server/socket_test.go
+++ b/server/socket_test.go
@@ -31,3 +31,20 @@ func TestEncode(t *testing.T) {
 		assert.Equal(t, tc.out, out)
 	}
 }
+
+func TestEncodeSlice(t *testing.T) {
+	tc := []struct {
+		in  interface{}
+		out string
+	}{
+		{[]string{"a", "b"}, `["a","b"]`},
+		{[2]int64{1, 2}, `[1,2]`},
+		{[]float64{1, math.NaN()}, `[1,null]`},
+	}
+
+	for _, tc := range tc {
+		out, err := encodeSlice(tc.in)
+		assert.NoError(t, err)
+		assert.Equal(t, tc.out, out)
+	}
+}