2023-05-30 17:30:06 +08:00
|
|
|
package grpcall
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
"github.com/golang/protobuf/ptypes/wrappers"
|
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonpbUnmarshalString(t *testing.T) {
|
|
|
|
msg := &wrappers.Int64Value{}
|
|
|
|
|
|
|
|
if err := jsonpb.UnmarshalString(`123`, msg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Log(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonpbMarshalString(t *testing.T) {
|
|
|
|
msg := &wrappers.Int64Value{Value: 123}
|
|
|
|
|
|
|
|
marshaler := &jsonpb.Marshaler{}
|
|
|
|
|
|
|
|
if json, err := marshaler.MarshalToString(msg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Log(json) // "123"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJsonpbMarshalEmpty(t *testing.T) {
|
|
|
|
msg := &emptypb.Empty{}
|
|
|
|
|
|
|
|
marshaler := &jsonpb.Marshaler{}
|
|
|
|
|
|
|
|
if json, err := marshaler.MarshalToString(msg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
} else {
|
|
|
|
t.Log(json) // {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// func TestNewJsonRequestParser(t *testing.T) {
|
|
|
|
// var str_msg wrapperspb.StringValue
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// inData := `"abc"`
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// if err := NewJsonRequestParser(nil, inData).Next(&str_msg); err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// }
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// t.Log("string", str_msg.GetValue())
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// var int_msg wrapperspb.Int64Value
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// inData = `"10"` // or `10`
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// if err := NewJsonRequestParser(nil, inData).Next(&int_msg); err != nil {
|
|
|
|
// t.Error(err)
|
|
|
|
// }
|
2023-05-30 17:30:06 +08:00
|
|
|
|
2023-06-01 10:28:08 +08:00
|
|
|
// t.Log("int", int_msg.GetValue())
|
|
|
|
// }
|