When converting a Pydantic model that uses recursion into a gRPC protobuf format, you will need to define the nested message types for any nested models.
For example, consider the following Pydantic model:
from typing import List, Optional
from pydantic import BaseModel
class Node(BaseModel):
label: str
children: Optional[List['Node']] = None
To convert this to a gRPC protobuf format, you will need to define a corresponding message type:
syntax = "proto3";
message Node {
string label = 1;
repeated Node children = 2;
}
Note that the repeated
keyword is used to indicate that children
is a list.
If the nested model is used in multiple places, you may want to define it as a separate message type and reference it from the parent message types.
For example, consider a modified version of the Node
model that also includes a reference to a Parent
:
from pydantic import BaseModel
class Node(BaseModel):
label: str
parent: Optional['Parent'] = None
children: List['Node'] = []
class Parent(BaseModel):
label: str
child: Optional[Node] = None
To convert this to a gRPC protobuf format, you can define the nested Node
message type as a separate message:
syntax = "proto3";
message Node {
string label = 1;
optional Parent parent = 2;
repeated Node children = 3;
}
message Parent {
string label = 1;
optional Node child = 2;
}
Note that the nested Node
message type is referenced in the Parent
message type.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-18 16:59:57 +0000
Seen: 11 times
Last updated: Jul 18 '23
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?
How can popen() be used to direct streaming data to TAR?
How does iOS retrieve information from a BLE device?
How can Django Admin accommodate a variety of formats and locales for its input fields?