2

My goal is to create a strict protobuf. This is because the server did not tell me that I used the wrong key UserId instead of user_id when I sent a POST request.

The protobuf

message GarageAndUserId {
    string user_id = 1;
    Garage garage = 2;
}

The generated protobuf

type GarageAndUserId struct {
    state         protoimpl.MessageState
    sizeCache     protoimpl.SizeCache
    unknownFields protoimpl.UnknownFields

    UserId string  `protobuf:"bytes,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
    Garage *Garage `protobuf:"bytes,2,opt,name=garage,proto3" json:"garage,omitempty"`
}

One solution I can think of is to create if else statement inside the handler.

    var requestBody model.GarageAndUserId
    err := decoder.Decode(&requestBody)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }

    if requestBody.UserId == nil {
      // do something.
    }

The solution I am looking for:

  1. Add something to the protobuf like
message GarageAndUserId {
    required string user_id = 1;
    required Garage garage = 2;
}
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31

0 Answers0