Skip to content

Commit

Permalink
will return ErrMesgNotSupport if mesg hasn't defined
Browse files Browse the repository at this point in the history
  • Loading branch information
francistm committed Nov 1, 2023
1 parent 7ee6920 commit 577ae81
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ jobs:
go-version: "1.20"

- name: Test
run: go test -v ./...
run: go test ./...
9 changes: 8 additions & 1 deletion decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Test_Unmarshal(t *testing.T) {
},
},
{
name: "mesg 0200 -2",
name: "mesg 0200 - 1",
mesgPack: new(message.MessagePack[message.MesgBody]),
data: []byte{0x7e, 0x02, 0x00, 0x00, 0x26, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x22, 0xb8, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0xba, 0x7f, 0x0e, 0x07, 0xe4, 0xf1, 0x1c, 0x00, 0x28, 0x00, 0x3c, 0x00, 0x00, 0x18, 0x07, 0x15, 0x10, 0x10, 0x10, 0x01, 0x04, 0x00, 0x00, 0x00, 0x64, 0x02, 0x02, 0x00, 0x37, 0x57, 0x7e},
assertFunc: func(t *testing.T, mesgPack *message.MessagePack[message.MesgBody]) {
Expand Down Expand Up @@ -77,13 +77,20 @@ func Test_Unmarshal(t *testing.T) {
assert.IsType(t, new(message.Body0200), mesgPack.PackBody)
},
},
{
name: "unknown mesg",
mesgPack: new(message.MessagePack[message.MesgBody]),
data: []byte{0x7e, 0x00, 0x00, 0x00, 0x05, 0x01, 0x86, 0x57, 0x40, 0x59, 0x79, 0x00, 0x8f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x2f, 0x7e},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := Unmarshal(tt.data, tt.mesgPack)

if tt.wantErr {
t.Log(err)
assert.Error(t, err)
} else if assert.NoError(t, err) {
tt.assertFunc(t, tt.mesgPack)
Expand Down
4 changes: 4 additions & 0 deletions message/const.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package message

import "errors"

const (
AckType_0001_Succeed byte = iota
AckType_0001_Failed
Expand Down Expand Up @@ -27,3 +29,5 @@ const (
Version2013 byte = iota
Version2019
)

var ErrMesgNotSupport = errors.New("unsupported message")
2 changes: 1 addition & 1 deletion message/mesg_pack.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions message/mesg_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,15 @@ func (p *MessagePack[T]) UnmarshalBinary(buf []byte) error {
_ = reader.UnreadFixedBytes(4)
}

checksumActually, err = bytes.CalcChecksum(buf[0 : len(buf)-1])

if err != nil {
return err
}

p.Checksum = buf[len(buf)-1]
p.checksumActually = checksumActually

// update PackBody field from readed bytes to struct
packBody, err := p.NewPackBodyFromMesgId()

Expand All @@ -145,15 +154,7 @@ func (p *MessagePack[T]) UnmarshalBinary(buf []byte) error {
return fmt.Errorf("can't convert mesgBody %T as %T", packBody, p.PackBody)
}

checksumActually, err = bytes.CalcChecksum(buf[0 : len(buf)-1])

if err != nil {
return err
}

p.PackBody = typedPackBody
p.Checksum = buf[len(buf)-1]
p.checksumActually = checksumActually

return nil
}
Expand Down
5 changes: 1 addition & 4 deletions tools/generator/decoder/gen_newpkg_body_from_mesgid.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func genNewPkgBodyFromMesgId(f *File, mesgDecls []*generator.MesgDecl) {

g.Default().Return(
Nil(),
Qual("fmt", "Errorf").Call(
Lit("unsupported messageId: 0x%.4X"),
Id("m").Dot("PackHeader").Dot("MessageID"),
),
Id("ErrMesgNotSupport"),
)
}),
),
Expand Down

0 comments on commit 577ae81

Please sign in to comment.