fix(pkg/logfile): define own un/marshal functions for xml
This commit is contained in:
parent
e6e566793a
commit
3006e6f0e6
@ -2,11 +2,15 @@ package logfile
|
|||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var errorLogfileNotFound = errors.New("Can not find logfile")
|
|
||||||
var errorLogfileCreate = errors.New("Can not create logfile")
|
var errorLogfileCreate = errors.New("Can not create logfile")
|
||||||
var errorLogfileDecode = errors.New("Can not decode from reader")
|
var errorLogfileDecode = errors.New("Can not decode from reader")
|
||||||
var errorLogfileEncode = errors.New("Can not encode from writer")
|
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 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 errorParseFloat = errors.New("Can not parse float")
|
||||||
var errorParseTime = errors.New("Can not parse time")
|
var errorParseTime = errors.New("Can not parse time")
|
||||||
|
17
pkg/logfile/types.go
Normal file
17
pkg/logfile/types.go
Normal 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
|
||||||
|
}
|
@ -3,6 +3,8 @@ package logfile
|
|||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/go-flucky/flucky/pkg/types"
|
"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)
|
return nil, fmt.Errorf("%v: %v", errorLogfileNotFound, xl.logfile)
|
||||||
}
|
}
|
||||||
|
|
||||||
temperatures := make([]*types.Temperature, 0)
|
|
||||||
|
|
||||||
f, err := os.Open(xl.logfile)
|
f, err := os.Open(xl.logfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
return nil, fmt.Errorf("%v: %v", errorLogfileOpen, xl.logfile)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
xmlDecoder := xml.NewDecoder(f)
|
bytes, err := ioutil.ReadAll(f)
|
||||||
err = xmlDecoder.Decode(&temperatures)
|
|
||||||
if err != nil {
|
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
|
return temperatures, nil
|
||||||
@ -86,11 +100,25 @@ func (xl *xmlLogfile) WriteTemperatures(temperatures []*types.Temperature) error
|
|||||||
|
|
||||||
writeCreationDate(temperatures)
|
writeCreationDate(temperatures)
|
||||||
|
|
||||||
xmlEncoder := xml.NewEncoder(f)
|
tmpTemperatures := new(Temperatures)
|
||||||
xmlEncoder.Indent("", " ")
|
|
||||||
err = xmlEncoder.Encode(temperatures)
|
for _, temperature := range temperatures {
|
||||||
if err != nil {
|
tmpTemperature := &Temperature{
|
||||||
return fmt.Errorf("%v: %v", errorLogfileEncode, err)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package types
|
package types
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// Temperature ...
|
// Temperature ...
|
||||||
type Temperature struct {
|
type Temperature struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user