tape: WIP

This commit is contained in:
Sasha Koshka 2025-07-02 06:25:24 -04:00
parent 2138d47f07
commit 07fc77c83e
2 changed files with 65 additions and 2 deletions

View File

@ -6,6 +6,8 @@ package tape
// TODO: add support for struct tags: `tape:"0000"`, tape:"0001"` so they can get // TODO: add support for struct tags: `tape:"0000"`, tape:"0001"` so they can get
// transformed into tables with a defined schema // transformed into tables with a defined schema
// TODO: test all of these smaller functions individually
import "fmt" import "fmt"
import "reflect" import "reflect"
@ -346,10 +348,12 @@ func peekSlice(decoder *Decoder, tag Tag) (Tag, int, error) {
for { for {
elem, populated, n, err := peekSliceOnce(decoder, tag, offset) elem, populated, n, err := peekSliceOnce(decoder, tag, offset)
if err != nil { return 0, 0, err } if err != nil { return 0, 0, err }
offset += n offset = n
dimension += 1 dimension += 1
if elem.Is(OTA) { if elem.Is(OTA) {
if !populated { if !populated {
// default to a large byte array, will be
// interpreted as a string.
return LBA, dimension + 1, nil return LBA, dimension + 1, nil
} }
} else { } else {
@ -371,7 +375,7 @@ func peekSliceOnce(decoder *Decoder, tag Tag, offset int) (elem Tag, populated b
headerBytes, err := decoder.Peek(elemTagEnd) headerBytes, err := decoder.Peek(elemTagEnd)
if err != nil { return 0, false, 0, err } if err != nil { return 0, false, 0, err }
elem = Tag(headerBytes[len(headerBytes)]) elem = Tag(headerBytes[len(headerBytes) - 1])
for index := lengthStart; index < lengthEnd; index += 1 { for index := lengthStart; index < lengthEnd; index += 1 {
if headerBytes[index] > 0 { if headerBytes[index] > 0 {
populated = true populated = true

View File

@ -67,6 +67,65 @@ func TestEncodeDecodeAnyMap(test *testing.T) {
if err != nil { test.Fatal(err) } if err != nil { test.Fatal(err) }
} }
func TestPeekSlice(test *testing.T) {
buffer := bytes.NewBuffer([]byte {
2, byte(OTA.WithCN(3)),
0, 0, 0, 1, byte(LI.WithCN(1)),
0, 0x5,
2, byte(LI.WithCN(1)),
0, 0x17,
0xAA, 0xAA,
})
decoder := NewDecoder(buffer)
elem, dimension, err := peekSlice(decoder, OTA.WithCN(0))
if err != nil { test.Fatal(err) }
if elem != LI.WithCN(1) {
test.Fatalf("wrong element tag: %v %02X", elem, byte(elem))
}
if got, correct := dimension, 2; got != correct {
test.Fatalf("wrong dimension: %d != %d", got, correct)
}
}
func TestPeekSliceOnce(test *testing.T) {
buffer := bytes.NewBuffer([]byte {
2, byte(OTA.WithCN(3)),
0, 0, 0, 1, byte(LI.WithCN(1)),
0, 0x5,
2, byte(LI.WithCN(1)),
0, 0x17,
0xAA, 0xAA,
})
decoder := NewDecoder(buffer)
test.Log("--- stage 1")
elem, populated, n, err := peekSliceOnce(decoder, OTA.WithCN(0), 0)
if err != nil { test.Fatal(err) }
if elem != OTA.WithCN(3) {
test.Fatal("wrong element tag:", elem)
}
if !populated {
test.Fatal("wrong populated:", populated)
}
if got, correct := n, 2; got != correct {
test.Fatalf("wrong n: %d != %d", got, correct)
}
test.Log("--- stage 2")
elem, populated, n, err = peekSliceOnce(decoder, elem, n)
if err != nil { test.Fatal(err) }
if elem != LI.WithCN(1) {
test.Fatal("wrong element tag:", elem)
}
if !populated {
test.Fatal("wrong populated:", populated)
}
if got, correct := n, 7; got != correct {
test.Fatalf("wrong n: %d != %d", got, correct)
}
}
func encAny(value any) ([]byte, Tag, int, error) { func encAny(value any) ([]byte, Tag, int, error) {
tag, err := TagAny(value) tag, err := TagAny(value)
if err != nil { return nil, 0, 0, err } if err != nil { return nil, 0, 0, err }