pb_test.go 842 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package pb
  2. import (
  3. "f1-game/internal/pb"
  4. "strings"
  5. "testing"
  6. cherryString "github.com/cherry-game/cherry/extend/string"
  7. clog "github.com/cherry-game/cherry/logger"
  8. "google.golang.org/protobuf/proto"
  9. )
  10. func TestPB1(t *testing.T) {
  11. text := "[8 177 141 6]"
  12. v := pb.I32{}
  13. err := unmarshal(text, &v)
  14. clog.Debugf("v = %s", v.String())
  15. clog.Debugf("err = %v", err)
  16. }
  17. func unmarshal(text string, v proto.Message) error {
  18. bytes := text2Bytes(text)
  19. return proto.Unmarshal(bytes, v)
  20. }
  21. func text2Bytes(text string) []byte {
  22. text = strings.TrimSpace(text)
  23. text = strings.Replace(text, "[", "", -1)
  24. text = strings.Replace(text, "]", "", -1)
  25. textStrings := strings.Split(text, " ")
  26. var bytes []byte
  27. for _, t := range textStrings {
  28. value, _ := cherryString.ToUint(t, 0)
  29. bytes = append(bytes, uint8(value))
  30. }
  31. return bytes
  32. }