membuat modeling data inventory komputer
- Mendefinisikan Skema
- Multifile .proto
- Field : Field rule, Field Types, Field Number
- Advance field type : Enum, Nested
brew install protobuf
go install google.golang.org/protobuf/cmd/protoc-gen-go
//syntax compile
protoc -I=<source folder proto> --go_out=<source destination > --go_opt=paths=source_relative file.proto
//Example
cd /proto
protoc -I=. --go_out=../pb --go_opt=paths=source_relative todo.proto
syntax = "proto3";
package pb;
option go_package = "github.com/protocol-buffer/pb";
// FORMAT
message <Name> {
/**
* field tag harus unik dan merupakan integer
* field tag yang dapat digunakan : 1 - 2^29-1
* field tag yang tidak dapat digunakan : 19000 - 19999
*/
<Field-rule> <Field-Types> <name> = <field_tag>;
...
}
//Example
message CPU {
string brand = 1;
string name = 2;
unit32 number_cores = 5;
uint32 numbers_threads = 5;
double min_ghz = 5;
double max_ghz = 6;
}
message ... {
....
}
-
/proto
cpu.proto
memory.protoimport "memory.proto"; import "..."; import "..."; message ... { Memory memory = 1; }
-
-
- Enum Type :FIELD NUMBER HARUS DIMULAI DARI 0
message ... { enum <EnumName> { DEFAULT = 0; .... } EnumName fieldName = 1; } //example message Memory { enum Unit { UNKNOW = 0; BIT = 1; BYTE = 2; KILOBYTE = 3; MEGABYTE = 4; GIGABYTE = 5; TERABYTE = 6; } Unit unit = 1; }
- Composite
message M1 { ... } message M2 { M2 composite = 1; }
- Date
import "google/protobuf/timestamp.proto"; message ... { ... google.protobuf.Timestamp updated_at = 1; }
- Enum Type :FIELD NUMBER HARUS DIMULAI DARI 0
-
- Repeated: Array
message M1 { } message M2 { repeated M2 composite = 1; }
- oneof : pilihan
message Weight { oneof name { double weight_kg = 1; double weight_lb = 2; } }
-