diff --git a/pkg/logfile/errors.go b/pkg/logfile/errors.go index bc905d0..9f17e66 100644 --- a/pkg/logfile/errors.go +++ b/pkg/logfile/errors.go @@ -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") diff --git a/pkg/logfile/types.go b/pkg/logfile/types.go new file mode 100644 index 0000000..e9c9947 --- /dev/null +++ b/pkg/logfile/types.go @@ -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 +} diff --git a/pkg/logfile/xml.go b/pkg/logfile/xml.go index 0fe7108..139e76d 100644 --- a/pkg/logfile/xml.go +++ b/pkg/logfile/xml.go @@ -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 } diff --git a/pkg/types/temperature.go b/pkg/types/temperature.go index 8714fc3..3b0f0dd 100644 --- a/pkg/types/temperature.go +++ b/pkg/types/temperature.go @@ -1,6 +1,8 @@ package types -import "time" +import ( + "time" +) // Temperature ... type Temperature struct {