Skip to content

Commit

Permalink
The fixes needed were not as bad as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Feb 17, 2025
1 parent d07df8a commit 38bb380
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 135 deletions.
26 changes: 0 additions & 26 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,32 +243,6 @@ func TestTranscodeMessage(t *testing.T) {
}
}

func TestInvalidUtf8Decoding(t *testing.T) {
assert := assert.New(t)

/*
"\x85" - 5 name value pairs
"\xa8""msg_type" : "\x03" // 3
"\xa4""dest" : "\xac""\xed\xbf\xbft-address"
"\xa7""payload" : "\xc4""\x03" - len 3
"123"
"\xa6""source" : "\xae""dns:example.io"
"\xb0""transaction_uuid" : "\xd9\x24""c07ee5e1-70be-444c-a156-097c767ad8aa"
*/
invalid := []byte{
0x85,
0xa8, 'm', 's', 'g', '_', 't', 'y', 'p', 'e', 0x03,
0xa4, 'd', 'e', 's', 't', 0xac /* \xed\xbf\xbf is invalid */, 0xed, 0xbf, 0xbf, 't', '-', 'a', 'd', 'd', 'r', 'e', 's', 's',
0xa7, 'p', 'a', 'y', 'l', 'o', 'a', 'd', 0xc4, 0x03, '1', '2', '3',
0xa6, 's', 'o', 'u', 'r', 'c', 'e', 0xae, 'd', 'n', 's', ':', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'i', 'o',
0xb0, 't', 'r', 'a', 'n', 's', 'a', 'c', 't', 'i', 'o', 'n', '_', 'u', 'u', 'i', 'd', 0xd9, 0x24, 'c', '0', '7', 'e', 'e', '5', 'e', '1', '-', '7', '0', 'b', 'e', '-', '4', '4', '4', 'c', '-', 'a', '1', '5', '6', '-', '0', '9', '7', 'c', '7', '6', '7', 'a', 'd', '8', 'a', 'a',
}

buf, err := DecodeThenValidateBytes[Message](invalid, Msgpack)
assert.ErrorIs(err, ErrNotUTF8)
assert.Nil(buf)
}

func TestAllFormats(t *testing.T) {
require.NotNil(t, AllFormats())
assert.Contains(t, AllFormats(), JSON)
Expand Down
7 changes: 1 addition & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
module github.com/xmidt-org/wrp-go/v4
module github.com/xmidt-org/wrp-go/v5

go 1.21

require (
github.com/k0kubun/pp v3.0.1+incompatible
github.com/stretchr/testify v1.10.0
github.com/tinylib/msgp v1.2.5
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
golang.org/x/sys v0.29.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 0 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c h1:dAMKvw0MlJT1GshSTtih8C2gDs04w8dReiOGXrGLNoY=
github.com/philhofer/fwd v1.1.3-0.20240916144458-20a13a1f6b7c/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand All @@ -28,9 +20,6 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tinylib/msgp v1.2.5 h1:WeQg1whrXRFiZusidTQqzETkRpGjFjcIhW6uqWH09po=
github.com/tinylib/msgp v1.2.5/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
Expand Down
51 changes: 33 additions & 18 deletions is.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ package wrp
// Is reports whether the msg is the same type as the target, or is convertible
// to the target.
//
// If the msg is a *Message, it will be validated against the provided
// If the msg parameter is a *Message, it will be validated against the provided
// validators. If the msg is not a *Message, the validators will be ignored.
// If the validators are not provided, the msg will be validated against the
// default validators. To skip validation, provide the NoStandardValidation()
// as a validator.
func Is(msg, target Union, validators ...Processor) bool {
if msg == nil || target == nil {
return msg == target
return msg == nil && target == nil
}

msgType := msg.MsgType()
Expand All @@ -32,34 +32,49 @@ func Is(msg, target Union, validators ...Processor) bool {
return true
}

// As converts the msg into the target, if possible.
// As converts the src into the dest, if possible.
//
// One of the msg or target must be a *Message.
// The src will be converted into the dest if the types match. If the src is
// not a *Message, the src will be converted into a *Message first. Validators
// are then applied to the intermediate *Message. If the dest is a *Message,
// the type of the dest will be overwritten by the type of the src and is
// ignored for comparison purposes.
//
// When the msg is a *Message, it will be validated against the provided
// validators. If the validators are not provided, the msg will be validated
// If the validators are not provided, the msg will be validated
// against the default validators. To skip validation, provide the
// NoStandardValidation() as a validator. If the message is not convertible to
// the target, an error will be returned.
func As(msg, target Union, validators ...Processor) error {
if msg == nil || target == nil {
if msg == target {
// the target, or validation fails, an error will be returned.
func As(src, dst Union, validators ...Processor) error {
if src == nil || dst == nil {
if src == dst {
return nil
}
return ErrInvalidMessageType
}

msgType := msg.MsgType()
targetType := target.MsgType()
srcType := src.MsgType()
dstType := dst.MsgType()

if !msgType.IsValid() || !targetType.IsValid() || msgType != targetType {
if !srcType.IsValid() {
return ErrInvalidMessageType
}

if m, ok := msg.(*Message); ok {
return target.From(m, validators...)
} else if s, ok := target.(*Message); ok {
return s.To(s, validators...)
if d, ok := dst.(*Message); ok {
// Because the target is the *Message, we don't care what the type is.
// The type will be overwritten by the source.
return src.To(d, validators...)
}

if !dstType.IsValid() || srcType != dstType {
return ErrInvalidMessageType
}

if s, ok := src.(*Message); ok {
return dst.From(s, validators...)
}
return ErrInvalidMessageType

var tmp Message
_ = src.To(&tmp, NoStandardValidation())

return dst.From(&tmp, validators...)
}
100 changes: 62 additions & 38 deletions is_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xmidt-org/wrp-go/v4"
"github.com/xmidt-org/wrp-go/v5"
)

func TestIs(t *testing.T) {
Expand All @@ -26,7 +26,7 @@ func TestIs(t *testing.T) {
}, {
desc: "nil msg and non-nil target",
msg: nil,
target: &wrp.Message{Type: 11},
target: &wrp.Message{Type: wrp.UnknownMessageType},
want: false,
}, {
desc: "non-nil msg, type is invalid",
Expand All @@ -35,13 +35,13 @@ func TestIs(t *testing.T) {
want: false,
}, {
desc: "non-nil msg and nil target",
msg: &wrp.Message{Type: 11},
msg: &wrp.Message{Type: wrp.UnknownMessageType},
target: nil,
want: false,
}, {
desc: "same msg and target type",
msg: &wrp.Message{Type: 11},
target: &wrp.Message{Type: 11},
msg: &wrp.Message{Type: wrp.UnknownMessageType},
target: &wrp.Message{Type: wrp.UnknownMessageType},
want: true,
}, {
desc: "different msg and target type",
Expand All @@ -50,23 +50,28 @@ func TestIs(t *testing.T) {
want: false,
}, {
desc: "msg type matches exact type",
msg: &wrp.Message{Type: 11},
msg: &wrp.Message{Type: wrp.UnknownMessageType},
target: &wrp.Unknown{},
want: true,
}, {
desc: "msg type matches exact type, inverse",
msg: &wrp.Unknown{},
target: &wrp.Message{Type: 11},
target: &wrp.Message{Type: wrp.UnknownMessageType},
want: true,
}, {
desc: "msg type is not the same type",
msg: &wrp.Message{Type: 11},
msg: &wrp.Message{Type: wrp.UnknownMessageType},
target: &wrp.SimpleEvent{},
want: false,
}, {
desc: "msg type is not the same type, inverse",
msg: &wrp.SimpleEvent{},
target: &wrp.Message{Type: 11},
target: &wrp.Message{Type: wrp.UnknownMessageType},
want: false,
}, {
desc: "msg type is not valid",
msg: &wrp.Message{Type: wrp.SimpleRequestResponseMessageType},
target: &wrp.SimpleRequestResponse{},
want: false,
},
}
Expand All @@ -82,80 +87,99 @@ func TestIs(t *testing.T) {
func TestAs(t *testing.T) {
tests := []struct {
desc string
msg wrp.Union
target wrp.Union
src wrp.Union
dst wrp.Union
want wrp.Union
Processor wrp.Processor
wantError bool
}{
{
desc: "nil src and nil dst",
src: nil,
dst: nil,
want: nil,
wantError: false,
}, {
desc: "valid msg and nil target",
src: &wrp.Message{Type: 11},
dst: nil,
want: nil,
wantError: true,
}, {
desc: "msg type matches exact type",
msg: &wrp.Message{
src: &wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Source: "mac:112233445566",
Destination: "event:device-status",
TransactionUUID: "12345678-1234-1234-1234-123456789012",
},
target: &wrp.SimpleRequestResponse{},
dst: &wrp.SimpleRequestResponse{},
want: &wrp.SimpleRequestResponse{
Source: "mac:112233445566",
Destination: "event:device-status",
TransactionUUID: "12345678-1234-1234-1234-123456789012",
},
Processor: wrp.NoStandardValidation(),
},
{
}, {
desc: "msg type matches exact type, inverse",
msg: &wrp.SimpleRequestResponse{
src: &wrp.SimpleRequestResponse{
Source: "mac:112233445566",
Destination: "event:device-status",
TransactionUUID: "12345678-1234-1234-1234-123456789012",
},
target: &wrp.Message{},
dst: &wrp.Message{},
want: &wrp.Message{
Type: wrp.SimpleRequestResponseMessageType,
Source: "mac:112233445566",
Destination: "event:device-status",
TransactionUUID: "12345678-1234-1234-1234-123456789012",
},
Processor: wrp.NoStandardValidation(),
},
/*
{
desc: "msg type is not the same type",
msg: &wrp.Message{Type: wrp.SimpleRequestResponseMessageType},
target: &wrp.SimpleEvent{},
wantError: true,
},
{
desc: "msg type is not the same type, inverse",
msg: &wrp.SimpleEvent{},
target: &wrp.Message{Type: wrp.SimpleRequestResponseMessageType},
wantError: true,
}, {
desc: "msg type is not the same type",
src: &wrp.Message{Type: wrp.SimpleRequestResponseMessageType},
dst: &wrp.SimpleEvent{},
wantError: true,
}, {
desc: "msg type is not the same type, inverse",
src: &wrp.SimpleEvent{},
dst: &wrp.Message{Type: wrp.SimpleRequestResponseMessageType},
wantError: true,
}, {
desc: "invalid message type",
src: &wrp.Message{Type: wrp.MessageType(999)},
dst: &wrp.SimpleRequestResponse{},
wantError: true,
}, {
desc: "from and to the same type ... or basically a clone with validation",
src: &wrp.Authorization{
Status: 200,
},
{
desc: "invalid message type",
msg: &wrp.Message{Type: wrp.MessageType(999)},
target: &wrp.SimpleRequestResponse{},
wantError: true,
dst: &wrp.Authorization{},
want: &wrp.Authorization{
Status: 200,
},
*/
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
err := wrp.As(tt.msg, tt.target, tt.Processor)
err := wrp.As(tt.src, tt.dst, tt.Processor)
if tt.wantError {
assert.Error(t, err)
return
}

assert.NoError(t, err)
if tt.want == nil {
return
}

var want wrp.Message
tt.want.To(&want)

var got wrp.Message
tt.target.To(&got)
tt.dst.To(&got)

assert.Equal(t, want, got)
})
Expand Down
4 changes: 2 additions & 2 deletions messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ type msgTest struct {
invalid bool
}

func int64Ptr(v int64) *int64 {
return &v
func int64Ptr(value int64) *int64 {
return &value
}

var testMessages = []msgTest{
Expand Down
Loading

0 comments on commit 38bb380

Please sign in to comment.