0Pricing
gRPC & High Performance APIs · درس

‏oneof والخرائط والأنواع المعروفة جيدًا

صمّموا بيانات مرنة قابلة للتطور باستخدام حقول oneof والخرائط في protobuf، والأنواع القياسية المعروفة جيدًا مثل Timestamp وDuration وAny.

‏oneof والخرائط والأنواع المعروفة جيدًا درس مجاني في gRPC & High Performance APIs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في gRPC & High Performance APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة gRPC & High Performance APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

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

الأسئلة الشائعة

هل درس «‏oneof والخرائط والأنواع المعروفة جيدًا» مجاني؟

نعم — نص درس «‏oneof والخرائط والأنواع المعروفة جيدًا» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة gRPC & High Performance APIs، انتقل إلى CoddyKit PRO. تتضمن دورة gRPC & High Performance APIs 4 دروس في المجموع.

ماذا ستتعلم في «‏oneof والخرائط والأنواع المعروفة جيدًا»؟

صمّموا بيانات مرنة قابلة للتطور باستخدام حقول oneof والخرائط في protobuf، والأنواع القياسية المعروفة جيدًا مثل Timestamp وDuration وAny. تتمرن على gRPC & High Performance APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ gRPC & High Performance APIs؟

لا تُشترط خبرة سابقة. gRPC & High Performance APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «‏oneof والخرائط والأنواع المعروفة جيدًا»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس gRPC & High Performance APIs هذا؟

نعم. كل درس في gRPC & High Performance APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. أفضل ممارسات Protobuf
  2. استراتيجيات تطور المخططات
  3. خيارات Protobuf المخصصة
  4. ‏oneof والخرائط والأنواع المعروفة جيدًا
← العودة إلى gRPC & High Performance APIs