Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should genericDecode also allow EOF error #477

Open
zinking opened this issue Nov 5, 2024 · 2 comments
Open

should genericDecode also allow EOF error #477

zinking opened this issue Nov 5, 2024 · 2 comments

Comments

@zinking
Copy link

zinking commented Nov 5, 2024

parsing to a struct object container allows EOF error, field not updated. However the behavior is different when you parse to a dynamic container such as any

func genericDecode(typ reflect2.Type, dec ValDecoder, r *Reader) any {
	ptr := typ.UnsafeNew()
	dec.Decode(ptr, r)
	if r.Error != nil {
		return nil
	}

	obj := typ.UnsafeIndirect(ptr)
	if reflect2.IsNil(obj) {
		return nil
	}
	return obj
}

should parse to a dynamic eface container (nil any), also allow such EOF error? instead of throwing away the result?

I guess it might be a bit complicated regarding the missing value

func TestDecodeCompatible(t *testing.T) {
	registry := NewAvroSchemaRegistry()
	err := registry.AddSchema("xxxx", `{
"type": "record",
"name": "schemaV1",
"namespace": "org.hamba.avro",
"fields": [
	{"name": "a", "type": "long", "default": 0}
]}`)

	if err != nil {
		t.Fatal(err)
	}

	type Object struct {
		A int64 `avro:"a"`
	}

	schema1, err := registry.GetSchema("xxxxxx")

	if err != nil {
		t.Fatal(err)
	}

	buf := bytes.NewBuffer(nil)
	err = schema1.NewEncoder(buf).Encode(&Object{A: 1})

	if err != nil {
		t.Fatal(err)
	}

	err = registry.AddSchema("xxxxxx", `{
"type": "record",
"name": "schemaV1",
"namespace": "org.hamba.avro",
"fields": [
	{"name": "a", "type": "long", "default": 0},
	{"name": "b", "type": "long", "default": 0}
]}`)

	if err != nil {
		t.Fatal(err)
	}

	type Object2 struct {
		A int64 `avro:"a"`
		B int64 `avro:"b"`
	}

	// var object2 Object2 // this works
        var object2 any // this wont work
	schema2, err := registry.GetSchema("xxxxxx")

	if err != nil {
		t.Fatal(err)
	}

	err = schema2.NewDecoder(buf).Decode(&object2)

	if err != nil {
		t.Fatal(err)
	}

	t.Logf("%+v", object2)
}
@nrwiersma
Copy link
Member

Can you give a short example of what is not working as expected?

@zinking
Copy link
Author

zinking commented Nov 6, 2024

@nrwiersma should have done so earlier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants