Skip to content

Commit

Permalink
feat:addmask test
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed May 19, 2024
1 parent 7888e01 commit aeab21e
Show file tree
Hide file tree
Showing 6 changed files with 936 additions and 390 deletions.
12 changes: 4 additions & 8 deletions gateway/serialization/mask.go → gateway/mask.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package serialization
package gateway

import (
"bytes"
Expand All @@ -15,7 +15,7 @@ import (
// GetMask() []string
// }

func SetUpdateMaskFields(message protoreflect.ProtoMessage, fields []string) error {
func SetUpdateMaskFields(message protoreflect.ProtoMessage, fields []string) {
// 反射获取消息的描述符
md := message.ProtoReflect().Descriptor()

Expand All @@ -33,10 +33,8 @@ func SetUpdateMaskFields(message protoreflect.ProtoMessage, fields []string) err
// 更新原始消息中的FieldMask字段
message.ProtoReflect().Set(field, protoreflect.ValueOf(fieldMask.ProtoReflect()))

return nil
}
}
return nil
}

type maskDecoder struct {
Expand Down Expand Up @@ -84,9 +82,7 @@ func (d *maskDecoder) Decode(v interface{}) error {
return err
}
// 设置更新掩码字段
if err := SetUpdateMaskFields(message, fields); err != nil {
return err
}

SetUpdateMaskFields(message, fields)

return nil
}
75 changes: 75 additions & 0 deletions gateway/mask_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package gateway

import (
"bytes"
"encoding/json"
"fmt"
"testing"

"github.com/agiledragon/gomonkey/v2"
api "github.com/begonia-org/go-sdk/api/example/v1"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
c "github.com/smartystreets/goconvey/convey"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/dynamicpb"
)

func TestMask(t *testing.T) {
c.Convey("TestMask", t, func() {
pb := &api.ExampleMessage{}
dpb := dynamicpb.NewMessage(pb.ProtoReflect().Descriptor())
r := bytes.NewReader([]byte(`{"message":"John Doe","msg":{"msg":"hello world"},"allow":"DENY","repeated_msg":[{"msg":"John Doe"}]}`))
decoder := NewMaskDecoder(NewJsonDecoder(r))
err := decoder.Decode(dpb)
c.So(err, c.ShouldBeNil)
bData, err := protojson.Marshal(dpb)
c.So(err, c.ShouldBeNil)
err = protojson.Unmarshal(bData, pb)
c.So(err, c.ShouldBeNil)
c.So(pb.Message, c.ShouldEqual, "John Doe")
c.So(pb.Allow, c.ShouldEqual, api.EnumAllow_DENY)
c.So(pb.RepeatedMsg[0].Msg, c.ShouldEqual, "John Doe")
c.So(pb.Msg.Msg, c.ShouldEqual, "hello world")
c.So(pb.Mask.Paths, c.ShouldNotBeEmpty)

pb2 := &api.HelloReply{}
dpb2 := dynamicpb.NewMessage(pb2.ProtoReflect().Descriptor())
r2 := bytes.NewReader([]byte(`{"message":"John Doe","name":"tester"}`))
decoder2 := NewMaskDecoder(NewJsonDecoder(r2))
err = decoder2.Decode(dpb2)
c.So(err, c.ShouldBeNil)
})
}
func TestDecodeErr(t *testing.T) {
c.Convey("TestDecodeErr", t, func() {
cases := []struct {
patch interface{}
output []interface{}
err error
}{
{
patch: (*runtime.DecoderWrapper).Decode,
output: []interface{}{fmt.Errorf("test")},
err: fmt.Errorf("test"),
},
{
patch: json.Marshal,
output: []interface{}{nil, fmt.Errorf("test DECODE")},
err: fmt.Errorf("test DECODE"),
},
}
for _, caseV := range cases {
pb := &api.ExampleMessage{}
dpb := dynamicpb.NewMessage(pb.ProtoReflect().Descriptor())
r := bytes.NewReader([]byte(`{"message":"John Doe","msg":{"msg":"hello world"},"allow":"DENY","repeated_msg":[{"msg":"John Doe"}]}`))
patch := gomonkey.ApplyFuncReturn(caseV.patch, caseV.output...)
defer patch.Reset()
decoder := NewMaskDecoder(NewJsonDecoder(r))

err := decoder.Decode(dpb)
patch.Reset()
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error())
}
})
}
17 changes: 1 addition & 16 deletions gateway/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gateway
import (
"fmt"
"io"
"reflect"

_ "github.com/begonia-org/go-sdk/api/app/v1"
_ "github.com/begonia-org/go-sdk/api/endpoint/v1"
Expand Down Expand Up @@ -37,13 +36,6 @@ type BinaryDecoder struct {
marshaler runtime.Marshaler
}

func (d *BinaryDecoder) fn() string {
if d.fieldName == "" {
return "Data"
}
return d.fieldName
}

// var typeOfBytes = reflect.TypeOf([]byte(nil))
// var typeOfHttpbody = reflect.TypeOf(&httpbody.HttpBody{})

Expand All @@ -52,10 +44,7 @@ func (d *BinaryDecoder) Decode(v interface{}) error {
return nil

}
rv := reflect.ValueOf(v).Elem() // assert it must be a pointer
if rv.Kind() != reflect.Struct {
return d
}

if dpb, ok := v.(*dynamicpb.Message); ok {
typ := dpb.Type().Descriptor().Name()
if string(typ) == "HttpBody" {
Expand All @@ -81,10 +70,6 @@ func (d *BinaryDecoder) Decode(v interface{}) error {
return d.marshaler.NewDecoder(d.r).Decode(v)
}

func (d *BinaryDecoder) Error() string {
d.r = nil
return "cannot set: " + d.fn()
}
func NewRawBinaryUnmarshaler() *RawBinaryUnmarshaler {
return &RawBinaryUnmarshaler{

Expand Down
31 changes: 0 additions & 31 deletions gateway/serialization/mask_test.go

This file was deleted.

101 changes: 101 additions & 0 deletions gateway/serialization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package gateway

import (
"bytes"
"fmt"
"io"
"testing"

"github.com/agiledragon/gomonkey/v2"
c "github.com/smartystreets/goconvey/convey"
"google.golang.org/genproto/googleapis/api/httpbody"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/dynamicpb"
)

func TestRawBinaryUnmarshaler(t *testing.T) {
c.Convey("TestRawBinaryUnmarshaler", t, func() {
r := bytes.NewReader([]byte(`{"test":"test"}`))
marshal := NewRawBinaryUnmarshaler()
decoder := marshal.NewDecoder(r)
val := make(map[string]string)
err := decoder.Decode(&val)
c.So(err, c.ShouldBeNil)

r2 := bytes.NewReader([]byte(`"test":"test"}`))
decoder2 := marshal.NewDecoder(r2)
err = decoder2.Decode(&val)
c.So(err, c.ShouldNotBeNil)
// t.Logf("err: %v", err)

body := &httpbody.HttpBody{
Data: []byte("test"),
}
msg := dynamicpb.NewMessage(body.ProtoReflect().Descriptor()).New()

r3 := bytes.NewReader([]byte(`test`))
decoder3 := marshal.NewDecoder(r3)
err = decoder3.Decode(msg)
c.So(err, c.ShouldBeNil)
c.So(msg.Get(msg.Descriptor().Fields().ByName("content_type")).String(), c.ShouldEqual, "application/octet-stream")
c.So(msg.Get(msg.Descriptor().Fields().ByName("data")).Bytes(), c.ShouldEqual, []byte(`test`))

httpBody := &httpbody.HttpBody{
ContentType: "application/octet-stream-test",
Data: []byte("test"),
}
msg2 := dynamicpb.NewMessage(httpBody.ProtoReflect().Descriptor()).New()
httpBodyBytes, err := proto.Marshal(httpBody)
c.So(err, c.ShouldBeNil)
err2 := proto.Unmarshal(httpBodyBytes, msg2.Interface())
c.So(err2, c.ShouldBeNil)
c.So(marshal.ContentType(msg2), c.ShouldContainSubstring, "application/octet-stream-test")

c.So(decoder3.Decode(nil),c.ShouldBeNil)
})
}

func TestRawBinaryDecodeErr(t *testing.T) {
c.Convey("TestRawBinaryDecodeErr", t, func() {
cases := []struct {
patch interface{}
err error
output []interface{}
}{
{
patch: io.ReadAll,
output: []interface{}{[]byte{}, fmt.Errorf("io.ReadAll: unexpected EOF")},
err: fmt.Errorf("io.ReadAll: unexpected EOF"),
},
{
patch: io.ReadAll,
output: []interface{}{[]byte{}, nil},
err: io.EOF,
},
{
patch: proto.Marshal,
output: []interface{}{nil, fmt.Errorf("proto.Marshal: nil")},
err: fmt.Errorf("proto.Marshal: nil"),
},
}
marshal := NewRawBinaryUnmarshaler()

for _, caseV := range cases {
body := &httpbody.HttpBody{
Data: []byte("test"),
}
msg := dynamicpb.NewMessage(body.ProtoReflect().Descriptor()).New()

r3 := bytes.NewReader([]byte(`test`))
decoder3 := marshal.NewDecoder(r3)
patch := gomonkey.ApplyFuncReturn(caseV.patch, caseV.output...)
defer patch.Reset()
err := decoder3.Decode(msg)
patch.Reset()
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error())

}

})
}
Loading

0 comments on commit aeab21e

Please sign in to comment.