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

Extensions are not encoded #104

Open
biosvs opened this issue Oct 4, 2023 · 0 comments
Open

Extensions are not encoded #104

biosvs opened this issue Oct 4, 2023 · 0 comments

Comments

@biosvs
Copy link
Contributor

biosvs commented Oct 4, 2023

It seems like extensions are not encoded to the wire (but what's interesting - decoded from the wire).

proto

syntax = "proto2";

option go_package = "protobuf-benchmarks/pb/google/test/test;testpb";

message MyMessage {
    optional int32 foo = 1;

    extensions 100 to max;
}

extend MyMessage {
    optional int32 bar = 100;
}

Test

import (
	...
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
	t.Run("encoding", func(t *testing.T) {
		m := &prototestpb.MyMessage{Foo: proto.Int32(1)}
		proto.SetExtension(m, prototestpb.E_Bar, int32(2))

		b, err := m.MarshalVT()
		require.NoError(t, err)

		m2 := &prototestpb.MyMessage{}
		err = m2.UnmarshalVT(b)
		require.NoError(t, err)

		assert.True(t, proto.HasExtension(m2, prototestpb.E_Bar)) // Fail, Should be true
	})

	t.Run("decoding", func(t *testing.T) {
		m := &prototestpb.MyMessage{Foo: proto.Int32(1)}
		proto.SetExtension(m, prototestpb.E_Bar, int32(2))

		b, err := proto.Marshal(m) // Use native proto for marshalling
		require.NoError(t, err)

		m2 := &prototestpb.MyMessage{}
		err = m2.UnmarshalVT(b)
		require.NoError(t, err)

		assert.True(t, proto.HasExtension(m2, prototestpb.E_Bar)) // Pass
	})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant