Skip to content

Commit 83bf193

Browse files
committed
improve the code and readme
1 parent b0ae4c8 commit 83bf193

23 files changed

+319
-567
lines changed

README.md

+132
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,135 @@
33

44
Scale Codec written by golang, reference from [polkascan/py-scale-codec](https://github.com/polkascan/py-scale-codec).
55

6+
---
7+
8+
# Interface
9+
define a interface ```Compact``` that means what we want to deal with.
10+
```go
11+
type Compact interface {
12+
Encode() ([]byte, error)
13+
Decode(val []byte) (int, error)
14+
GetVal() interface{}
15+
GetType() PrimitiveType
16+
CloneNew() Compact
17+
}
18+
```
19+
20+
---
21+
22+
23+
# Usage
24+
25+
Here are the examples for the types that we supported.
26+
27+
+ ##### Fix Number
28+
use ```uint8``` as the example, we wrap the real val with a struct, the implement the interface.
29+
```go
30+
// encode
31+
c := FixU8{Val: 25}
32+
res, err := c.Encode()
33+
34+
35+
// decode
36+
c := FixU8{}
37+
res, err := c.Decode([]byte{25})
38+
```
39+
40+
+ ##### Compact Number
41+
compact number use the compact encode way which can save the space of memory.
42+
```go
43+
// encode
44+
a := CompactU8{Val: uint8(25)}
45+
res, err := a.Encode()
46+
47+
// decode
48+
a := CompactU8{}
49+
num, err := a.Decode([]byte{100})
50+
```
51+
52+
+ ##### Bool
53+
same as fix number, but the real value is a ```bool```.
54+
```go
55+
// encode
56+
a := CompactBool{Val: true}
57+
res, err := a.Encode()
58+
59+
// decode
60+
a := CompactBool{}
61+
num, err := a.Decode([]byte{1})
62+
```
63+
64+
+ ##### String
65+
```go
66+
// encode
67+
s := CompactString{
68+
Val: "set_hash",
69+
}
70+
res, err := s.Encode()
71+
72+
// decode
73+
s := &CompactString{}
74+
s.Decode(Hex2Bytes("0x207365745f686173680c6b657914776f726c64"))
75+
```
76+
77+
+ ##### Vec
78+
79+
```go
80+
// encode
81+
v := &CompactVec{Val: []Compact{&FixU16{
82+
Val: uint16(1),
83+
}, &FixU16{
84+
Val: uint16(64),
85+
}}, NextList: []PrimitiveType{Uint16}}
86+
res, err := v.Encode()
87+
88+
// decode
89+
s := &CompactVec{NextList: []PrimitiveType{Uint16}}
90+
_, err := s.Decode([]byte{8, 1, 0, 64, 0})
91+
```
92+
93+
+ ##### Array
94+
```go
95+
// encode
96+
v := &CompactArray{Val: []Compact{&FixU16{
97+
Val: uint16(1),
98+
}, &FixU16{
99+
Val: uint16(64),
100+
}}, NextList: []PrimitiveType{Uint16}, Len: 2}
101+
res, err := v.Encode()
102+
103+
// decode
104+
s := &CompactArray{NextList: []PrimitiveType{Uint16}, Len: 2}
105+
_, err := s.Decode([]byte{1, 0, 64, 0})
106+
```
107+
108+
+ ##### Enum
109+
110+
```go
111+
// encode
112+
a := CompactEnum{Val: &FixU8{Val: 42}, index: 0}
113+
ans, err := a.Encode()
114+
115+
//decode
116+
a := CompactEnum{Val: &FixU8{}, index: 0}
117+
ans, err := a.Decode([]byte{0, 42})
118+
```
119+
120+
+ ##### Tuple
121+
```go
122+
// encode
123+
a := CompactTuple{Val: []Compact{
124+
&CompactU32{Val: 3},
125+
&CompactBool{Val: false},
126+
}}
127+
ans, err := a.Encode()
128+
129+
// decode
130+
a := CompactTuple{Val: []Compact{
131+
&CompactU32{},
132+
&CompactBool{},
133+
}}
134+
_, err := a.Decode([]byte{12, 0})
135+
```
136+
137+

array.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
type CompactArray struct {
99
Val []Compact
1010
Len int
11-
NextList []TypeString
11+
NextList []PrimitiveType
1212
}
1313

1414
func (c *CompactArray) Encode() ([]byte, error) {
@@ -58,12 +58,12 @@ func (c *CompactArray) GetVal() interface{} {
5858
return c.Val
5959
}
6060

61-
func (c *CompactArray) GetType() TypeString {
62-
return ArrayName
61+
func (c *CompactArray) GetType() PrimitiveType {
62+
return Array
6363
}
6464

6565
func (c *CompactArray) getNextCompact(i int) (Compact, error) {
66-
switch changeStringToType(c.NextList[0]) {
66+
switch c.NextList[0] {
6767
case String:
6868
return &CompactString{}, nil
6969
case Uint8:

array_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ func TestCompactArray_Encode(t *testing.T) {
1010
Val: uint16(1),
1111
}, &FixU16{
1212
Val: uint16(64),
13-
}}, NextList: []TypeString{Uint16Name}, Len: 2}
13+
}}, NextList: []PrimitiveType{Uint16}, Len: 2}
1414
res, err := v.Encode()
1515
if err != nil {
1616
t.Error(err)
1717
}
1818
assert.Equal(t, []byte{1, 0, 64, 0}, res)
19-
assert.Equal(t, ArrayName, v.GetType())
19+
assert.Equal(t, Array, v.GetType())
2020
}
2121

2222
func TestCompactArray_Decode(t *testing.T) {
23-
s := &CompactArray{NextList: []TypeString{Uint16Name}, Len: 2}
23+
s := &CompactArray{NextList: []PrimitiveType{Uint16}, Len: 2}
2424
_, err := s.Decode([]byte{1, 0, 64, 0})
2525
if err != nil {
2626
t.Error(err)

base.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
type littleEncode struct {
99
Val interface{}
10-
Type TypeString
10+
Type PrimitiveType
1111
}
1212

1313
var BaseIntEncode littleEncode

bool.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ func (c *CompactBool) Decode(val []byte) (int, error) {
2424
return 1, nil
2525
}
2626

27-
func (c *CompactBool) GetType() TypeString {
28-
return BoolName
27+
func (c *CompactBool) GetType() PrimitiveType {
28+
return Bool
2929
}
3030

3131
func (c *CompactBool) CloneNew() Compact {

bool_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestBool(t *testing.T) {
1818
res2, err := b.Encode()
1919
assert.Nil(t, err)
2020
assert.Equal(t, []byte{0}, res2)
21-
assert.Equal(t, BoolName, a.GetType())
21+
assert.Equal(t, Bool, a.GetType())
2222
})
2323
t.Run("decode", func(t *testing.T) {
2424
a := CompactBool{}

compact.go

+12-12
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import (
88
)
99

1010
// Compact base interface for encoding and decoding. We redefine
11-
// the way to encode and decode all the types in abi file.
11+
// the way to encode and decode all the types that supported by us.
1212
// For more details see [SCALE Codec](https://docs.substrate.io/v3/advanced/scale-codec/)
1313
type Compact interface {
1414
Encode() ([]byte, error)
1515
Decode(val []byte) (int, error)
1616
GetVal() interface{}
17-
GetType() TypeString
17+
GetType() PrimitiveType
1818
CloneNew() Compact
1919
}
2020

@@ -57,8 +57,8 @@ func (c *CompactU8) GetVal() interface{} {
5757
return c.Val
5858
}
5959

60-
func (c *CompactU8) GetType() TypeString {
61-
return CompactUInt8Name
60+
func (c *CompactU8) GetType() PrimitiveType {
61+
return CompactUInt8
6262
}
6363

6464
func (c *CompactU8) CloneNew() Compact {
@@ -112,8 +112,8 @@ func (u *CompactU16) Decode(val []byte) (int, error) {
112112
}
113113
}
114114

115-
func (u *CompactU16) GetType() TypeString {
116-
return CompactUint16Name
115+
func (u *CompactU16) GetType() PrimitiveType {
116+
return CompactUint16
117117
}
118118

119119
func (u *CompactU16) CloneNew() Compact {
@@ -178,8 +178,8 @@ func (u *CompactU32) Decode(val []byte) (int, error) {
178178
return 0, nil
179179
}
180180

181-
func (u *CompactU32) GetType() TypeString {
182-
return CompactUint32Name
181+
func (u *CompactU32) GetType() PrimitiveType {
182+
return CompactUint32
183183
}
184184

185185
func (u *CompactU32) CloneNew() Compact {
@@ -278,8 +278,8 @@ func (c *CompactU64) Decode(val []byte) (int, error) {
278278
return 0, nil
279279
}
280280

281-
func (c *CompactU64) GetType() TypeString {
282-
return CompactUint64Name
281+
func (c *CompactU64) GetType() PrimitiveType {
282+
return CompactUint64
283283
}
284284

285285
func (c *CompactU64) CloneNew() Compact {
@@ -294,8 +294,8 @@ func (c *CompactU128) GetVal() interface{} {
294294
return c.Val
295295
}
296296

297-
func (c *CompactU128) GetType() TypeString {
298-
return CompactBigIntName
297+
func (c *CompactU128) GetType() PrimitiveType {
298+
return CompactBigInt
299299
}
300300

301301
func (c *CompactU128) Encode() ([]byte, error) {

compact_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestCompactU8(t *testing.T) {
1313
res, err := a.Encode()
1414
assert.Nil(t, err)
1515
assert.Equal(t, []byte{100}, res)
16-
assert.Equal(t, CompactUInt8Name, a.GetType())
16+
assert.Equal(t, CompactUInt8, a.GetType())
1717
})
1818
t.Run("encode2", func(t *testing.T) {
1919
a := CompactU8{Val: 64}
@@ -43,7 +43,7 @@ func TestCompactU16(t *testing.T) {
4343
res, err := a.Encode()
4444
assert.Nil(t, err)
4545
assert.Equal(t, []byte{100}, res)
46-
assert.Equal(t, CompactUint16Name, a.GetType())
46+
assert.Equal(t, CompactUint16, a.GetType())
4747
})
4848
t.Run("encode2", func(t *testing.T) {
4949
a := CompactU16{Val: uint16(69)}
@@ -86,7 +86,7 @@ func TestCompactU32(t *testing.T) {
8686
res, err := a.Encode()
8787
assert.Nil(t, err)
8888
assert.Equal(t, []byte{186, 201, 2, 0}, res)
89-
assert.Equal(t, CompactUint32Name, a.GetType())
89+
assert.Equal(t, CompactUint32, a.GetType())
9090
})
9191
t.Run("encode2", func(t *testing.T) {
9292
a := CompactU32{Val: 25}
@@ -210,7 +210,7 @@ func TestCompactU128(t *testing.T) {
210210
res, err := s.Encode()
211211
assert.Nil(t, err)
212212
assert.Equal(t, []byte{32}, res)
213-
assert.Equal(t, CompactBigIntName, s.GetType())
213+
assert.Equal(t, CompactBigInt, s.GetType())
214214
})
215215
t.Run("encode2", func(t *testing.T) {
216216
s := CompactU128{Val: new(big.Int).SetUint64(100000000000000)}

const.go

-36
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,3 @@ const (
4848
Tuple
4949
Enum
5050
)
51-
52-
type TypeString string
53-
54-
const (
55-
NoneName TypeString = TypeString("None")
56-
StringName TypeString = "String"
57-
StructName TypeString = "struct"
58-
Uint8Name TypeString = "u8"
59-
Int8Name TypeString = "i8"
60-
CompactUInt8Name TypeString = "Compact < u8 >"
61-
Uint16Name TypeString = "u16"
62-
Int16Name TypeString = "i16"
63-
CompactUint16Name TypeString = "Compact < u16 >"
64-
Uint32Name TypeString = "u32"
65-
Int32Name TypeString = "i32"
66-
CompactUint32Name TypeString = "Compact < u32 >"
67-
Uint64Name TypeString = "u64"
68-
Int64Name TypeString = "i64"
69-
CompactUint64Name TypeString = "Compact < u64 >"
70-
BigIntName TypeString = "i128"
71-
BigUIntName TypeString = "u128"
72-
CompactBigIntName TypeString = "Compact < u128 >"
73-
BoolName TypeString = "bool"
74-
VecName TypeString = "Vec"
75-
PrimitiveName TypeString = "primitive"
76-
ArrayName TypeString = "Array"
77-
TupleName TypeString = "tuple"
78-
EnumName TypeString = "enum"
79-
)
80-
81-
func (t TypeString) String() string {
82-
return string(t)
83-
}
84-
85-
// CustomParamsSection custom section named `params` in wasm defined by us.
86-
const CustomParamsSection = "params"

encoder.go

-13
This file was deleted.

encoder_test.go

-24
This file was deleted.

0 commit comments

Comments
 (0)