vtprotobuf icon indicating copy to clipboard operation
vtprotobuf copied to clipboard

Extensions are not encoded

Open biosvs opened this issue 1 year ago • 0 comments

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
	})
}

biosvs avatar Oct 04 '23 14:10 biosvs