Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
Signed-off-by: LiangliangSui <[email protected]>
  • Loading branch information
LiangliangSui committed Apr 18, 2024
1 parent 5e3ebc0 commit 9b6c6ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ on:
push:
branches:
- main
- go_magic_number
- 'releases/**'
- 'deploy/**'
- 'test*'
Expand Down
16 changes: 16 additions & 0 deletions go/fury/fury.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ const (
isOutOfBandFlag
)

const MAGIC_NUMBER int16 = 0x62d4

type Fury struct {
typeResolver *typeResolver
refResolver *RefResolver
Expand Down Expand Up @@ -121,6 +123,11 @@ func (f *Fury) Serialize(buf *ByteBuffer, v interface{}, callback BufferCallback
buffer = f.buffer
buffer.writerIndex = 0
}
if f.language == XLANG {
buffer.WriteInt16(MAGIC_NUMBER)
} else {
return fmt.Errorf("%d language is not supported", f.language)
}
var bitmap byte = 0
if isNil(reflect.ValueOf(v)) {
bitmap |= isNilFlag
Expand Down Expand Up @@ -310,6 +317,15 @@ func (f *Fury) Unmarshal(data []byte, v interface{}) error {

func (f *Fury) Deserialize(buf *ByteBuffer, v interface{}, buffers []*ByteBuffer) error {
defer f.resetRead()
if f.language == XLANG {
magicNumber := buf.ReadInt16()
if magicNumber != MAGIC_NUMBER {
return fmt.Errorf("the XLANG cross-language protocol is not currently used. For details," +
"see https://github.com/apache/incubator-fury/blob/main/docs/specification/xlang_serialization_spec.md")
}
} else {
return fmt.Errorf("%d language is not supported", f.language)
}
var bitmap = buf.ReadByte_()
if bitmap&isNilFlag == isNilFlag {
return nil
Expand Down
12 changes: 12 additions & 0 deletions go/fury/fury_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,18 @@ func TestSerializeStructSimple(t *testing.T) {
}
}

func TestSerializeBeginWithMagicNumber(t *testing.T) {
strSlice := []string{"str1", "str1", "", "", "str2"}
fury := NewFury(true)
bytes, err := fury.Marshal(strSlice)
require.Nil(t, err, fmt.Sprintf("serialize value %s with type %s failed: %s",
reflect.ValueOf(strSlice), reflect.TypeOf(strSlice), err))
// Contains at least two bytes.
require.True(t, len(bytes) > 2)
magicNumber := int16(bytes[0]) | (int16(bytes[1]) << 8)
require.Equal(t, magicNumber, MAGIC_NUMBER)
}

type Foo struct {
F1 int32
F2 string
Expand Down

0 comments on commit 9b6c6ff

Please sign in to comment.