Skip to content

Commit

Permalink
fix: seems vitodata server expects floats with "," as decimal separator
Browse files Browse the repository at this point in the history
Closes #15.

Signed-off-by: Maxime Soulé <[email protected]>
  • Loading branch information
maxatome committed Sep 19, 2024
1 parent d386531 commit e9d6e8c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 8 additions & 3 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strconv"
"strings"
)

// Singletons matching Vitodata™ types.
Expand Down Expand Up @@ -56,19 +57,23 @@ func (v *VitodataDouble) Human2VitodataValue(value string) (string, error) {
if err != nil {
return "", err
}
return strconv.FormatFloat(num, 'f', -1, 64), nil
return strings.Replace(strconv.FormatFloat(num, 'f', -1, 64), ".", ",", 1), nil
}

// Vitodata2HumanValue checks that the value is a float number and
// returns it after reformatting.
func (v *VitodataDouble) Vitodata2HumanValue(value string) (string, error) {
return v.Human2VitodataValue(value)
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
if err != nil {
return "", err
}
return strconv.FormatFloat(num, 'f', -1, 64), nil
}

// Vitodata2NativeValue extract the number from the passed string and
// returns it as a float64.
func (v *VitodataDouble) Vitodata2NativeValue(value string) (interface{}, error) {
num, err := strconv.ParseFloat(value, 64)
num, err := strconv.ParseFloat(strings.Replace(value, ",", ".", 1), 64)
if err != nil {
return nil, err
}
Expand Down
10 changes: 9 additions & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestVitodataDouble(tt *testing.T) {

// Human2VitodataValue
str, err := TypeDouble.Human2VitodataValue("1.200")
t.CmpDeeply(str, "1.2")
t.CmpDeeply(str, "1,2")
t.CmpNoError(err)

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

str, err = TypeDouble.Vitodata2HumanValue("1,200")
t.CmpDeeply(str, "1.2")
t.CmpNoError(err)

str, err = TypeDouble.Vitodata2HumanValue("foo")
t.Empty(str)
t.CmpError(err)
Expand All @@ -35,6 +39,10 @@ func TestVitodataDouble(tt *testing.T) {
t.CmpDeeply(num, float64(1.2))
t.CmpNoError(err)

num, err = TypeDouble.Vitodata2NativeValue("1,200")
t.CmpDeeply(num, float64(1.2))
t.CmpNoError(err)

num, err = TypeDouble.Vitodata2NativeValue("foo")
t.Nil(num)
t.CmpError(err)
Expand Down

0 comments on commit e9d6e8c

Please sign in to comment.