My proto is something like:
message PlatformConfig {
google.protobuf.Any source_adapter_config = 1;
};
and I want to set proto:
message SessionBundleSourceAdapterConfig {
SessionBundleConfig config = 1;
}
into source_adapter_config, How to write the this TextFormat of protobuf.
Finally, I found that Any type need a type_url to specify type, for example:
platform_configs {
key: "tensorflow"
value {
source_adapter_config {
type_url:"type.googleapis.com/tensorflow.serving.SavedModelBundleSourceAdapterConfig"
value: "..."
}
}
}
Related
I have a proto with the following definitions.
import "google/protobuf/descriptor.proto";
extend google.protobuf.FieldOptions{
optional bool is_key = 50002;
}
message Foo{
int64 id = 1 [(is_key) = true];
}
I generated a .desc file for the above. I was able to access all the Fields and Message defined by the FieldDescriptorProto and DescriptorProto types but not sure how to access the options defined and the value provided to it in this case is_key.
Could anyone provide me with a java version that could access the options from the .desc file
Not sure If this is the right way but all I was trying to do was read custom options from a descriptor_set.desc file.
I parsed the desc file, the problem is that it returns fileDescriptorProto Types. So I parsed them and built a dependencies graph. This helped me to create FileDescriptor Types.
Through FileDescriptor Types I was able to get Extensions and add them to extensionRegistry.
Post Which I parsed the .desc file once again but this time with extensionRegistry.
val filePathToFileDescriptorMap = mutableMapOf <String, FileDescriptor>(DescriptorProtos.getDescriptor().file.name to
DescriptorProtos.getDescriptor())
val extensionRegistry = ExtensionRegistry.newInstance()
fun buildDependencies(fileDescriptorProto: DescriptorProtos.FileDescriptorProto,
filePathToFileDescriptorProtoMap: Map<String, DescriptorProtos.FileDescriptorProto>) : FileDescriptor {
val dependencies = fileDescriptorProto.dependencyList.map { dependency ->
if(filePathToFileDescriptorMap.containsKey(dependency)) {
filePathToFileDescriptorMap[dependency]
}
else if(!filePathToFileDescriptorProtoMap.containsKey(dependency)) {
throw Exception("dependency not found $dependency")
}
else {
buildDependencies(filePathToFileDescriptorProtoMap[dependency]!!, filePathToFileDescriptorProtoMap)
}
}
filePathToFileDescriptorMap[fileDescriptorProto.name] =
FileDescriptor.buildFrom(fileDescriptorProto, dependencies.toTypedArray())
filePathToFileDescriptorMap[fileDescriptorProto.name]!!.extensions.forEach {
if(it.type == Descriptors.FieldDescriptor.Type.MESSAGE){
extensionRegistry.add(it, DynamicMessage.newBuilder(it.messageType).build())
}else{
extensionRegistry.add(it)
}
}
return filePathToFileDescriptorMap[fileDescriptorProto.name]!!
}
fun registerExtensions(){
val fileDescriptorSet = DescriptorProtos.FileDescriptorSet.parseFrom(
FileInputStream("src/generated/resources/desc_set.desc"),
)
val filePathToFileDescriptorProtoMap = fileDescriptorSet.fileList.associateBy { it.name }
fileDescriptorSet.fileList.forEach { fileDescriptorProto ->
if(!filePathToFileDescriptorMap.containsKey(fileDescriptorProto.name)){
buildDependencies(fileDescriptorProto, filePathToFileDescriptorProtoMap)
}
}
val fileDescriptorSetWithOptions = DescriptorProtos.FileDescriptorSet.parseFrom(
FileInputStream("src/generated/resources/desc_set.desc"),
extensionRegistry
)
println(fileDescriptorSetWithOptions)
}
When the kratos proto file uses the http interface when the structure is nested, the parameter assignment cannot go to the structure
This`s my proto file
This`s my request
This`s my debug
I want like this:
pb.ListUserRequest{
Page: {
Index: 1,
Size: 10,
},
}
There is no binding parameter
what should i do
http can't decode inner_object by 'GET'
use 'POST'
option (google.api.http) = {
put: "/user/v1/list"
body: "*"
};
proto:
message ListUserRequest {
Page page = 1;
}
2.not use inner_object
proto:
message ListUserRequest {
int32 index = 1;
int32 size = 2;
}
I've followed the structure in a tf file, can you help me to create a proper structure, as I'm new to Go.
Here is tf
ipv4 = {
cidrblock = "10.0.0.0/16"
secondary = [
{
cidrs = "20.0.0.0/16"
enabled = true
},
{
cidrs = "30.0.0.0/16"
enabled = true
}
]
}
So I've an object of strings, as well a list of objects in the main object. I could make a primitive type, for example:
type ipv4 struct {
cidrblock string
cidrs string
enabled bool
}
type ipv6 struct {
border string
generate bool
}
type Sets struct {
Name string
IPv4 *ipv4
IPv6 *ipv6
Tags map[string]string
Tenancy string
}
But I would really like to have a complex structure
you can do something like this:
type ipv4 struct {
cidrblock string
secondary []ipv4secondary
}
type ipv4secondary struct {
cidrblock string
enabled bool
}
and use it as this:
example := ipv4{
cidrblock: "10.0.0.0/16",
secondary: []ipv4secondary{
ipv4secondary{cidrblock: "20.0.0.0/16", enabled: true},
ipv4secondary{cidrblock: "30.0.0.0/16", enabled: true},
},
}
here is the example: https://go.dev/play/p/U7o0BbAis9T
I'm fairly new to protocol buffers but have been trying to learn them as a means of sending data via MQTT. So far, I've been fine with creating proto messages and compiling them for python runtime, until I started to notice incompatibility between versions of my protobufs.
When I add a message type (no changes to existing messages/fields) to my server-side proto definitions without updating my client side proto definitions, decoding messages sent to the server give me non-deterministic results.
Here is an example of what I'm talking about:
Client proto:
message Wrapper {
optional uint32 id = 1;
optional string name = 2;
oneof payload {
Event event = 3;
Command command = 4;
}
}
message Event {
uint32 event_id = 1;
oneof event_payload {
LoginEvent login_event = 2;
LogoffEvent logoff_event = 3;
}
}
Server Proto:
message Wrapper {
optional uint32 id = 1;
optional string name = 2;
oneof payload {
Event event = 3;
Command command = 4;
}
message Event {
uint32 event_id = 1;
oneof event_payload {
LoginEvent login_event = 2;
LogoffEvent logoff_event = 3;
NewUserEvent new_user_event = 4;
}
}
I will encode and send a message from the client:
message Wrapper {
id = 12345;
name = John;
event = {
login_event = ...
}
}
And will decode the message on the server and get:
message Wrapper {
id = 12345;
name = John;
event = {
logoff_event = ...
}
}
NOTE: The decoded message type isn't deterministic and changes between messages
Can someone explain why adding an event type seems to screw up decode? Or any best-practices I should obey to improve version compatibility? Thanks in advance!
This may be a side-effect of the Python implementation rather than a bug.
You don't include the code you're using to (un)marshal the protobufs.
In your example, does logoff_event contain a LogoffEvent message type?
You say non-deterministic? Do you see different event_payload messages types on the server for the same message sent by the client?
What is returned by:
msg = Wrapper() # Your decoded message
assert msg.event.WhichOneof("event_payload")
# or
assert msg.event.HasField("login_event")
See Python Generated Code: OneOf
Update 210927
I'm unable to repro the behavior you observe; it works as expected for me.
Client:
import client_pb2
msg = client_pb2.Wrapper()
msg.id=0
msg.name="Test"
msg.event.event_id=1
msg.event.login_event.y="Hello Freddie"
print(msg)
f=open("message","wb")
f.write(msg.SerializeToString())
f.close()
Yields:
id: 0
name: "Test"
event {
event_id: 1
login_event {
y: "Hello Freddie"
}
}
Server:
import server_pb2
msg = server_pb2.Wrapper()
f=open("message","rb")
msg.ParseFromString(f.read())
f.close
assert msg.event.WhichOneof("event_payload")
assert msg.event.HasField("login_event")
print(msg)
Yields:
id: 0
name: "Test"
event {
event_id: 1
login_event {
y: "Hello Freddie"
}
}
What's the best way to convert this json object to protobuf?
JSON:
{
"name": "test",
"_list": {
"some1": { "value": 1 },
"some2": [
{ "value": 2 },
{ "value": 3 },
]
}
}
Proto:
message Something {
string name = 1;
message ListType {
repeated string = 1;
}
map<string, ListType> _list = 2;
}
Without having the _list in the message I would use jsonpb.Unmarsal, but I can't think of a way to define the Unmarshaler interface on a type that is generated in a diff package.
I also thought of having _list as a Any (json.RawMessage) and handle it after the Unmarshal (but can't make this to work; err message: Any JSON doesn't have '#type')
With _list being inconsistent (not just a list of strings/map of values/etc) and you mentioning you looked into using Any you could consider making your message:
message Something {
string name = 1;
google.protobuf.Struct _list = 2;
}
https://github.com/golang/protobuf/blob/master/ptypes/struct/struct.proto
With that you can marshal/unmarshal json to/from proto messages using github.com/golang/protobuf/jsonpb which is actually designed for use with the grpc gateway but you can use it too