The proto nested structure cannot be bound to http - go

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;
}

Related

Go Testing define custom structure

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

Proto2 encode/decode issues after adding new message

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"
}
}

Unmarshal custom types with jsonpb

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

go-swagger with path param

Route:
// swagger:route DELETE /v1/path/{id} api DeleteV1
//
// Deletes an item.
//
// Schemes: https
//
// Responses:
// 202: AcceptedResponse
// 401: UnauthorizedErrorResponse
// 500: InternalServerErrorResponse
"/v1/path/{id}": {
"DELETE": Handle(DeleteRequest{}),
"OPTIONS": s.handleOptionsRequest,
},
Params/response model:
// Description
//
// swagger:parameters DeleteV1
type DeleteRequest struct {
// id of an item
//
// In: path
id string `json:"id"`
cookies []*http.Cookie
}
// Description
//
// swagger:response DeleteResponse
type DeleteResponse struct {
}
I keep getting - path param "{id}" has no parameter definition whatever I try.
The endpoint takes only path param "id", cookies (they are not visible in a swagger model) and returns an empty body and an HTTP status code.
How to make go-swagger see the "parameter definition"for the "{id}"?
The struct field needs to be exported. Try:
type DeleteRequest struct {
// ID of an item
//
// In: path
ID string `json:"id"`

How to write text format when proto including google::protobuf::Any

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: "..."
}
}
}

Resources