Giter Site home page Giter Site logo

Comments (5)

wongoo avatar wongoo commented on May 19, 2024

@gaoxinge yes,list should support nil item,u can submit a pr with unit test about it. thanks!

from dubbo-go-hessian2.

wongoo avatar wongoo commented on May 19, 2024

@gaoxinge slice support nil element only if the type of element is pointer, which must be verified when decoding. The following is a test to append nil to slice:

func TestSliceAppendNil(t *testing.T) {
	type Msg struct {
		Text string
	}
	a := Msg{Text: "a"}
	c := Msg{Text: "c"}

	var list []*Msg

	list = append(list, &a)
	list = append(list, nil) // append nil
	list = append(list, &c)

	assert.Equal(t, 3, len(list))
	assert.Equal(t, "a", list[0].Text)
	assert.Nil(t, list[1])
	assert.Equal(t, "c", list[2].Text)

	var list2 []*Msg
	v := reflect.ValueOf(list)
	vv := reflect.ValueOf(list2)

	for i := 0; i < v.Len(); i++ {
		vv = reflect.Append(vv, v.Index(i)) // support append nil value
	}
	list2 = vv.Interface().([]*Msg)

	assert.Equal(t, 3, len(list2))
	assert.Equal(t, "a", list2[0].Text)
	assert.Nil(t, list2[1])
	assert.Equal(t, "c", list2[2].Text)
}

from dubbo-go-hessian2.

gaoxinge avatar gaoxinge commented on May 19, 2024

@wongoo Yes, if the slice hold a concrete type (Msg or *Msg), we should consinder to verify whether the type of element is a pointer when adding a nil. However, we use a abstract type (Interface{}) in list.go , which can hold anything, including nil.

from dubbo-go-hessian2.

wongoo avatar wongoo commented on May 19, 2024

@gaoxinge yes, currently it ignore the list type and uses []interface{} to accept elements, and u can submit a pr for it .

While in the future, may be we can refactor to define the exact type of slice. see another implement
https://github.com/vogo/gohessian/blob/228bdae7a821ce53f4512694969325e7d60bc264/list.go#L194

from dubbo-go-hessian2.

gaoxinge avatar gaoxinge commented on May 19, 2024

@wongoo You are very right! Although []interface{}{} is a dynamic type slice, we can't use reflect.Append to append a interface{}(nil) (or reflect.ValueOf(nil)) to the slice. So the code below

https://github.com/dubbogo/hessian2/blob/eb9857f7fae12289cad801c2914fb0b5cffbff33/list.go#L181-L195

will throw a runtime error at line 191 if variable it is nil.

So I raise a question, and maybe we can use this answer to work around this.

from dubbo-go-hessian2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.