summaryrefslogtreecommitdiff
path: root/rrdformat
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2020-02-01 10:22:14 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2020-02-01 10:22:14 -0500
commit096410449cad14b32b41a6de1ef56f16ef3d25fb (patch)
treececcc45cc59d819ec897818bbecd8129c6b4d580 /rrdformat
parentbe65ef54f7d2371602ae5999d9955d4bf9f48cd5 (diff)
fiddle with unmarshaling of numbers
Diffstat (limited to 'rrdformat')
-rw-r--r--rrdformat/marshal_xml.go2
-rw-r--r--rrdformat/rrdbinary/types.go31
2 files changed, 24 insertions, 9 deletions
diff --git a/rrdformat/marshal_xml.go b/rrdformat/marshal_xml.go
index fb6c9e1..2d0269b 100644
--- a/rrdformat/marshal_xml.go
+++ b/rrdformat/marshal_xml.go
@@ -1,8 +1,8 @@
package rrdformat
import (
- "strconv"
"encoding/xml"
+ "strconv"
)
//const XMLNS = "https://oss.oetiker.ch/rrdtool/rrdtool-dump.xml"
diff --git a/rrdformat/rrdbinary/types.go b/rrdformat/rrdbinary/types.go
index 47df26e..e3011d5 100644
--- a/rrdformat/rrdbinary/types.go
+++ b/rrdformat/rrdbinary/types.go
@@ -2,8 +2,9 @@ package rrdbinary
import (
"encoding/binary"
- "encoding/json"
+ "fmt"
"math"
+ "strconv"
)
type Architecture struct {
@@ -30,16 +31,30 @@ type Unival uint64 // 8 bytes
type Time int64 // 4 or 8 bytes, only has second-precision
type EOF struct{} // 0 bytes
-func (u Unival) AsUint64() uint64 { return uint64(u) }
-func (u Unival) AsFloat64() float64 { return math.Float64frombits(uint64(u)) }
+func (u Unival) AsULong() ULong { return ULong(u) }
+func (u Unival) AsDouble() Double { return Double(math.Float64frombits(uint64(u))) }
+
+func (u Unival) String() string {
+ return fmt.Sprintf("{ .ulong=%d; .double=%s }", u.AsULong(), u.AsDouble().String())
+}
+
+func (u Unival) MarshalText() ([]byte, error) {
+ return []byte(u.String()), nil
+}
-// MarshalJSON is for my own debugging.
func (f Double) MarshalJSON() ([]byte, error) {
- raw := float64(f)
- if math.IsNaN(raw) {
- return json.Marshal("NaN")
+ if math.IsNaN(float64(f)) {
+ return []byte(`"NaN"`), nil
}
- return json.Marshal(raw)
+ return f.MarshalText()
+}
+
+func (f Double) MarshalText() ([]byte, error) {
+ return []byte(f.String()), nil
+}
+
+func (f Double) String() string {
+ return strconv.FormatFloat(float64(f), 'e', 10, 64)
}
// Statically assert that each of the above types implements the