Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Practical gRPC

You're reading from   Practical gRPC Build highly-connected systems with a framework that can run on any platform

Arrow left icon
Product type Paperback
Published in Nov 2019
Publisher
ISBN-13 9781839211744
Length 169 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (6):
Arrow left icon
Robert Ross Robert Ross
Author Profile Icon Robert Ross
Robert Ross
Carles Sistare Carles Sistare
Author Profile Icon Carles Sistare
Carles Sistare
Joshua B. Humphries Joshua B. Humphries
Author Profile Icon Joshua B. Humphries
Joshua B. Humphries
Backstop Media LLC Backstop Media LLC
Author Profile Icon Backstop Media LLC
Backstop Media LLC
David Konsumer David Konsumer
Author Profile Icon David Konsumer
David Konsumer
David Muto David Muto
Author Profile Icon David Muto
David Muto
+2 more Show less
Arrow right icon
View More author details
Toc

Polymorphism

The idea of this section is to explain how inheritance may be implemented in order to enable polymorphism among messages. There are two methods, depending on the proto version that we are using:

  • Proto 2 uses Extensions
  • Proto 3 deprecates Extensions in favor of Any statement

Extensions are a range of tags reserved for future extended messages so you can use them for different purposes:

syntax = "proto2";

message Film {

    extensions 50 to 99;

}

message TerrorFilm {

    extend Film {

        required TerrorFilm film = 50;

    }

    bool has_zombies = 1;

}

message ScienceFictionFilm {

    extend Film {

        required ScienceFictionFilm film = 51;

    }

    bool has_jedis = 1;

}

In terms of binary serialization the final message will look the same as a non extended message, but in terms of code generated for accessing those fields, it’s a different story. Here is an example for setting the extended field film in a Java implementation.

ScienceFictionFilm...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £16.99/month. Cancel anytime