Skip to content

Commit

Permalink
Deprecate SetData method
Browse files Browse the repository at this point in the history
  • Loading branch information
mfdeveloper508 committed Oct 2, 2023
1 parent b05481c commit a4b1be2
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 124 deletions.
15 changes: 8 additions & 7 deletions field/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (f *Binary) Unpack(data []byte) (int, error) {
return read + prefBytes, nil
}

// Deprecated. Use Marshal instead
func (f *Binary) SetData(data interface{}) error {
return f.Marshal(data)
}

func (f *Binary) Unmarshal(v interface{}) error {
if v == nil {
return nil
Expand All @@ -129,12 +134,12 @@ func (f *Binary) Unmarshal(v interface{}) error {
return nil
}

func (f *Binary) SetData(data interface{}) error {
if data == nil {
func (f *Binary) Marshal(v interface{}) error {
if v == nil {
return nil
}

bin, ok := data.(*Binary)
bin, ok := v.(*Binary)
if !ok {
return errors.New("data does not match required *Binary type")
}
Expand All @@ -146,10 +151,6 @@ func (f *Binary) SetData(data interface{}) error {
return nil
}

func (f *Binary) Marshal(data interface{}) error {
return f.SetData(data)
}

func (f *Binary) MarshalJSON() ([]byte, error) {
str, err := f.String()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions field/binary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ func TestBinaryField(t *testing.T) {
require.Equal(t, in, bin.value)
})

t.Run("SetData sets data to the field", func(t *testing.T) {
t.Run("Marshal sets data to the field", func(t *testing.T) {
bin := NewBinary(spec)
bin.SetData(NewBinaryValue(in))
bin.Marshal(NewBinaryValue(in))

packed, err := bin.Pack()

Expand All @@ -71,7 +71,7 @@ func TestBinaryField(t *testing.T) {
t.Run("SetBytes sets data to the data field", func(t *testing.T) {
bin := NewBinary(spec)
data := &Binary{}
bin.SetData(data)
bin.Marshal(data)

err := bin.SetBytes(in)
require.NoError(t, err)
Expand All @@ -90,7 +90,7 @@ func TestBinaryField(t *testing.T) {
t.Run("Unpack sets data to data value", func(t *testing.T) {
bin := NewBinary(spec)
data := NewBinaryValue([]byte{})
bin.SetData(data)
bin.Marshal(data)

n, err := bin.Unpack(in)

Expand Down
15 changes: 8 additions & 7 deletions field/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func (f *Bitmap) Unpack(data []byte) (int, error) {
return read, nil
}

// Deprecated. Use Marshal instead
func (f *Bitmap) SetData(data interface{}) error {
return f.Marshal(data)
}

func (f *Bitmap) Unmarshal(v interface{}) error {
if v == nil {
return nil
Expand All @@ -129,12 +134,12 @@ func (f *Bitmap) Unmarshal(v interface{}) error {
return nil
}

func (f *Bitmap) SetData(data interface{}) error {
if data == nil {
func (f *Bitmap) Marshal(v interface{}) error {
if v == nil {
return nil
}

bmap, ok := data.(*Bitmap)
bmap, ok := v.(*Bitmap)
if !ok {
return fmt.Errorf("data does not match required *Bitmap type")
}
Expand All @@ -143,10 +148,6 @@ func (f *Bitmap) SetData(data interface{}) error {
return nil
}

func (f *Bitmap) Marshal(data interface{}) error {
return f.SetData(data)
}

// Reset resets the bitmap to its initial state because of how message works,
// Message need a way to initialize bitmap. That's why we set parameters to
// their default values here like we do in constructor.
Expand Down
10 changes: 5 additions & 5 deletions field/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ func TestBitmap_Unmarshal(t *testing.T) {
})
}

func TestBitmap_SetData(t *testing.T) {
func TestBitmap_Marshal(t *testing.T) {
spec := &Spec{
Description: "Bitmap",
Enc: encoding.BytesToASCIIHex,
Expand All @@ -327,7 +327,7 @@ func TestBitmap_SetData(t *testing.T) {

t.Run("Nil data causes no side effects", func(t *testing.T) {
bitmap := NewBitmap(spec)
err := bitmap.SetData(nil)
err := bitmap.Marshal(nil)
require.NoError(t, err)
require.Equal(t, NewBitmap(spec), bitmap)
})
Expand All @@ -339,11 +339,11 @@ func TestBitmap_SetData(t *testing.T) {
a string
}{"left"}

err := bitmap.SetData(str)
err := bitmap.Marshal(str)
require.Error(t, err)
})

t.Run("Unpack sets the data field with the correct bitmap provided using SetData", func(t *testing.T) {
t.Run("Unpack sets the data field with the correct bitmap provided using Marshal", func(t *testing.T) {
bitmap := NewBitmap(spec)

// set bit: 10
Expand All @@ -361,7 +361,7 @@ func TestBitmap_SetData(t *testing.T) {
require.Equal(t, bitmapBytes, dataBytes)
})

t.Run("Pack returns bytes using the bitmap provided using SetData", func(t *testing.T) {
t.Run("Pack returns bytes using the bitmap provided using Marshal", func(t *testing.T) {
bitmap := NewBitmap(spec)

data := NewBitmap(&Spec{})
Expand Down
Loading

0 comments on commit a4b1be2

Please sign in to comment.