0

I created the following protobuf object:

message House {
    message Room {
        message Attributes {
            string bed = 1;
            string desk = 2;
        }
    }
    message Kitchen {
        message Attributes {
            string stove = 1;
            string fridge = 2;
        }
    }
    Room room = 1;
    Kitchen kitchen = 2; 
 }

I'm trying to initialize a House object using:

attributes = House.Room.Attributes(
        bed = "Queen",
        desk = "Office desk"
    )

request = House(
        room=House.Room(
            Attributes = attributes
        ),
        sourceFactAttributes=None
    )

However, I keep getting the following error: ValueError: Protocol message Room has no "Attributes" field.

1 Answers1

0

Nesting messages like this can get messy.

You may find it easier to keep everything flat, i.e. have House, Room, Attributes all defined at the top level as messages. See example at end.

Your issue is that the message (type) Attributes is defined in the message House but isn't then referenced as an field.

This corresponds to what you're doing with Room and Kitchen where, after defining the message (type), you then define a field that uses the type.

The solution is to add fields to the message Room and to the message Kitchen for the type Attributes, i.e.:

message House {
    message Room {
        message Attributes {
            string bed = 1;
            string desk = 2;
        }
        Attributes attributes = 1; // Here
    }
    message Kitchen {
        message Attributes {
            string stove = 1;
            string fridge = 2;
        }
        Attributes attributes = 1; // Here
    }
    Room room = 1;
    Kitchen kitchen = 2; 
}

I think this is easier understood as:

message House {
    Room room = 1;
    Room kitchen = 2; // Room==Kitchen so define it once
}

message Room {
    Attributes attributes = 1;
}

message Attributes {
    string bed = 1;
    string desk = 2;
}

NOTE Flattening the messages also makes the distinction between message definitions and field types clearer. And, in this case, allows us to see that Room and Kitchen are the same definition and so may benefit from sharing a type.

DazWilkin
  • 32,823
  • 5
  • 47
  • 88