Compare commits
No commits in common. "e1f58a194a109b30756373288af92b02e4e7f9cb" and "3eb826735bd51ed17bfacec0e7907689d55a2a24" have entirely different histories.
e1f58a194a
...
3eb826735b
@ -157,7 +157,7 @@ func decodeAny(decoder *Decoder, destination reflect.Value, tag Tag) (n int, err
|
||||
n += nn; if err != nil { return n, err }
|
||||
itemTag, nn, err := decoder.ReadTag()
|
||||
n += nn; if err != nil { return n, err }
|
||||
value, err := skeletonValue(decoder, itemTag)
|
||||
value, err := skeletonValue(itemTag)
|
||||
if err != nil { return n, err }
|
||||
nn, err = decodeAny(decoder, value.Elem(), itemTag)
|
||||
n += nn; if err != nil { return n, err }
|
||||
@ -304,7 +304,7 @@ func decodeAndSetFloat(decoder *Decoder, destination reflect.Value, bytes int) (
|
||||
|
||||
// skeletonValue returns a pointer value. In order for it to be set, it must be
|
||||
// dereferenced using Elem().
|
||||
func skeletonValue(decoder *Decoder, tag Tag) (reflect.Value, error) {
|
||||
func skeletonValue(tag Tag) (reflect.Value, error) {
|
||||
switch tag.WithoutCN() {
|
||||
case SI:
|
||||
value := uint8(0)
|
||||
@ -325,20 +325,12 @@ func skeletonValue(decoder *Decoder, tag Tag) (reflect.Value, error) {
|
||||
return reflect.Value { }, fmt.Errorf("unknown CN %d for FP", tag.CN())
|
||||
case SBA: value := []byte { }; return reflect.ValueOf(&value), nil
|
||||
case LBA: value := []byte { }; return reflect.ValueOf(&value), nil
|
||||
case OTA: return skeletonValueSlice(decoder, tag)
|
||||
case OTA: value := unknownSlicePlaceholder { }; return reflect.ValueOf(&value), nil
|
||||
case KTV: value := map[uint16] any { }; return reflect.ValueOf(&value), nil
|
||||
}
|
||||
return reflect.Value { }, fmt.Errorf("unknown TN %d", tag.TN())
|
||||
}
|
||||
|
||||
// skeletonValueSlice returns a pointer value. In order for it to be set, it
|
||||
// must be dereferenced using Elem().
|
||||
func skeletonValueSlice(decoder *Decoder, tag Tag) (reflect.Value, error) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// TODO: delete fucntion below
|
||||
|
||||
// skeletonValueSlice returns a pointer value. In order for it to be set, it
|
||||
// must be dereferenced using Elem().
|
||||
func skeletonValueSlice(tag Tag, length int) (reflect.Value, error) {
|
||||
|
||||
@ -1,136 +0,0 @@
|
||||
package tape
|
||||
|
||||
import "fmt"
|
||||
import "bytes"
|
||||
import "testing"
|
||||
import "reflect"
|
||||
import tu "git.tebibyte.media/sashakoshka/hopp/internal/testutil"
|
||||
|
||||
func TestEncodeAnyInt(test *testing.T) {
|
||||
err := testEncodeAny(test, uint8(0xCA), LI.WithCN(0), tu.S(0xCA))
|
||||
if err != nil { test.Fatal(err) }
|
||||
err = testEncodeAny(test, 400, LI.WithCN(3), tu.S(
|
||||
0, 0, 0x1, 0x90,
|
||||
))
|
||||
if err != nil { test.Fatal(err) }
|
||||
}
|
||||
|
||||
func TestEncodeAnyTable(test *testing.T) {
|
||||
err := testEncodeAny(test, map[uint16] any {
|
||||
0xF3B9: 1,
|
||||
0x0102: 2,
|
||||
0x0000: "hi!",
|
||||
0xFFFF: []uint16 { 0xBEE5, 0x7777 },
|
||||
0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} },
|
||||
}, KTV.WithCN(0), tu.S(5).AddVar(
|
||||
[]byte {
|
||||
0xF3, 0xB9,
|
||||
byte(LI.WithCN(3)),
|
||||
0, 0, 0, 1,
|
||||
},
|
||||
[]byte {
|
||||
0x01, 0x02,
|
||||
byte(LI.WithCN(3)),
|
||||
0, 0, 0, 2,
|
||||
},
|
||||
[]byte {
|
||||
0, 0,
|
||||
byte(SBA.WithCN(3)),
|
||||
'h', 'i', '!',
|
||||
},
|
||||
[]byte {
|
||||
0xFF, 0xFF,
|
||||
byte(OTA.WithCN(0)), 2, byte(LI.WithCN(1)),
|
||||
0xBE, 0xE5, 0x77, 0x77,
|
||||
},
|
||||
[]byte {
|
||||
0x12, 0x34,
|
||||
byte(OTA.WithCN(0)), 2, byte(OTA.WithCN(0)),
|
||||
1, byte(LI.WithCN(1)),
|
||||
0, 0x5,
|
||||
2, byte(LI.WithCN(1)),
|
||||
0, 0x17,
|
||||
0xAA, 0xAA,
|
||||
},
|
||||
))
|
||||
if err != nil { test.Fatal(err) }
|
||||
}
|
||||
|
||||
func TestEncodeDecodeAnyMap(test *testing.T) {
|
||||
err := testEncodeDecodeAny(test, map[uint16] any {
|
||||
0xF3B9: 1,
|
||||
0x0102: 2,
|
||||
0x0000: "hi!",
|
||||
0xFFFF: []uint16 { 0xBEE5, 0x7777 },
|
||||
0x1234: [][]uint16 { []uint16 { 0x5 }, []uint16 { 0x17, 0xAAAA} },
|
||||
}, nil)
|
||||
if err != nil { test.Fatal(err) }
|
||||
}
|
||||
|
||||
func encAny(value any) ([]byte, Tag, int, error) {
|
||||
tag, err := TagAny(value)
|
||||
if err != nil { return nil, 0, 0, err }
|
||||
buffer := bytes.Buffer { }
|
||||
n, err := EncodeAny(&Encoder {
|
||||
Writer: &buffer,
|
||||
}, value, tag)
|
||||
if err != nil { return nil, 0, n, err }
|
||||
return buffer.Bytes(), tag, n, nil
|
||||
}
|
||||
|
||||
func decAny(data []byte) (Tag, any, int, error) {
|
||||
destination := map[uint16] any { }
|
||||
tag, err := TagAny(destination)
|
||||
if err != nil { return 0, nil, 0, err }
|
||||
n, err := DecodeAny(&Decoder {
|
||||
Reader: bytes.NewBuffer(data),
|
||||
}, &destination, tag)
|
||||
if err != nil { return 0, nil, n, err }
|
||||
return tag, destination, n, nil
|
||||
}
|
||||
|
||||
func testEncodeAny(test *testing.T, value any, correctTag Tag, correctBytes tu.Snake) error {
|
||||
bytes, tag, n, err := encAny(value)
|
||||
if err != nil { return err }
|
||||
test.Log("tag: ", tag)
|
||||
test.Log("got: ", tu.HexBytes(bytes))
|
||||
test.Log("correct:", correctBytes)
|
||||
if tag != correctTag {
|
||||
return fmt.Errorf("tag not equal")
|
||||
}
|
||||
if ok, n := correctBytes.Check(bytes); !ok {
|
||||
return fmt.Errorf("bytes not equal: %d", n)
|
||||
}
|
||||
if n != len(bytes) {
|
||||
return fmt.Errorf("n not equal: %d != %d", n, len(bytes))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func testEncodeDecodeAny(test *testing.T, value, correctValue any) error {
|
||||
if correctValue == nil {
|
||||
correctValue = value
|
||||
}
|
||||
|
||||
test.Log("encoding...")
|
||||
bytes, tag, n, err := encAny(value)
|
||||
if err != nil { return err }
|
||||
test.Log("tag:", tag)
|
||||
test.Log("got:", tu.HexBytes(bytes))
|
||||
test.Log("decoding...", tag)
|
||||
if n != len(bytes) {
|
||||
return fmt.Errorf("n not equal: %d != %d", n, len(bytes))
|
||||
}
|
||||
|
||||
_, decoded, n, err := decAny(bytes)
|
||||
if err != nil { return err }
|
||||
test.Log("got: ", decoded)
|
||||
test.Log("correct:", correctValue)
|
||||
if !reflect.DeepEqual(decoded, correctValue) {
|
||||
return fmt.Errorf("values not equal")
|
||||
}
|
||||
if n != len(bytes) {
|
||||
return fmt.Errorf("n not equal: %d != %d", n, len(bytes))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -2,7 +2,6 @@ package tape
|
||||
|
||||
import "io"
|
||||
import "math"
|
||||
import "bufio"
|
||||
|
||||
// Encodable is any type that can write itself to an encoder.
|
||||
type Encodable interface {
|
||||
@ -11,16 +10,14 @@ type Encodable interface {
|
||||
Encode(encoder *Encoder) (n int, err error)
|
||||
}
|
||||
|
||||
// Encoder encodes data to an io.Writer.
|
||||
// Encoder wraps an [io.Writer] and encodes data to it.
|
||||
type Encoder struct {
|
||||
bufio.Writer
|
||||
io.Writer
|
||||
}
|
||||
|
||||
// NewEncoder creates a new encoder that writes to writer.
|
||||
func NewEncoder(writer io.Writer) *Encoder {
|
||||
encoder := &Encoder { }
|
||||
encoder.Reset(writer)
|
||||
return encoder
|
||||
// WriteByte encodes a single byte to the output writer.
|
||||
func (this *Encoder) WriteByte(value byte) (n int, err error) {
|
||||
return this.WriteByte(uint8(value))
|
||||
}
|
||||
|
||||
// WriteInt8 encodes an 8-bit signed integer to the output writer.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user