fix(pkg/logfile): define own un/marshal functions for xml

This commit is contained in:
Markus Pesch 2019-06-21 13:37:29 +02:00
parent e6e566793a
commit 3006e6f0e6
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
4 changed files with 63 additions and 12 deletions

View File

@ -2,11 +2,15 @@ package logfile
import "errors"
var errorLogfileNotFound = errors.New("Can not find logfile")
var errorLogfileCreate = errors.New("Can not create logfile")
var errorLogfileDecode = errors.New("Can not decode from reader")
var errorLogfileEncode = errors.New("Can not encode from writer")
var errorLogfileMarshal = errors.New("Can not marshal values")
var errorLogfileNotFound = errors.New("Can not find logfile")
var errorLogfileOpen = errors.New("Can not open logfile")
var errorLogfileRead = errors.New("Can not read from given reader")
var errorLogfileUnmarshal = errors.New("Can not unmarshal values")
var errorLogfileWrite = errors.New("Can not write with given writer")
var errorParseFloat = errors.New("Can not parse float")
var errorParseTime = errors.New("Can not parse time")

17
pkg/logfile/types.go Normal file
View File

@ -0,0 +1,17 @@
package logfile
import (
"encoding/xml"
"github.com/go-flucky/flucky/pkg/types"
)
type Temperatures struct {
XMLName xml.Name `xml:"temperatures"`
Temperatures []*Temperature `xml:"temperature"`
}
type Temperature struct {
XMLName xml.Name `xml:"temperature"`
*types.Temperature
}

View File

@ -3,6 +3,8 @@ package logfile
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/go-flucky/flucky/pkg/types"
@ -43,18 +45,30 @@ func (xl *xmlLogfile) ReadTemperatures() ([]*types.Temperature, error) {
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
}
temperatures := make([]*types.Temperature, 0)
f, err := os.Open(xl.logfile)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
}
defer f.Close()
xmlDecoder := xml.NewDecoder(f)
err = xmlDecoder.Decode(&temperatures)
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileDecode, err)
return nil, fmt.Errorf("%v: %v", errorLogfileRead, err)
}
tmpTemperatures := new(Temperatures)
err = xml.Unmarshal(bytes, tmpTemperatures)
if err != nil {
return nil, fmt.Errorf("%v: %v", errorLogfileUnmarshal, err)
}
log.Println(len(tmpTemperatures.Temperatures))
temperatures := make([]*types.Temperature, 0)
for _, tmpTemperature := range tmpTemperatures.Temperatures {
temperatures = append(temperatures, tmpTemperature.Temperature)
}
return temperatures, nil
@ -86,11 +100,25 @@ func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error
writeCreationDate(temperatures)
xmlEncoder := xml.NewEncoder(f)
xmlEncoder.Indent("", " ")
err = xmlEncoder.Encode(temperatures)
if err != nil {
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
tmpTemperatures := new(Temperatures)
for _, temperature := range temperatures {
tmpTemperature := &Temperature{
Temperature: temperature,
}
tmpTemperatures.Temperatures = append(tmpTemperatures.Temperatures, tmpTemperature)
}
bytes, err := xml.MarshalIndent(tmpTemperatures, "", " ")
if err != nil {
return fmt.Errorf("%v: %v", errorLogfileMarshal, err)
}
_, err = f.Write(bytes)
if err != nil {
return fmt.Errorf("%v: %v", errorLogfileWrite, err)
}
return nil
}

View File

@ -1,6 +1,8 @@
package types
import "time"
import (
"time"
)
// Temperature ...
type Temperature struct {