Skip to content

Accessing nested Messages with [[ does not behave as expected #75

@Selbosh

Description

@Selbosh

In R, if I have a nested list, then I can access elements within it using repeated $ signs, or repeated square brackets, like so:

nested_list <- list(top = list(middle = list(tail = 42)))
nested_list$top$middle$tail
## [1] 42
nested_list[['top']]['middle']]['tail']]
## [1] 42

You can also access a nested list by passing a vector to the [[ function, like so:

nested_list[[c('top', 'middle', 'tail')]]
## 42

But while the first two methods work with nested RProtoBuf Messages, the last way doesn't. That is, if I pass a character vector to a Message in double square brackets, then it only returns values from the top level.

The tutorial message types don't demonstrate this problem because all of their nested Message fields are also repeated. Here is a minimal example:

syntax = proto3;
message NestedList {
  .TopList top = 1;
}
message TopList {
  .MiddleList middle = 1;
}
message MiddleList {
  int64 tail = 1;
}

The first two methods work:

readProtoFiles('nested.proto') # containing the definition above
nested_pb <- NestedList$new(top = TopList$new(middle = MiddleList$new(tail = 42)))
nested_pb$top$middle$tail
## [1] 42
nested_pb[['top']][['middle']][['tail']]
## [1] 42

But the last one doesn't.

nested_pb[[c('top', 'middle', 'tail')]]
## message of type 'TopList' with 1 field set

This seems inconsistent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions