summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntti Kupila <akupila@gmail.com>2019-01-10 15:02:37 +0100
committerMartin Atkins <mart@degeneration.co.uk>2019-01-10 11:12:57 -0800
commit40e962e08e80f350603feac3ca64495a2fbfaa79 (patch)
tree03958ca010506f95a0c5e76cfb17ea11d2691c90
parentd5b12e1c082530e8f30e94ee2962fa43430a5eb9 (diff)
hclpack: fix marshalling file index positions
When marshalling, the current file index was not stored. Because of this, a ';' was inserted multiple times for each file, even if the file did not change. When unmarshalling, the fileIdx determined by number of ';' was ignored. Thus, if there were more than one file, all the positions would still point to the first file.
-rw-r--r--hclpack/positions_packed.go2
-rw-r--r--hclpack/positions_packed_test.go30
2 files changed, 32 insertions, 0 deletions
diff --git a/hclpack/positions_packed.go b/hclpack/positions_packed.go
index 0622cc5..29c7144 100644
--- a/hclpack/positions_packed.go
+++ b/hclpack/positions_packed.go
@@ -28,6 +28,7 @@ func (pp positionsPacked) MarshalBinary() ([]byte, error) {
// for a body to be entirely in one file, this can lead to considerable
// savings in that case.
delims := ppr.FileIdx - lastFileIdx
+ lastFileIdx = ppr.FileIdx
for i := 0; i < delims; i++ {
buf = buf.AppendRawByte(';')
}
@@ -65,6 +66,7 @@ func (pp *positionsPacked) UnmarshalBinary(data []byte) error {
var ppr positionPacked
var err error
+ ppr.FileIdx = fileIdx
ppr.LineDelta, buf, err = buf.ReadInt()
if err != nil {
return err
diff --git a/hclpack/positions_packed_test.go b/hclpack/positions_packed_test.go
new file mode 100644
index 0000000..fbb2ca4
--- /dev/null
+++ b/hclpack/positions_packed_test.go
@@ -0,0 +1,30 @@
+package hclpack
+
+import (
+ "testing"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func TestBinaryRoundTrip(t *testing.T) {
+ startPacked := positionsPacked{
+ {FileIdx: 0, LineDelta: 1, ColumnDelta: 2, ByteDelta: 3},
+ {FileIdx: 1, LineDelta: 2, ColumnDelta: 3, ByteDelta: 4},
+ {FileIdx: 2, LineDelta: 3, ColumnDelta: 4, ByteDelta: 5},
+ }
+
+ b, err := startPacked.MarshalBinary()
+ if err != nil {
+ t.Fatalf("Failed to marshal: %s", err)
+ }
+
+ var endPacked positionsPacked
+ err = endPacked.UnmarshalBinary(b)
+ if err != nil {
+ t.Fatalf("Failed to unmarshal: %s", err)
+ }
+
+ if !cmp.Equal(startPacked, endPacked) {
+ t.Errorf("Incorrect result\n%s", cmp.Diff(startPacked, endPacked))
+ }
+}