Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: seems vitodata server expects floats with "," as decimal separator #16

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ jobs:
test:
strategy:
matrix:
go-version: [1.16.x, 1.17.x, tip]
go-version: [1.21.x, 1.22.x, tip]
full-tests: [false]
include:
- go-version: 1.18.x
- go-version: 1.23.x
full-tests: true

runs-on: ubuntu-latest

steps:
- name: Setup go
run: |
curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.3/install-go.pl |
curl -sL https://raw.githubusercontent.com/maxatome/install-go/v3.6/install-go.pl |
perl - ${{ matrix.go-version }} $HOME/go
- name: Checkout code
uses: actions/checkout@v2
Expand All @@ -30,14 +30,13 @@ jobs:
if: matrix.full-tests
run: |
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh |
sh -s -- -b $HOME/go/bin v1.46.2
sh -s -- -b $HOME/go/bin v1.61.0
echo $PATH
$HOME/go/bin/golangci-lint run --max-issues-per-linter 0 \
--max-same-issues 0 \
-E asciicheck \
-E bidichk \
-E durationcheck \
-E exportloopref \
-E gocritic \
-E godot \
-E goimports \
Expand All @@ -54,7 +53,7 @@ jobs:
if [ ${{ matrix.full-tests }} = true ]; then
GO_TEST_FLAGS="-covermode=atomic -coverprofile=coverage.out"
fi
go test $GO_TEST_FLAGS ./...
go test $GO_TEST_FLAGS .
- name: Reporting
if: matrix.full-tests
env:
Expand Down
12 changes: 6 additions & 6 deletions cmd/vitotrol/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -239,7 +239,7 @@ type devicesAction struct {
authAction
}

func (a *devicesAction) Do(pOptions *Options, params []string) error {
func (a *devicesAction) Do(pOptions *Options, _ []string) error {
err := a.initVitotrol(pOptions)
if err != nil {
return err
Expand Down Expand Up @@ -267,7 +267,7 @@ func (a *listAction) NeedAuth() bool {
return false
}

func (a *listAction) Do(pOptions *Options, params []string) error {
func (a *listAction) Do(_ *Options, params []string) error {
if len(params) == 0 || params[0] == "attrs" {
for _, pAttrRef := range vitotrol.AttributesRef {
fmt.Println(pAttrRef)
Expand Down Expand Up @@ -419,7 +419,7 @@ type errorsAction struct {
authAction
}

func (a *errorsAction) Do(pOptions *Options, params []string) error {
func (a *errorsAction) Do(pOptions *Options, _ []string) error {
err := a.initVitotrol(pOptions)
if err != nil {
return err
Expand Down Expand Up @@ -465,7 +465,7 @@ func (a *setTimesheetAction) Do(pOptions *Options, params []string) error {
tss := make(map[string]vitotrol.TimeslotSlice)
var data []byte
if strings.HasPrefix(params[1], "@") && len(params[1]) > 1 {
data, err = ioutil.ReadFile(params[1][1:])
data, err = os.ReadFile(params[1][1:])
if err != nil {
return fmt.Errorf("Cannot read file %s: %s", params[1][1:], err)
}
Expand Down Expand Up @@ -548,7 +548,7 @@ type remoteAttrsAction struct {
authAction
}

func (a *remoteAttrsAction) Do(pOptions *Options, params []string) error {
func (a *remoteAttrsAction) Do(pOptions *Options, _ []string) error {
err := a.initVitotrol(pOptions)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module github.com/maxatome/go-vitotrol

go 1.18

require github.com/maxatome/go-testdeep v1.11.0
require github.com/maxatome/go-testdeep v1.14.0

require github.com/davecgh/go-spew v1.1.1 // indirect
23 changes: 2 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
github.com/davecgh/go-spew v0.0.0-20180221232628-8991bc29aa16 h1:HqkufMBR7waVfFFRABWqHa1WgTvjtVDJTLJe3CR576A=
github.com/davecgh/go-spew v0.0.0-20180221232628-8991bc29aa16/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/maxatome/go-testdeep v1.0.3 h1:xTds+j0BxPnSJbBEc3jqj8M6mUcffoIGbXCUgCs7Ybc=
github.com/maxatome/go-testdeep v1.0.3/go.mod h1:n887vr2m8DvuP4YfGAZBug2HhsEiVOC+RMvCDxo70X4=
github.com/maxatome/go-testdeep v1.0.4 h1:Oq/Hr9suAUJ3p11Xr2bcpN4Urlv1ZgJ8FysHGQvYTNo=
github.com/maxatome/go-testdeep v1.0.4/go.mod h1:n887vr2m8DvuP4YfGAZBug2HhsEiVOC+RMvCDxo70X4=
github.com/maxatome/go-testdeep v1.0.5/go.mod h1:n887vr2m8DvuP4YfGAZBug2HhsEiVOC+RMvCDxo70X4=
github.com/maxatome/go-testdeep v1.3.0 h1:r66U/hnWMFYAEwbHeZvJWmTGfXddhZ8TzHSzi2kMFJo=
github.com/maxatome/go-testdeep v1.3.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.4.0 h1:vKQh3/lHKAMsxggya/fXB6fLbf70c7k6wlLveuS9sKE=
github.com/maxatome/go-testdeep v1.4.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.7.0 h1:FdH/t2zHa+I43pKIjeUAwfUPoSADSkX0NwY6nvvxT7o=
github.com/maxatome/go-testdeep v1.7.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.8.0 h1:7QpuvRvyCUefU41jbEU1+k8N4DhuE6jbu/CvFeUUOhU=
github.com/maxatome/go-testdeep v1.8.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.9.2 h1:5D7u/JkeG0A/HDTbZ/CFFmpYZPeWN+uGQ1IbsEHYlCo=
github.com/maxatome/go-testdeep v1.9.2/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.10.0 h1:s1pP+gqiylIm2Fqhscvec1bnvGmCLayVQW8iFkGNGKw=
github.com/maxatome/go-testdeep v1.10.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.11.0 h1:Tgh5efyCYyJFGUYiT0qxBSIDeXw0F5zSoatlou685kk=
github.com/maxatome/go-testdeep v1.11.0/go.mod h1:011SgQ6efzZYAen6fDn4BqQ+lUR72ysdyKe7Dyogw70=
github.com/maxatome/go-testdeep v1.14.0 h1:rRlLv1+kI8eOI3OaBXZwb3O7xY3exRzdW5QyX48g9wI=
github.com/maxatome/go-testdeep v1.14.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM=
2 changes: 2 additions & 0 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func (t *Time) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
}

// String returns the time formatted using the format string
//
// 2006-01-02 15:04:05
//
// considered as being a localtime value.
func (t Time) String() string {
return time.Time(t).Format(vitodataTimeFormat)
Expand Down
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
4 changes: 2 additions & 2 deletions vitotrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"sort"
Expand Down Expand Up @@ -55,7 +55,7 @@ func (v *Session) sendRequest(soapAction string, reqBody string, respBody HasRes
}
defer resp.Body.Close()

respBodyRaw, _ := ioutil.ReadAll(resp.Body)
respBodyRaw, _ := io.ReadAll(resp.Body)

if resp.StatusCode == 200 {
cookies := resp.Header[http.CanonicalHeaderKey("Set-Cookie")]
Expand Down
13 changes: 5 additions & 8 deletions vitotrol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package vitotrol
import (
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand All @@ -27,7 +27,7 @@ const (
func extractRequestBody(t *td.T, r *http.Request, reqBody interface{}, testName string) bool {
t.Helper()

bodyRaw, err := ioutil.ReadAll(r.Body)
bodyRaw, err := io.ReadAll(r.Body)
if !t.CmpNoError(err, "%s: request body ReadAll OK", testName) {
return false
}
Expand All @@ -45,7 +45,8 @@ func virginInstance(pOrig interface{}) interface{} {
func testSendRequestAny(t *td.T,
sendReq func(v *Session) bool, soapAction string,
expectedRequest interface{}, serverResponse string,
testName string) bool {
testName string,
) bool {
t.Helper()

ts := httptest.NewServer(http.HandlerFunc(
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestSendRequestErrors(tt *testing.T) {

// HTTP status error
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
Expand Down Expand Up @@ -236,9 +237,7 @@ func TestSendRequest(tt *testing.T) {
"sendRequest app error")
}

//
// Login.
//
func TestLogin(tt *testing.T) {
t := td.NewT(tt)

Expand Down Expand Up @@ -294,9 +293,7 @@ func TestLogin(tt *testing.T) {
"Login with error")
}

//
// GetDevices.
//
func TestGetDevices(tt *testing.T) {
t := td.NewT(tt)

Expand Down
Loading