summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2020-02-03 00:30:31 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2020-02-03 00:30:31 -0500
commitdff188a6157c10ab6745b1a389c925794d6ca1ba (patch)
treef498118587f6db75dd19c6657407714711cbe250
parent8c121d944aafa0b682030156f6c5dd743dfab607 (diff)
fix
-rw-r--r--rrdformat/marshal_xml.go32
-rw-r--r--rrdformat/rpn.go32
-rw-r--r--rrdformat/rrdbinary/decode.go2
-rw-r--r--rrdformat/sniff.go2
4 files changed, 44 insertions, 24 deletions
diff --git a/rrdformat/marshal_xml.go b/rrdformat/marshal_xml.go
index 7b97894..cb2db93 100644
--- a/rrdformat/marshal_xml.go
+++ b/rrdformat/marshal_xml.go
@@ -3,6 +3,8 @@ package rrdformat
import (
"encoding/xml"
"strconv"
+
+ "git.lukeshu.com/go/librrd/rrdformat/rrdbinary"
)
//const XMLNS = "https://oss.oetiker.ch/rrdtool/rrdtool-dump.xml"
@@ -50,7 +52,7 @@ func (rrd RRD) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
}
// 3. Data Sources
for _, ds := range rrd.DSDefs {
- if err := e.EncodeElement(ds, xmlStart("ds")); err != nil {
+ if err := ds.marshalXML(rrd, e, xmlStart("ds")); err != nil {
return err
}
}
@@ -87,7 +89,7 @@ func (t TimeWithUsec) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
return nil
}
-func (ds DSDef) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
+func (ds DSDef) marshalXML(rrd RRD, e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start); err != nil {
return err
}
@@ -101,29 +103,14 @@ func (ds DSDef) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
switch ds.DSType {
case DST_CDEF:
var rpnps []rrdbinary.RPNToken
- for _, param := range ds.Params {
+ for _, param := range ds.Parameters {
rpnps = append(rpnps, param.AsRPNTokens()...)
}
- strs := make([]string, len(rpnps))
- for _, rpnp := range rpnps {
- switch Op(rpnp.Op) {
- case OP_NUMBER:
- strs = append(strs, fmt.Sprintf("%d", rpnp.Val))
- case OP_VARIABLE:
- if rpnp.Val < 0 || rpnp.Val >= len(rrd.DSDefs) {
- return fmt.Errorf("out-of-bounds %s %d", Op(rpnp.Op), rpnp.Val)
- }
- strs = append(strs, rrd.DSDefs[int(rpnp.Val)].Name)
- case OP_PREV_OTHER:
- if rpnp.Val < 0 || rpnp.Val >= len(rrd.DSDefs) {
- return fmt.Errorf("out-of-bounds %s %d", Op(rpnp.Op), rpnp.Val)
- }
- strs = append(strs, fmt.Sprintf("PREV(%s)", rrd.DSDefs[int(rpnp.Val)].Name))
- default:
- strs = append(strs, rpnp.Op.String())
- }
+ str, err := rrd.RPNCompactToString(rpnps)
+ if err != nil {
+ return err
}
- if err := e.EncodeElement(strings.Join(strs, ","), xmlStart("cdef")); err != nil {
+ if err := e.EncodeElement(str, xmlStart("cdef")); err != nil {
return err
}
default:
@@ -133,4 +120,5 @@ func (ds DSDef) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if err := e.EncodeToken(start.End()); err != nil {
return err
}
+ return nil
}
diff --git a/rrdformat/rpn.go b/rrdformat/rpn.go
index 386c4ca..883a730 100644
--- a/rrdformat/rpn.go
+++ b/rrdformat/rpn.go
@@ -2,6 +2,13 @@
package rrdformat
+import (
+ "fmt"
+ "strings"
+
+ "git.lukeshu.com/go/librrd/rrdformat/rrdbinary"
+)
+
type Op uint8
const (
@@ -93,3 +100,28 @@ const (
OP_PERCENT
OP_POW
)
+
+func (rrd RRD) RPNCompactToString(rpnps []rrdbinary.RPNToken) (string, error) {
+ strs := make([]string, len(rpnps))
+ for _, rpnp := range rpnps {
+ switch Op(rpnp.Op) {
+ case OP_NUMBER:
+ strs = append(strs, fmt.Sprintf("%d", rpnp.Val))
+ case OP_VARIABLE:
+ if rpnp.Val < 0 || int(rpnp.Val) >= len(rrd.DSDefs) {
+ return "", fmt.Errorf("out-of-bounds %s %d", Op(rpnp.Op), rpnp.Val)
+ }
+ strs = append(strs, string(rrd.DSDefs[int(rpnp.Val)].DSName))
+ case OP_PREV_OTHER:
+ if rpnp.Val < 0 || int(rpnp.Val) >= len(rrd.DSDefs) {
+ return "", fmt.Errorf("out-of-bounds %s %d", Op(rpnp.Op), rpnp.Val)
+ }
+ strs = append(strs, fmt.Sprintf("PREV(%s)", rrd.DSDefs[int(rpnp.Val)].DSName))
+ case OP_END:
+ break
+ default:
+ strs = append(strs, Op(rpnp.Op).String())
+ }
+ }
+ return strings.Join(strs, ","), nil
+}
diff --git a/rrdformat/rrdbinary/decode.go b/rrdformat/rrdbinary/decode.go
index c4f7760..81d9b05 100644
--- a/rrdformat/rrdbinary/decode.go
+++ b/rrdformat/rrdbinary/decode.go
@@ -275,7 +275,7 @@ func (obj *Unival) unmarshalRRD(d *Decoder, tag string) error {
*obj = Unival{
arch: d.arch,
- data: data,
+ data: data[:d.arch.UnivalWidth],
}
d.pos += padding + d.arch.UnivalWidth
return nil
diff --git a/rrdformat/sniff.go b/rrdformat/sniff.go
index a6a8c6d..099655f 100644
--- a/rrdformat/sniff.go
+++ b/rrdformat/sniff.go
@@ -136,7 +136,7 @@ func SniffArchitecture(data []byte) (rrdbinary.Architecture, error) {
// For now, just assume it's sizeof(long)/2, which is true on
// i686, x86_64, and arm. (It is not true on alpha or ia64.)
arch.ShortWidth = arch.LongWidth / 2
- arch.Shortalign = arch.LongAlign / 2
+ arch.ShortAlign = arch.LongAlign / 2
return arch, nil
}