简体   繁体   中英

How to build a data model in Protocol Buffers

For example, I need a model like this:

[
  {
    "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
  }
]

Although the input, select, switch, image, and number objects have the same level, But there are differences in the property of the objects (value, rules, and properties).

How to establish a corresponding data model without interfaces and inheritance mechanisms? Separate each object completely?

File structure
proto                   
├─ form                 
│  ├─ business.proto    
│  ├─ input.proto       
│  ├─ select.proto      
│  ├─ image.proto       
│  ├─ number.proto      
│  ├─ ...               
│  └─ switch.proto      
└─ items                
   └─ index.proto       
Basic data model
// 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;
}
Entity model
// 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM