Add pk.Option API

This commit is contained in:
Tnze
2022-12-06 02:14:38 +08:00
parent 516b3c2a8b
commit 55bf5eddbb
6 changed files with 107 additions and 50 deletions

View File

@ -138,16 +138,16 @@ func ExampleOpt_ReadFrom() {
// WILL NOT BE READ, WILL NOT BE COVERED
}
// As an example, we define this packet as this:
// +------+-----------------+----------------------------------+
// | Name | Type | Notes |
// +------+-----------------+----------------------------------+
// | Flag | Unsigned Byte | Odd if the following is present. |
// +------+-----------------+----------------------------------+
// | User | Optional String | The player's name. |
// +------+-----------------+----------------------------------+
// So we need a function to decide if the User field is present.
func ExampleOpt_ReadFrom_func() {
// As an example, we define this packet as this:
// +------+-----------------+----------------------------------+
// | Name | Type | Notes |
// +------+-----------------+----------------------------------+
// | Flag | Unsigned Byte | Odd if the following is present. |
// +------+-----------------+----------------------------------+
// | User | Optional String | The player's name. |
// +------+-----------------+----------------------------------+
// So we need a function to decide if the User field is present.
var flag pk.Byte
var data pk.String
p := pk.Packet{Data: []byte{
@ -188,3 +188,38 @@ func ExampleTuple_ReadFrom() {
panic(err)
}
}
// As an example, we define this packet as this:
// +------+-----------------+-----------------------------------+
// | Name | Type | Notes |
// +------+-----------------+-----------------------------------+
// | Has | Boolean | True if the following is present. |
// +------+-----------------+-----------------------------------+
// | User | Optional String | The player's name. |
// +------+-----------------+-----------------------------------+
// So we need a function to decide if the User field is present.
func ExampleOption_ReadFrom_func() {
p1 := pk.Packet{Data: []byte{
0x01, // pk.Boolean(true)
4, 'T', 'n', 'z', 'e', // pk.String("Tnze")
}}
p2 := pk.Packet{Data: []byte{
0x00, // pk.Boolean(false)
// empty
}}
var User1, User2 pk.Option[pk.String]
if err := p1.Scan(&User1); err != nil {
panic(err)
}
if err := p2.Scan(&User2); err != nil {
panic(err)
}
fmt.Println(User1.Has, User1.Val)
fmt.Println(User2.Has, User2.Val)
// Output:
// true Tnze
// false
}