379 Star 2.5K Fork 616

GVPJohn / gf

 / 详情

类型转换-UnmarshalValue的错误问题

待办的
创建于  
2021-01-03 15:56

1. 您当前使用的Go版本,及系统版本、系统架构?

go version go1.15.6 windows/amd64

2. 您当前使用的GoFrame框架版本?

github.com/gogf/gf v1.14.5

3. 更新到最新的框架版本是否能够解决问题?

文档代码:

package main

import (
"errors"
"fmt"
"github.com/gogf/gf/crypto/gcrc32"
"github.com/gogf/gf/encoding/gbinary"
"github.com/gogf/gf/util/gconv"
)

type Pkg struct {
Length uint16 // Total length.
Crc32 uint32 // CRC32.
Data []byte
}

// NewPkg creates and returns a package with given data.
func NewPkg(data []byte) *Pkg {
return &Pkg{
Length: uint16(len(data) + 6),
Crc32: gcrc32.Encrypt(data),
Data: data,
}
}

// Marshal encodes the protocol struct to bytes.
func (p *Pkg) Marshal() []byte {
b := make([]byte, 6+len(p.Data))
copy(b, gbinary.EncodeUint16(p.Length))
copy(b[2:], gbinary.EncodeUint32(p.Crc32))
copy(b[6:], p.Data)
return b
}

// UnmarshalValue decodes bytes to protocol struct.
func (p *Pkg) UnmarshalValue(v interface{}) error {
b := gconv.Bytes(v)
if len(b) < 6 {
return errors.New("invalid package length")
}
p.Length = gbinary.DecodeToUint16(b[:2])
if len(b) < int(p.Length) {
return errors.New("invalid data length")
}
p.Crc32 = gbinary.DecodeToUint32(b[2:6])
p.Data = b[6:]
if gcrc32.Encrypt(p.Data) != p.Crc32 {
return errors.New("crc32 validation failed")
}
return nil
}

func main() {
var p1, p2 *Pkg

// Create a demo pkg as p1.
p1 = NewPkg([]byte("123"))
fmt.Println(p1)

// Convert bytes from p1 to p2 using gconv.Struct.
err := gconv.Struct(p1.Marshal(), &p2)
if err != nil {
	panic(err)
}
fmt.Println(p2)

}
地址:https://itician.org/pages/viewpage.action?pageId=1114226

我的测试代码

package test

import (
	"errors"
	"fmt"
	"github.com/gogf/gf/crypto/gcrc32"
	"github.com/gogf/gf/encoding/gbinary"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gconv"
	"testing"
)

type Pkg struct {
	Length uint16 // Total length.
	Crc32  uint32 // CRC32.
	Data   []byte
}

// NewPkg creates and returns a package with given data.
func NewPkg(data []byte) *Pkg {
	return &Pkg{
		Length: uint16(len(data) + 6),
		Crc32:  gcrc32.Encrypt(data),
		Data:   data,
	}
}

// Marshal encodes the protocol struct to bytes.
func (p *Pkg) Marshal() []byte {

	fmt.Println(p)
	b := make([]byte, 6+len(p.Data))
	copy(b, gbinary.EncodeUint16(p.Length))
	copy(b[2:], gbinary.EncodeUint32(p.Crc32))
	copy(b[6:], p.Data)

	fmt.Println(p)
	return b
}

// UnmarshalValue decodes bytes to protocol struct.
func (p *Pkg) UnmarshalValue(v interface{}) error {
	b := gconv.Bytes(v)
	if len(b) < 6 {
		return errors.New("invalid package length")
	}
	p.Length = gbinary.DecodeToUint16(b[:2])
	if len(b) < int(p.Length) {
		return errors.New("invalid data length")
	}
	p.Crc32 = gbinary.DecodeToUint32(b[2:6])
	p.Data = b[6:]
	if gcrc32.Encrypt(p.Data) != p.Crc32 {
		return errors.New("crc32 validation failed")
	}
	return nil
}
func TestConvStruct(t *testing.T) {
	var p1, p2 *Pkg

	// Create a demo pkg as p1.
	p1 = NewPkg([]byte("123"))
	fmt.Println(p1)

	// Convert bytes from p1 to p2 using gconv.Struct.
	err := gconv.Struct(p1.Marshal(), &p2)
	if err != nil {
		panic(err)
	}
	fmt.Println(p2)
}
运行结果及错误提示:
=== RUN   TestConvStruct
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
--- FAIL: TestConvStruct (0.00s)
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51] [recovered]
	panic: convert params to map failed: [9 0 210 99 72 136 49 50 51]

goroutine 34 [running]:
testing.tRunner.func1.1(0xfbc7c0, 0xc000252680)
	D:/Go/src/testing/testing.go:1072 +0x310
testing.tRunner.func1(0xc000248480)
	D:/Go/src/testing/testing.go:1075 +0x43a
panic(0xfbc7c0, 0xc000252680)
	D:/Go/src/runtime/panic.go:969 +0x1c7
base/test.TestConvStruct(0xc000248480)
	E:/docker/dnmpr/www/go/test/test/map_test.go:125 +0x268
testing.tRunner(0xc000248480, 0x1074560)
	D:/Go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
	D:/Go/src/testing/testing.go:1168 +0x2b3

Process finished with exit code 1

4. 问题描述?

这是文档里面的一个问题,我进行测试的时候,没测试通过。

5. 您期望得到的结果?

测试通过

6. 您实际得到的结果?

运行结果及错误提示:

=== RUN   TestConvStruct
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
&{9 2286445522 [49 50 51]}
--- FAIL: TestConvStruct (0.00s)
panic: convert params to map failed: [9 0 210 99 72 136 49 50 51] [recovered]
	panic: convert params to map failed: [9 0 210 99 72 136 49 50 51]

goroutine 34 [running]:
testing.tRunner.func1.1(0xfbc7c0, 0xc000252680)
	D:/Go/src/testing/testing.go:1072 +0x310
testing.tRunner.func1(0xc000248480)
	D:/Go/src/testing/testing.go:1075 +0x43a
panic(0xfbc7c0, 0xc000252680)
	D:/Go/src/runtime/panic.go:969 +0x1c7
base/test.TestConvStruct(0xc000248480)
	E:/docker/dnmpr/www/go/test/test/map_test.go:125 +0x268
testing.tRunner(0xc000248480, 0x1074560)
	D:/Go/src/testing/testing.go:1123 +0xef
created by testing.(*T).Run
	D:/Go/src/testing/testing.go:1168 +0x2b3

Process finished with exit code 1

评论 (0)

wodekaiyua 创建了任务
wodekaiyua 关联仓库设置为John/gf
展开全部操作日志

登录 后才可以发表评论

状态
负责人
里程碑
Pull Requests
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
开始日期   -   截止日期
-
置顶选项
优先级
参与者(1)
Go
1
https://gitee.com/johng/gf.git
git@gitee.com:johng/gf.git
johng
gf
gf

搜索帮助