Skip to content

Commit

Permalink
unmarshal does not return nil object when value is nil
Browse files Browse the repository at this point in the history
Signed-off-by: Iceber Gu <wei.cai-nat@daocloud.io>
  • Loading branch information
Iceber committed Apr 14, 2023
1 parent 2a94991 commit b2621cc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 4 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,6 @@ func UnmarshalToByTypeURL(typeURL string, value []byte, out interface{}) error {
}

func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error) {
if value == nil {
return nil, nil
}

t, err := getTypeByUrl(typeURL)
if err != nil {
return nil, err
Expand All @@ -216,6 +212,10 @@ func unmarshal(typeURL string, value []byte, v interface{}) (interface{}, error)
}
}

if value == nil {
return v, err
}

if t.isProto {
switch t := v.(type) {
case proto.Message:
Expand Down
18 changes: 8 additions & 10 deletions types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,22 @@ func TestRegisterDiffUrls(t *testing.T) {
Register(&test{}, "test", "two")
}

func TestUnmarshalNil(t *testing.T) {
var pba *anypb.Any // This is nil.
var a Any = pba // This is typed nil.
func TestUnmarshalNilValue(t *testing.T) {
clear()
Register(&test{}, "test")

if pba != nil {
t.Fatal("pbany must be nil")
}
if a == nil {
t.Fatal("nilany must not be nil")
var a Any = &anyType{
typeURL: "test",
value: nil,
}

actual, err := UnmarshalAny(a)
if err != nil {
t.Fatal(err)
}

if actual != nil {
t.Fatalf("expected nil, got %v", actual)
if _, ok := actual.(*test); !ok {
t.Fatalf("expected *test, got %v", actual)
}
}

Expand Down

0 comments on commit b2621cc

Please sign in to comment.