0Pricing
gRPC & High Performance APIs · Lekcja

Oneof, mapy i dobrze znane typy

Modeluj elastyczne i rozwijające się dane za pomocą pól protobuf oneof, map oraz standardowych dobrze znanych typów, takich jak Timestamp, Duration i Any.

Oneof, mapy i dobrze znane typy to bezpłatna lekcja gRPC & High Performance APIs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej gRPC & High Performance APIs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs gRPC & High Performance APIs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Beyond Plain Scalar Fields

Real schemas need more than strings and ints: mutually exclusive choices, key/value collections, and standard date/time types. Protobuf provides oneof, maps, and well-known types.

The oneof Construct

A oneof groups fields where at most one can be set at a time. Setting one clears the others, and only the active field is sent on the wire.

message Contact {
  oneof method {
    string email = 1;
    string phone = 2;
  }
}

Reading a oneof

Generated code gives a way to check which case is set, usually a case enum or accessor, so you branch on the active field.

switch c.Method.(type) {
case *Contact_Email: useEmail(c.GetEmail())
case *Contact_Phone: usePhone(c.GetPhone())
}

Map Fields

A map stores key/value pairs. Keys must be scalar (string/int); values can be any type except another map.

message Config {
  map<string, string> settings = 1;
}

How Maps Work on the Wire

Maps are sugar over a repeated message of key/value entries. Order is not guaranteed, and duplicate keys keep the last value — important when evolving schemas.

Well-Known Types

Protobuf ships standard types in google/protobuf/:

  • Timestamp — a point in time
  • Duration — a length of time
  • Any — a packed arbitrary message
  • Struct — dynamic JSON-like data

Timestamp and Duration

Prefer these over raw int seconds for clarity and tooling support. They map to native time types in most languages.

import 'google/protobuf/timestamp.proto';
message Event {
  google.protobuf.Timestamp created_at = 1;
}

Using Any

Any wraps any message plus a type URL, letting you carry heterogeneous payloads. The rich error model and reflection both use it.

import 'google/protobuf/any.proto';
message Envelope {
  google.protobuf.Any payload = 1;
}

FieldMask for Partial Updates

FieldMask lists which fields a request touches, enabling partial updates (PATCH-style) without ambiguity over unset vs default values.

// paths: ['name', 'email']

Wrappers for Nullability

Proto3 scalars cannot distinguish unset from zero. Wrapper types like Int32Value and StringValue add explicit nullability when you need it.

Evolution Tips

When evolving these constructs:

  • Add new fields to a oneof safely; do not reuse tag numbers
  • Maps: avoid changing value type
  • Well-known types are stable; prefer them over hand-rolled equivalents

Quick Check

Test your advanced protobuf knowledge.

Recap

You learned advanced protobuf constructs:

  • oneof models mutually exclusive fields
  • map stores key/value pairs (sugar over repeated entries)
  • Well-known types: Timestamp, Duration, Any, Struct
  • FieldMask enables partial updates; wrappers add nullability
  • Evolve carefully without reusing tag numbers

Często zadawane pytania

Czy lekcja „Oneof, mapy i dobrze znane typy” jest bezpłatna?

Tak — pełny tekst „Oneof, mapy i dobrze znane typy” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu gRPC & High Performance APIs, przejdź na CoddyKit PRO. Kurs gRPC & High Performance APIs zawiera 4 lekcji w sumie.

Co nauczysz się w „Oneof, mapy i dobrze znane typy”?

Modeluj elastyczne i rozwijające się dane za pomocą pól protobuf oneof, map oraz standardowych dobrze znanych typów, takich jak Timestamp, Duration i Any. Ćwiczysz gRPC & High Performance APIs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć gRPC & High Performance APIs?

Nie wymagamy żadnego doświadczenia. gRPC & High Performance APIs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Oneof, mapy i dobrze znane typy”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji gRPC & High Performance APIs?

Tak. Każda lekcja gRPC & High Performance APIs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Najlepsze praktyki Protobuf
  2. Strategie ewolucji schematów
  3. Niestandardowe opcje Protobuf
  4. Oneof, mapy i dobrze znane typy
← Powrót do gRPC & High Performance APIs