protocol-buffers - 如何在 Protocol Buffers 中构建数据模型
问题描述
例如,我需要这样的模型:
[
{
"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对象的级别相同,但是对象的属性(值、规则、属性)有区别。
没有接口和继承机制,如何建立相应的数据模型?将每个对象完全分开?
解决方案
文件结构
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);
}
推荐阅读
- reverse-proxy - 具有静态内容(尤其是 kubevious UI)的网站的 Istio 配置
- grails - 在 grails 3 中注入数据源会忽略 application.groovy 中设置的其他属性
- angular - JWT 令牌未定义,但它存在于 http 响应中
- sql - Sparksql 针对 3 个表选择某些记录
- javascript - addCustomFilter() 不适用于 BPF,用于在 Microsoft Dynamics 365 CRM 中使用链接实体筛选器进行系统用户查找
- c# - 无法在使用 C# 的 influxdb 中使用 JWT 进行身份验证
- python - 如何在 GitPython 中使用 git pull?
- ruby-on-rails - 将嵌套关联渲染为 json
- openstack - 乌苏里版 Openstack 中的 cpu_util 指标
- mysql - 为什么 express (node.js) 需要参数来删除请求?