Skip to content

Poor interaction with embedding #291

Closed
@Merovius

Description

@Merovius

This program currently type-checks correctly, but then fails with "proto: no encoder for Empty empty.Empty [GetProperties]". The reason, from what I can tell, is that the proto package registers en-/decoders per message and then looks them up via their reflect.Type. It would be nice, if messages where correctly embeddable.

A (conceptually) simple way to do that, would be to change the ProtoMessage() method to ProtoMessage() proto.Message and have the generated types return themselves from it. The functions in the proto-package can then, instead of operating on a pb Message parameter, operate on pb.ProtoMessage() instead.

My specific usecase is, that I want to be able to attach additional methods to protobufs (or rather, have an interface that also guarantees protobuf-encodability). Currently I'm trying type Actuator interface{ proto.Message; Actuate(Context) error }, and recommend embedding a concrete message to a struct and add the Actuate method to that. I thought of alternatives (like changing it to interface{ Proto() proto.Message; … } or interface{ Marshal() ([]byte, error); Unmarshal([]byte) error; … }), but all of them require additional boilerplate for each implementation of Actuator. Correctly embeddable Messages would neatly avoid that.

Activity

dsnet

dsnet commented on Dec 27, 2017

@dsnet
Member

The fundamental issue is that the proto.Message interface is marginally better than interface{}. It is just a marker and the logic in the proto package makes all sort of deep assumptions about what the message looks like (i.e., that it is a flat struct with fields).

#364 is intended to make proto.Message into a behaviorally complete interface, eliminating this problem.

locked and limited conversation to collaborators on Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @Merovius@dsnet

        Issue actions

          Poor interaction with embedding · Issue #291 · golang/protobuf