Skip to content

Commit

Permalink
refactor: remove config option and change null encoding to default su…
Browse files Browse the repository at this point in the history
…pport
  • Loading branch information
root committed Nov 26, 2023
1 parent 5d34775 commit ebf53db
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 41 deletions.
40 changes: 9 additions & 31 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ type Encoder struct {
classInfoList []*ClassInfo
buffer []byte
refMap map[unsafe.Pointer]_refElem
config *EncoderConfig
}

// classIndex find the index of the given java name in encoder class info list.
Expand All @@ -50,32 +49,12 @@ func (e *Encoder) classIndex(javaName string) int {
}

// NewEncoder generate an encoder instance
func NewEncoder(opts ...EncoderOption) *Encoder {
func NewEncoder() *Encoder {
buffer := make([]byte, 64)
config := &EncoderConfig{}
for _, opt := range opts {
opt(config)
}

return &Encoder{
buffer: buffer[:0],
refMap: make(map[unsafe.Pointer]_refElem, 7),
config: config,
}
}

// EncoderConfig config for encoder
type EncoderConfig struct {
JavaNullCompatible bool
}

// EncoderOption inject configuration into encoder
type EncoderOption func(*EncoderConfig)

// WithJavaNullCompatible configuring java null compatible for encoder
func WithJavaNullCompatible() EncoderOption {
return func(config *EncoderConfig) {
config.JavaNullCompatible = true
}
}

Expand Down Expand Up @@ -115,13 +94,7 @@ func (e *Encoder) Append(buf []byte) {

// Encode If @v can not be encoded, the return value is nil. At present only struct may can not be encoded.
func (e *Encoder) Encode(v interface{}) error {
if e.config.JavaNullCompatible {
vv := reflect.ValueOf(v)
if vv.Kind() == reflect.Ptr && vv.IsNil() {
e.buffer = EncNull(e.buffer)
return nil
}
} else if v == nil {
if v == nil {
e.buffer = EncNull(e.buffer)
return nil
}
Expand Down Expand Up @@ -228,7 +201,7 @@ func (e *Encoder) Encode(v interface{}) error {
if vv != nil {
e.buffer = encBool(e.buffer, *vv)
} else {
e.buffer = encBool(e.buffer, false)
e.buffer = EncNull(e.buffer)
}
case reflect.Int32:
if t == _typeOfRune {
Expand All @@ -246,7 +219,12 @@ func (e *Encoder) Encode(v interface{}) error {
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64: // resolve base type
vVal := reflect.ValueOf(v)
if reflect.Ptr == vVal.Kind() && !vVal.IsNil() {
if reflect.Ptr == vVal.Kind() {
if vVal.IsNil() {
e.buffer = EncNull(e.buffer)
return nil
}

return e.Encode(vVal.Elem().Interface())
}
default:
Expand Down
2 changes: 1 addition & 1 deletion int.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (d *Decoder) decInt32(flag int32) (int32, error) {

func (e *Encoder) encTypeInt32(b []byte, p interface{}) ([]byte, error) {
value := reflect.ValueOf(p)
if PackPtr(value).IsNil() {
if value.Kind() == reflect.Ptr && value.IsNil() || PackPtr(value).IsNil() {
return EncNull(b), nil
}
value = UnpackPtrValue(value)
Expand Down
68 changes: 64 additions & 4 deletions null_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,74 @@ func TestNullEncode(t *testing.T) {
testJavaDecode(t, "argNull", nil)
}

func TestNullCompatibleEncode(t *testing.T) {
e := NewEncoder(
WithJavaNullCompatible(),
)
func TestNullIntPtr(t *testing.T) {
e := NewEncoder()
var null *int = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullBoolPtr(t *testing.T) {
e := NewEncoder()
var null *bool = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullInt32Ptr(t *testing.T) {
e := NewEncoder()
var null *int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullSlice(t *testing.T) {
e := NewEncoder()
var null []int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

func TestNullMap(t *testing.T) {
e := NewEncoder()
var null map[bool]int32 = nil
e.Encode(null)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("N"), e.buffer, t)
}

type NullFieldStruct struct {
Int *int
Bool *bool
Int32 *int32
Slice []int32
Map map[bool]int32
}

func (*NullFieldStruct) JavaClassName() string {
return "NullFieldStruct"
}

func TestNullFieldStruct(t *testing.T) {
e := NewEncoder()
req := &NullFieldStruct{}
e.Encode(req)
if e.Buffer() == nil {
t.Fail()
}
assertEqual([]byte("NNNNN"), e.buffer[len(e.buffer)-5:], t)
}
6 changes: 1 addition & 5 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,11 +681,7 @@ func TestBasePointer(t *testing.T) {
base = BasePointer{
A: nil,
}
expectedF := false
expectedBase := BasePointer{
A: &expectedF,
}
doTestBasePointer(t, &base, &expectedBase)
doTestBasePointer(t, &base, &base)
}

func doTestBasePointer(t *testing.T, base *BasePointer, expected *BasePointer) {
Expand Down

0 comments on commit ebf53db

Please sign in to comment.