Protobuf Basic – 2

Pada modul ini kita akan membahas dasar-dasar protobuf yang lebih advanced.

Multiple Message dalam satu .proto file.

Mari kita langsung menggunakan contoh dengan membuat message Date dan menambahkannya pada message Person (dari modul sebelumnya) sebagai birthday.

/* Comment goes here
 * Comment goes here */

syntax = "proto3";

// comment goes here
message Person {
    int32 age = 1;  // comment goes here
    string first_name = 2;
    string last_name = 3;
    bytes small_pict = 4;
    bool is_verified = 5;
    float height = 6;

    repeated string phone_numbers = 7;

    enum EyeColor {
        UNKNOWN_EYE_COLOR = 0;
        EYE_BLACK = 1;
        EYE_GREY = 2;
        EYE_BLUE = 3;
    }

    EyeColor eye_color = 8;

    Date birthday = 9;

}

message Date {
    int32 year = 1;
    int32 month = 2;
    int32 day = 3;
}

Nesting Message

Dalam protobuf kita dalam mendefinisikan types dalam types atau nesting dan tidak ada batasan jumlah nesting.

Berikut beberapa pertimbangan melakukan nesting message:

  • Untuk menghindari naming conflicts.
  • Menunjukan scoping untuk type yang dimaksud.

Untuk lebih jelas, kita akan tambahkan field Address yang akan digunakan dalam message Person untuk memililki multiple address.

/* Comment goes here
 * Comment goes here */

syntax = "proto3";

// comment goes here
message Person {
    int32 age = 1;  // comment goes here
    string first_name = 2;
    string last_name = 3;
    bytes small_pict = 4;
    bool is_verified = 5;
    float height = 6;

    repeated string phone_numbers = 7;

    enum EyeColor {
        UNKNOWN_EYE_COLOR = 0;
        EYE_BLACK = 1;
        EYE_GREY = 2;
        EYE_BLUE = 3;
    }

    EyeColor eye_color = 8;

    Date birthday = 9;

    message Address {
        string addr_line_1 = 1;
        string addr_line_2 = 2;
        string zipcode = 3;
        string city = 4;
        string country = 5;
    }

    repeated Address addresses = 10;
}

message Date {
    int32 year = 1;
    int32 month = 2;
    int32 day = 3;
}

Import Types

Adalah fitur untuk menggunakan types dari file .proto berbeda. Syntax dasar import adalah seperti berikut:

import "nama_file.proto";

Sebagai contoh, kita akan pindahkan message Date ke file proto berbeda, lalu kita import dari message Person.

file person.proto

/* Comment goes here
 * Comment goes here */

syntax = "proto3";

import "date.proto";

// comment goes here
message Person {
    int32 age = 1;  // comment goes here
    string first_name = 2;
    string last_name = 3;
    bytes small_pict = 4;
    bool is_verified = 5;
    float height = 6;

    repeated string phone_numbers = 7;

    enum EyeColor {
        UNKNOWN_EYE_COLOR = 0;
        EYE_BLACK = 1;
        EYE_GREY = 2;
        EYE_BLUE = 3;
    }

    EyeColor eye_color = 8;

    Date birthday = 9;

    message Address {
        string addr_line_1 = 1;
        string addr_line_2 = 2;
        string zipcode = 3;
        string city = 4;
        string country = 5;
    }

    repeated Address addresses = 10;
}

file date.proto

syntax = "proto3";

message Date {
    int32 year = 1;
    int32 month = 2;
    int32 day = 3;
}

Package

Package specifier digunakan untuk mencegah name clashes antara protocol message types.

Sebagai contoh, kita akan gunakan message Date. Gunakan specifier package dengan nama my.date;

file date.proto

syntax = "proto3";

package my.date;

message Date {
    int32 year = 1;
    int32 month = 2;
    int32 day = 3;
}

file person.proto

syntax = "proto3";

import "date.proto";

package person;

message Person {
    int32 age = 1;
    string first_name = 2;
    string last_name = 3;
    bytes small_pict = 4;
    bool is_verified = 5;
    float height = 6;

    repeated string phone_numbers = 7;

    enum EyeColor {
        UNKNOWN_EYE_COLOR = 0;
        EYE_BLACK = 1;
        EYE_GREY = 2;
        EYE_BLUE = 3;
    }

    EyeColor eye_color = 8;

    //message date diakses berdasarkan package name
    my.date.Date birthday = 9;

    message Address {
        string addr_line_1 = 1;
        string addr_line_2 = 2;
        string zipcode = 3;
        string city = 4;
        string country = 5;
    }

    repeated Address addresses = 10;
}

Sampai disini kita sudah mempelajari dasar-dasar dari protobuf. Pada modul berikutnya kita akan install protoc compiler.

Sharing is caring:

Leave a Comment