From 6437ab0fe400965882d5f2a62230c517b67d6dbb Mon Sep 17 00:00:00 2001 From: likai Date: Tue, 11 Oct 2022 15:53:51 +0800 Subject: [PATCH] remove NextList --- README.md | 10 +++++----- array.go | 18 ++++++++---------- array_test.go | 4 ++-- string_test.go | 2 +- struct_test.go | 12 ++++++------ vec.go | 24 +++++++++++------------- vec_test.go | 4 ++-- 7 files changed, 35 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index d5d0af3..f76ecc8 100644 --- a/README.md +++ b/README.md @@ -75,18 +75,18 @@ s.Decode(Hex2Bytes("0x207365745f686173680c6b657914776f726c64")) ``` + ##### Vec -in this type, we include ```NextList``` to define which type of value in the vector, we will also check the type while encode or decode. +in this type, we include ```EleType``` to define which type of value in the vector, we will also check the type while encode or decode. ```go // encode v := &CompactVec{Val: []Compact{&FixU16{ Val: uint16(1), }, &FixU16{ Val: uint16(64), -}}, NextList: []PrimitiveType{Uint16}} +}}, EleType: Uint16} res, err := v.Encode() // decode -s := &CompactVec{NextList: []PrimitiveType{Uint16}} +s := &CompactVec{EleType: Uint16} _, err := s.Decode([]byte{8, 1, 0, 64, 0}) ``` @@ -99,11 +99,11 @@ v := &CompactArray{Val: []Compact{&FixU16{ Val: uint16(1), }, &FixU16{ Val: uint16(64), -}}, NextList: []PrimitiveType{Uint16}, Len: 2} +}}, EleType: Uint16, Len: 2} res, err := v.Encode() // decode -s := &CompactArray{NextList: []PrimitiveType{Uint16}, Len: 2} +s := &CompactArray{EleType: Uint16, Len: 2} _, err := s.Decode([]byte{1, 0, 64, 0}) ``` diff --git a/array.go b/array.go index 833c557..6213de9 100644 --- a/array.go +++ b/array.go @@ -6,21 +6,21 @@ import ( ) type CompactArray struct { - Val []Compact - Len int - NextList []PrimitiveType + Val []Compact + Len int + EleType PrimitiveType } func (c *CompactArray) Encode() ([]byte, error) { if len(c.Val) != c.Len { return nil, errors.New("wrong array length") } - if len(c.NextList) == 0 { - return nil, errors.New("must point next type list for array") + if c.EleType == 0 { + return nil, errors.New("must point element type for array") } var buf bytes.Buffer for _, v := range c.Val { - if v.GetType() != c.NextList[0] { + if v.GetType() != c.EleType { return nil, errors.New("invalid array") } res, err := v.Encode() @@ -63,7 +63,7 @@ func (c *CompactArray) GetType() PrimitiveType { } func (c *CompactArray) getNextCompact(i int) (Compact, error) { - switch c.NextList[0] { + switch c.EleType { case String: return &CompactString{}, nil case Uint8: @@ -113,9 +113,7 @@ func (c *CompactArray) CloneNew() Compact { temp := &CompactArray{ Len: c.Len, } - for _, v := range c.NextList { - temp.NextList = append(temp.NextList, v) - } + temp.EleType = c.EleType for _, v := range c.Val { temp.Val = append(temp.Val, v.CloneNew()) } diff --git a/array_test.go b/array_test.go index f1d6ac0..8c43739 100644 --- a/array_test.go +++ b/array_test.go @@ -10,7 +10,7 @@ func TestCompactArray_Encode(t *testing.T) { Val: uint16(1), }, &FixU16{ Val: uint16(64), - }}, NextList: []PrimitiveType{Uint16}, Len: 2} + }}, EleType: Uint16, Len: 2} res, err := v.Encode() if err != nil { t.Error(err) @@ -20,7 +20,7 @@ func TestCompactArray_Encode(t *testing.T) { } func TestCompactArray_Decode(t *testing.T) { - s := &CompactArray{NextList: []PrimitiveType{Uint16}, Len: 2} + s := &CompactArray{EleType: Uint16, Len: 2} _, err := s.Decode([]byte{1, 0, 64, 0}) if err != nil { t.Error(err) diff --git a/string_test.go b/string_test.go index 60e85ef..d4612ec 100644 --- a/string_test.go +++ b/string_test.go @@ -33,7 +33,7 @@ func TestCompactString_Decode(t *testing.T) { fmt.Println(s.Val) s1 := &CompactVec{ - NextList: []PrimitiveType{String}, + EleType: String, } s1.Decode(Hex2Bytes("0x207365745f686173680c6b657914776f726c64")) fmt.Println(s1.Val) diff --git a/struct_test.go b/struct_test.go index a225fbe..7372b74 100644 --- a/struct_test.go +++ b/struct_test.go @@ -45,16 +45,16 @@ func TestEncodeStruct2(t *testing.T) { Val: "hello", }, }, - NextList: []PrimitiveType{String}, + EleType: String, }, &CompactVec{ Val: []Compact{ &CompactString{Val: "world"}, }, - NextList: []PrimitiveType{String}, + EleType: String, }, }, - NextList: []PrimitiveType{Vec, String}, + EleType: Vec, }, }} res, err := a.Encode() @@ -77,16 +77,16 @@ func TestDecodeStruct2(t *testing.T) { Val: "hello", }, }, - NextList: []PrimitiveType{String}, + EleType: String, }, &CompactVec{ Val: []Compact{ &CompactString{Val: "world"}, }, - NextList: []PrimitiveType{String}, + EleType: String, }, }, - NextList: []PrimitiveType{Vec, String}, + EleType: Vec, }, }} _, err := a.Decode(Hex2Bytes("01000000107465737408041468656c6c6f0414776f726c64")) diff --git a/vec.go b/vec.go index eeac99b..252fe0a 100644 --- a/vec.go +++ b/vec.go @@ -6,13 +6,13 @@ import ( ) type CompactVec struct { - Val []Compact - NextList []PrimitiveType + Val []Compact + EleType PrimitiveType } func (c *CompactVec) Encode() ([]byte, error) { - if len(c.NextList) == 0 { - return nil, errors.New("must point next type list for vec") + if c.EleType == 0 { + return nil, errors.New("must point element type for vec") } l := getStringLenCompactType(len(c.Val)) if l == nil { @@ -25,7 +25,7 @@ func (c *CompactVec) Encode() ([]byte, error) { } buf.Write(lengthEncode) for _, v := range c.Val { - if v.GetType() != c.NextList[0] { + if v.GetType() != c.EleType { return nil, errors.New("invalid vec") } res, err := v.Encode() @@ -38,8 +38,8 @@ func (c *CompactVec) Encode() ([]byte, error) { } func (c *CompactVec) Decode(value []byte) (int, error) { - if len(c.NextList) == 0 { - return 0, errors.New("must point next type list for vec") + if c.EleType == 0 { + return 0, errors.New("must point element type for vec") } ss := &CompactU128{} offset, err := ss.Decode(value) @@ -82,7 +82,7 @@ func (c *CompactVec) GetType() PrimitiveType { } func (c *CompactVec) getNextCompact() (Compact, error) { - switch c.NextList[0] { + switch c.EleType { case String: return &CompactString{}, nil case Uint8: @@ -130,12 +130,10 @@ func (c *CompactVec) getNextCompact() (Compact, error) { func (c *CompactVec) CloneNew() Compact { temp := &CompactVec{ - Val: nil, - NextList: nil, - } - for _, v := range c.NextList { - temp.NextList = append(temp.NextList, v) + Val: nil, + EleType: 0, } + temp.EleType = c.EleType for _, v := range c.Val { temp.Val = append(temp.Val, v.CloneNew()) } diff --git a/vec_test.go b/vec_test.go index 15bba96..185f5b2 100644 --- a/vec_test.go +++ b/vec_test.go @@ -10,7 +10,7 @@ func TestCompactVec_Encode(t *testing.T) { Val: uint16(1), }, &FixU16{ Val: uint16(64), - }}, NextList: []PrimitiveType{Uint16}} + }}, EleType: Uint16} res, err := v.Encode() if err != nil { t.Error(err) @@ -19,7 +19,7 @@ func TestCompactVec_Encode(t *testing.T) { } func TestCompactVec_Decode(t *testing.T) { - s := &CompactVec{NextList: []PrimitiveType{Uint16}} + s := &CompactVec{EleType: Uint16} _, err := s.Decode([]byte{8, 1, 0, 64, 0}) if err != nil { t.Error(err)