Skip to content

Commit

Permalink
remove NextList
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaohu committed Oct 11, 2022
1 parent 3be183f commit 6437ab0
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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})
```

Expand All @@ -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})
```

Expand Down
18 changes: 8 additions & 10 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"))
Expand Down
24 changes: 11 additions & 13 deletions vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6437ab0

Please sign in to comment.