-
I just discovered this library and am very pleased overall! I am struggling a bit with properly handling protobuf structs. In the default Python bindings, I am able to use any dict containing JSON-serializable values directly to my struct. I am not able to achieve this with this library however. Let me give a short example. Consider the following protobuf definition: import "google/protobuf/struct.proto";
message Request {
google.protobuf.Struct metadata = 1;
} In Typescript, I now try to create such a message with the following code import { Request } from "...";
const req: Request = {
metadata = { id: 1 }
} I get the following error:
The problem seems to be that such structs are also strongly typed according to the actual serialization format. Thus, a struct is defined as follows: fields: {
[key: string]: Value;
}; I also tried to assign my metadata as follows: import { Request } from "...";
const req: Request = {
metadata = { fields: { id: 1 } }
} In this case, I get the error
export interface Value {
/**
* @generated from protobuf oneof: kind
*/
kind: {
oneofKind: "nullValue";
/**
* Represents a null value.
*
* @generated from protobuf field: google.protobuf.NullValue null_value = 1;
*/
nullValue: NullValue;
} | {
oneofKind: "numberValue";
/**
* Represents a double value.
*
* @generated from protobuf field: double number_value = 2;
*/
numberValue: number;
} | {
oneofKind: "stringValue";
/**
* Represents a string value.
*
* @generated from protobuf field: string string_value = 3;
*/
stringValue: string;
} | {
oneofKind: "boolValue";
/**
* Represents a boolean value.
*
* @generated from protobuf field: bool bool_value = 4;
*/
boolValue: boolean;
} | {
oneofKind: "structValue";
/**
* Represents a structured value.
*
* @generated from protobuf field: google.protobuf.Struct struct_value = 5;
*/
structValue: Struct;
} | {
oneofKind: "listValue";
/**
* Represents a repeated `Value`.
*
* @generated from protobuf field: google.protobuf.ListValue list_value = 6;
*/
listValue: ListValue;
} | {
oneofKind: undefined;
};
} Here I stopped my investigation, because as you can see, it is essentially a 1-to-1 mapping of the underlying format of a protobuf struct. Thus my question is: Are structs currently not supported (they are at least not listed in the well-known types in the manual) or did I just not find the correct way of using them? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Mirko, you are correct, we're representing Struct exactly like it's defined in it's proto source. I agree it is terrible to use 😆 However, Struct maps perfectly to a JSON object (and our Struct.fromJson({
"foo": "you can put any JSON here",
"bar": 123,
}); Does that help? |
Beta Was this translation helpful? Give feedback.
Hi Mirko, you are correct, we're representing Struct exactly like it's defined in it's proto source.
I agree it is terrible to use 😆
However, Struct maps perfectly to a JSON object (and our
JsonObject
type), so you can do:Does that help?