-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
4318488
commit 4d247aa
Showing
15 changed files
with
1,202 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package y3 | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/yomorun/y3-codec-golang/pkg/spec" | ||
) | ||
|
||
// EncodeBool encode bool type data | ||
func EncodeBool(tag int, v bool) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetBool(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeUInt encode uint type data | ||
func EncodeUInt(tag int, v uint) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetUInt32(uint32(v)) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeInt encode int type data | ||
func EncodeInt(tag int, v int) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetInt32(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeUInt64 encode uint64 type data | ||
func EncodeUInt64(tag int, v uint64) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetUInt64(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeInt64 encode int type data | ||
func EncodeInt64(tag int, v int64) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetInt64(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeFloat32 encode float32 type data | ||
func EncodeFloat32(tag int, v float32) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetFloat32(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeFloat64 encode float64 type data | ||
func EncodeFloat64(tag int, v float64) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetFloat64(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeString encode UTF-8 string data | ||
func EncodeString(tag int, v string) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.SetUTF8String(v) | ||
return p.Encode() | ||
} | ||
|
||
// EncodeBytes encode raw bytes | ||
func EncodeBytes(tag int, v []byte) ([]byte, error) { | ||
p, err := spec.NewPacket(uint64(tag)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
p.PutBytes(v) | ||
return p.Encode() | ||
} | ||
|
||
// Marshal TODO wip | ||
func Marshal(tag int, obj interface{}) ([]byte, error) { | ||
panic(errors.New("NotImplementedError")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package y3 | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestD2EncodeUInt32(t *testing.T) { | ||
testD2EncodeUInt32(t, 0x03, 6, []byte{0x03, 0x01, 0x06}) | ||
testD2EncodeUInt32(t, 0x06, 127, []byte{0x06, 0x02, 0x80, 0x7F}) | ||
} | ||
|
||
func TestD2EncodeInt32(t *testing.T) { | ||
testD2EncodeInt(t, 0x03, -1, []byte{0x03, 0x01, 0x7F}) | ||
testD2EncodeInt(t, 0x06, -65, []byte{0x06, 0x02, 0xFF, 0x3F}) | ||
testD2EncodeInt(t, 0x09, 255, []byte{0x09, 0x02, 0x81, 0x7F}) | ||
} | ||
|
||
func TestD2EncodeUInt64(t *testing.T) { | ||
testD2EncodeUInt64(t, 0x03, 0, []byte{0x03, 0x01, 0x00}) | ||
testD2EncodeUInt64(t, 0x06, 1, []byte{0x06, 0x01, 0x01}) | ||
testD2EncodeUInt64(t, 0x09, 18446744073709551615, []byte{0x09, 0x01, 0x7F}) | ||
} | ||
|
||
func TestD2EncodeInt64(t *testing.T) { | ||
testD2EncodeInt64(t, 0x03, 0, []byte{0x03, 0x01, 0x00}) | ||
testD2EncodeInt64(t, 0x06, 1, []byte{0x06, 0x01, 0x01}) | ||
testD2EncodeInt64(t, 0x09, -1, []byte{0x09, 0x01, 0x7F}) | ||
} | ||
|
||
func TestD2EncodeFloat32(t *testing.T) { | ||
testD2EncodeFloat32(t, 0x03, -2, []byte{0x03, 0x01, 0xC0}) | ||
testD2EncodeFloat32(t, 0x06, 0.25, []byte{0x06, 0x02, 0x3E, 0x80}) | ||
testD2EncodeFloat32(t, 0x09, 68.123, []byte{0x09, 0x04, 0x42, 0x88, 0x3E, 0xFA}) | ||
} | ||
|
||
func TestD2EncodeFloat64(t *testing.T) { | ||
testD2EncodeFloat64(t, 0x03, 23, []byte{0x03, 0x02, 0x40, 0x37}) | ||
testD2EncodeFloat64(t, 0x06, 2, []byte{0x06, 0x01, 0x40}) | ||
testD2EncodeFloat64(t, 0x09, 0.01171875, []byte{0x09, 0x02, 0x3F, 0x88}) | ||
} | ||
|
||
func TestD2EncodeString(t *testing.T) { | ||
p, _ := EncodeString(0x01, "C") | ||
compareTwoBytes(t, p, []byte{0x01, 0x01, 0x43}) | ||
p, _ = EncodeString(0x01, "CC") | ||
compareTwoBytes(t, p, []byte{0x01, 0x02, 0x43, 0x43}) | ||
p, _ = EncodeString(0x01, "Yona") | ||
compareTwoBytes(t, p, []byte{0x01, 0x04, 0x59, 0x6F, 0x6E, 0x61}) | ||
p, _ = EncodeString(0x01, "https://yomo.run") | ||
compareTwoBytes(t, p, []byte{0x01, 0x10, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3A, 0x2F, 0x2F, 0x79, 0x6F, 0x6D, 0x6F, 0x2E, 0x72, 0x75, 0x6E}) | ||
} | ||
|
||
func TestD2EncodeBytes(t *testing.T) { | ||
p, _ := EncodeBytes(0x01, []byte{0x03, 0x06, 0x09, 0x0C, 0x0F}) | ||
compareTwoBytes(t, p, []byte{0x01, 0x05, 0x03, 0x06, 0x09, 0x0C, 0x0F}) | ||
} | ||
|
||
func TestD2EncodeBool(t *testing.T) { | ||
p, _ := EncodeBool(0x01, true) | ||
compareTwoBytes(t, p, []byte{0x01, 0x01, 0x01}) | ||
p, _ = EncodeBool(0x01, false) | ||
compareTwoBytes(t, p, []byte{0x01, 0x01, 0x00}) | ||
} | ||
|
||
func testD2EncodeUInt32(t *testing.T, tag int, val uint, expected []byte) { | ||
p, _ := EncodeUInt(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func testD2EncodeInt(t *testing.T, tag int, val int, expected []byte) { | ||
p, _ := EncodeInt(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func testD2EncodeUInt64(t *testing.T, tag int, val uint64, expected []byte) { | ||
p, _ := EncodeUInt64(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func testD2EncodeInt64(t *testing.T, tag int, val int64, expected []byte) { | ||
p, _ := EncodeInt64(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func testD2EncodeFloat32(t *testing.T, tag int, val float32, expected []byte) { | ||
p, _ := EncodeFloat32(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func testD2EncodeFloat64(t *testing.T, tag int, val float64, expected []byte) { | ||
p, _ := EncodeFloat64(tag, val) | ||
compareTwoBytes(t, p, expected) | ||
} | ||
|
||
func compareTwoBytes(t *testing.T, result []byte, expected []byte) { | ||
for i, p := range result { | ||
if p != expected[i] { | ||
t.Errorf("\nexpected:[% X]\n actual:[% X]\n", expected, result) | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.