Skip to content

Cannot extract extension from FieldOptions message #372

@tiziano88

Description

@tiziano88

even after defining an extension, calling proto.HasExtension(fd.Options, pb.E_MyExtension) always returns false.

On my machine, FieldOptions has the following methods:

func (m *FieldOptions) Reset()                    { *m = FieldOptions{} }
func (m *FieldOptions) String() string            { return proto.CompactTextString(m) }
func (*FieldOptions) ProtoMessage()               {}
func (*FieldOptions) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{11} }

var extRange_FieldOptions = []proto.ExtensionRange{
	{1000, 536870911},
}

func (*FieldOptions) ExtensionRangeArray() []proto.ExtensionRange {
	return extRange_FieldOptions
}

but HasExtension checks whether the message is extendable:

// extendableProto is an interface implemented by any protocol buffer generated by the current
// proto compiler that may be extended.
type extendableProto interface {
	Message
	ExtensionRangeArray() []ExtensionRange
	extensionsWrite() map[int32]Extension
	extensionsRead() (map[int32]Extension, sync.Locker)
}

// extendableProtoV1 is an interface implemented by a protocol buffer generated by the previous
// version of the proto compiler that may be extended.
type extendableProtoV1 interface {
	Message
	ExtensionRangeArray() []ExtensionRange
	ExtensionMap() map[int32]Extension
}

...

// extendable returns the extendableProto interface for the given generated proto message.
// If the proto message has the old extension format, it returns a wrapper that implements
// the extendableProto interface.
func extendable(p interface{}) (extendableProto, bool) {
	if ep, ok := p.(extendableProto); ok {
		return ep, ok
	}
	if ep, ok := p.(extendableProtoV1); ok {
		return extensionAdapter{ep}, ok
	}
	return nil, false
}

but it looks like FieldOptions does not have the extensionsWrite and extensionsRead methods, so this fails immediately.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions