如何在 Protocol Buffers 中构建数据模型

Posted

技术标签:

【中文标题】如何在 Protocol Buffers 中构建数据模型【英文标题】:How to build a data model in Protocol Buffers 【发布时间】:2021-09-14 06:10:26 【问题描述】:

例如,我需要这样的模型:

[
  
    "type": "input",
    "identity": "protocol_buffers_input",
    "value": "Input Value",
    "rules": 
      "required": true,
      "max_length": 100
    ,
    "properties": 
      "label": "Input Controller",
      "readonly": false,
      "default_value": "Input Default Value",
      "width": "100%",
      "placeholder": "Input Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "select",
    "identity": "protocol_buffers_select",
    "value": ["key1", "key2"],
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Select Controller",
      "readonly": false,
      "default_value": ["key1", "key2"],
      "clear_able": true,
      "filterable": false,
      "width": "100%",
      "options": [
         "label": "label1", "value": "key1" ,
         "label": "label2", "value": "key2" ,
         "label": "label3", "value": "key3" 
      ],
      "multiple": true,
      "placeholder": "Select Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "switch",
    "identity": "protocol_buffers_switch",
    "value": true,
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Switch Controller",
      "readonly": false,
      "default_value": true
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "image",
    "identity": "protocol_buffers_image",
    "value": "",
    "rules": 
      "required": true
    ,
    "properties": 
      "label": "Image Controller",
      "readonly": false,
      "tip": "Image Controller Tips"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  ,
  
    "type": "number",
    "identity": "protocol_buffers_number",
    "value": 1,
    "rules": 
      "min_value": 0,
      "max_value": 100,
      "required": true
    ,
    "properties": 
      "label": "Number Controller",
      "readonly": false,
      "default_value": 1,
      "width": "100%",
      "step": 1,
      "placeholder": "Number Placeholder"
    ,
    "business": 
      "client_visible": true,
      "disabled": false
    ,
    "disabled": true
  
]

虽然input、select、switch、image、number对象的级别相同,但是对象的属性(值、规则、属性)还是有区别的。

如何在没有接口和继承机制的情况下建立相应的数据模型?将每个对象完全分开?

【问题讨论】:

【参考方案1】:
文件结构
proto                   
├─ form                 
│  ├─ business.proto    
│  ├─ input.proto       
│  ├─ select.proto      
│  ├─ image.proto       
│  ├─ number.proto      
│  ├─ ...               
│  └─ switch.proto      
└─ items                
   └─ index.proto       
基本数据模型
// form/business.proto
syntax = "proto3";

package form;

message Business 
  bool client_visible = 1;
  bool disabled = 2;

// form/input.proto
syntax = "proto3";

package form;

import "form/business.proto";

message InputRules 
  bool required = 1;
  uint32 max_length = 2;


message InputProperties 
  string label = 1;
  bool readonly = 2;
  string default_value = 3;
  string width = 4;
  string placeholder = 5;


message Input 
  string type = 1;
  string identity = 2;
  string value = 3;
  InputRules rules = 4;
  InputProperties properties = 5;
  Business business = 6;
  bool disabled = 7;

// form/image.proto
syntax = "proto3";

package form;

import "form/business.proto";

message ImageRules 
  bool required = 1;


message ImageProperties 
  string label = 1;
  bool readonly = 2;
  string tip = 3;


message Image 
  string type = 1;
  string identity = 2;
  string value = 3;
  ImageRules rules = 4;
  ImageProperties properties = 5;
  Business business = 6;
  bool disabled = 7;

实体模型
// items/index.proto
syntax = "proto3";

package items;

import "form/input.proto";
import "form/textarea.proto";
import "form/radio.proto";
import "form/checkbox.proto";
import "form/select.proto";
import "form/date.proto";
import "form/switch.proto";
import "form/image.proto";
import "form/number.proto";

message Nested 
  uint32 id = 1;

  oneof items 
    form.Input input = 2;
    form.Textarea textarea = 3;
    form.Radio radio = 4;
    form.Checkbox checkbox = 5;
    form.Select select = 6;
    form.Date date = 7;
    form.Switch switch = 8;
    form.Image image = 9;
    form.Number number = 10;
  


message SearchRequest 
  string query = 1;
  int32 page_number = 2;
  int32 per_page = 3;


message SearchResponse 
  repeated Nested items = 1;


service SearchService 
  rpc Search(SearchRequest) returns (SearchResponse);

【讨论】:

以上是关于如何在 Protocol Buffers 中构建数据模型的主要内容,如果未能解决你的问题,请参考以下文章

如何在 PHP 中处理 Protocol Buffers 数据

如何在PHP中处理Protocol Buffers数据

如何从 Protocol Buffers .NET 代码生成 .proto 文件?

我如何知道在使用 Protocol Buffers 库时收到了啥消息?

Protocol buffers--python 实践 protocol buffers vs json

protocol buffers 使用方法