Skip to content

Commit e9d6e8c

Browse files
committed
fix: seems vitodata server expects floats with "," as decimal separator
Closes #15. Signed-off-by: Maxime Soulé <[email protected]>
1 parent d386531 commit e9d6e8c

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

types.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"strconv"
7+
"strings"
78
)
89

910
// Singletons matching Vitodata™ types.
@@ -56,19 +57,23 @@ func (v *VitodataDouble) Human2VitodataValue(value string) (string, error) {
5657
if err != nil {
5758
return "", err
5859
}
59-
return strconv.FormatFloat(num, 'f', -1, 64), nil
60+
return strings.Replace(strconv.FormatFloat(num, 'f', -1, 64), ".", ",", 1), nil
6061
}
6162

6263
// Vitodata2HumanValue checks that the value is a float number and
6364
// returns it after reformatting.
6465
func (v *VitodataDouble) Vitodata2HumanValue(value string) (string, error) {
65-
return v.Human2VitodataValue(value)
66+
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
67+
if err != nil {
68+
return "", err
69+
}
70+
return strconv.FormatFloat(num, 'f', -1, 64), nil
6671
}
6772

6873
// Vitodata2NativeValue extract the number from the passed string and
6974
// returns it as a float64.
7075
func (v *VitodataDouble) Vitodata2NativeValue(value string) (interface{}, error) {
71-
num, err := strconv.ParseFloat(value, 64)
76+
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
7277
if err != nil {
7378
return nil, err
7479
}

types_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestVitodataDouble(tt *testing.T) {
1414

1515
// Human2VitodataValue
1616
str, err := TypeDouble.Human2VitodataValue("1.200")
17-
t.CmpDeeply(str, "1.2")
17+
t.CmpDeeply(str, "1,2")
1818
t.CmpNoError(err)
1919

2020
str, err = TypeDouble.Human2VitodataValue("foo")
@@ -26,6 +26,10 @@ func TestVitodataDouble(tt *testing.T) {
2626
t.CmpDeeply(str, "1.2")
2727
t.CmpNoError(err)
2828

29+
str, err = TypeDouble.Vitodata2HumanValue("1,200")
30+
t.CmpDeeply(str, "1.2")
31+
t.CmpNoError(err)
32+
2933
str, err = TypeDouble.Vitodata2HumanValue("foo")
3034
t.Empty(str)
3135
t.CmpError(err)
@@ -35,6 +39,10 @@ func TestVitodataDouble(tt *testing.T) {
3539
t.CmpDeeply(num, float64(1.2))
3640
t.CmpNoError(err)
3741

42+
num, err = TypeDouble.Vitodata2NativeValue("1,200")
43+
t.CmpDeeply(num, float64(1.2))
44+
t.CmpNoError(err)
45+
3846
num, err = TypeDouble.Vitodata2NativeValue("foo")
3947
t.Nil(num)
4048
t.CmpError(err)

0 commit comments

Comments
 (0)