protocol-buffersgrpcproto

gRPC Proto Buff URL/URI type


Is there a way to use Url or Uri data type inside a message of gRPC? And if not what is the best way to do this?

I am using gRPC and Protocol Buffers for this and I run a backend go app to trigger popup notifications that display in my Flutter app. The notification has a link that takes the user to a webpage when clicked on in my Flutter app.

Right now I am using a String for this, like so:

message NotificationResponse{
    string title = 1;
    string url = 2;
}

I can't seem to find a way to use Url/Uri as a type. Is there such a thing?


Solution

  • Storing in a string is a totally viable solution. But if you would like to reduce the payload size a little and make the instantiation little bit safer, you could remove the schema part of the url (eg: https://).

    To do that you could do the following:

    message Url {
        enum Schema {
            UNSPECIFIED = 0;
            HTTP = 1;
            HTTPS = 2;
            // more if needed
        }
    
        Schema schema = 1;
        string rest = 2;
    }
    

    and then you can use it in your message like this:

    message NotificationResponse {
        string title = 1;
        Url url = 2;
    }
    

    That has one inconvenience though. You will have to concatenate that back in your client code, but in my opinion this is worth the effort since the concatenation is pretty trivial (change enum value to is text value and add ://).

    Note: doing the same enum trick for Domain Name (.com, .net, ...) would not be as trivial and would force you to store the path and the host in different field (not worth it since it increase payload).

    Let me know if you need more help.