映射嵌套映射(Map in map)

Excel 映射中嵌套映射的规范说明。

垂直映射中的嵌套

垂直映射嵌套水平映射(Horizontal-map in vertical-map)

HelloWorld.xlsx 中的工作表 ItemConf

IDNameProp1IDProp1ValueProp2IDProp2Value
map<uint32, Item>stringmap<int32, Prop>int64int32int64
Item’s IDItem’s nameProp1’s IDProp1’s valueProp2’s IDProp2’s value
1Apple110220
2Orange330
3Banana

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    string name = 2 [(tableau.field) = {name:"Name"}];
    map<int32, Prop> prop_map = 3 [(tableau.field) = {name:"Prop" key:"ID" layout:LAYOUT_HORIZONTAL}];
    message Prop {
      int32 id = 1 [(tableau.field) = {name:"ID"}];
      int64 value = 2 [(tableau.field) = {name:"Value"}];
    }
  }
}
ItemConf.json
{
    "itemMap": {
        "1": {"id": 1, "name": "Apple", "propMap": {"1": {"id": 1, "value": "10"}, "2": {"id": 2, "value": "20"}}},
        "2": {"id": 2, "name": "Orange", "propMap": {"3": {"id": 3, "value": "30"}}},
        "3": {"id": 3, "name": "Banana", "propMap": {}}
    }
}

垂直映射嵌套水平聚合映射(Horizontal-aggregate-map in vertical-map)

[!NOTE] 当键相同的多行需要把水平展开的子映射元素跨行合并到同一个父记录时, 在水平映射字段上设置 prop:{aggregate:true}, 父映射的键字段同时设置 prop:{unique:false} 允许重复键。

子映射的键跨行重复会触发 E2005

HelloWorld.xlsx 中的工作表 ItemConf

IDNameProp1IDProp1ValueProp2IDProp2Value
map<uint32, Item>|{unique:false}stringmap<int32, Prop>|{aggregate:true}int64int32int64
Item’s IDItem’s nameProp1’s IDProp1’s valueProp2’s IDProp2’s value
1Apple110
1220
2Orange330440
3Banana550

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    uint32 id = 1 [(tableau.field) = {name:"ID" prop:{unique:false}}];
    string name = 2 [(tableau.field) = {name:"Name"}];
    map<int32, Prop> prop_map = 3 [(tableau.field) = {name:"Prop" key:"ID" layout:LAYOUT_HORIZONTAL prop:{aggregate:true}}];
    message Prop {
      int32 id = 1 [(tableau.field) = {name:"ID"}];
      int64 value = 2 [(tableau.field) = {name:"Value"}];
    }
  }
}
ItemConf.json
{
    "itemMap": {
        "1": {"id": 1, "name": "Apple", "propMap": {"1": {"id": 1, "value": "10"}, "2": {"id": 2, "value": "20"}}},
        "2": {"id": 2, "name": "Orange", "propMap": {"3": {"id": 3, "value": "30"}, "4": {"id": 4, "value": "40"}}},
        "3": {"id": 3, "name": "Banana", "propMap": {"5": {"id": 5, "value": "50"}}}
    }
}

垂直映射嵌套垂直映射(Vertical-map in vertical-map)

HelloWorld.xlsx 中的工作表 ItemConf

IDNamePropIDPropValue
map<uint32, Item>stringmap<int32, Prop>int64
Item’s IDItem’s nameProp’s IDProp’s value
1Apple110
2Orange120
2Orange230

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    string name = 2 [(tableau.field) = {name:"Name"}];
    map<int32, Prop> prop_map = 3 [(tableau.field) = {key:"PropID" layout:LAYOUT_VERTICAL}];
    message Prop {
      int32 prop_id = 1 [(tableau.field) = {name:"PropID"}];
      int64 prop_value = 2 [(tableau.field) = {name:"PropValue"}];
    }
  }
}
ItemConf.json
{
    "itemMap": {
        "1": {"id": 1, "name": "Apple", "propMap": {"1": {"propId": 1, "propValue": "10"}}},
        "2": {"id": 2, "name": "Orange", "propMap": {"1": {"propId": 1, "propValue": "20"}, "2": {"propId": 2, "propValue": "30"}}}
    }
}

垂直映射嵌套单元格内映射(Incell-map in vertical-map)

HelloWorld.xlsx 中的工作表 ItemConf

IDProps
map<uint32, Item>map<int32, string>
Item’s IDItem’s props
11:sour,2:sweet,3:delicious
21:sour,2:sweet
31:sour

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Item> item_map = 1 [(tableau.field) = {key:"ID" layout:LAYOUT_VERTICAL}];
  message Item {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    map<int32, string> props_map = 2 [(tableau.field) = {name:"Props" layout:LAYOUT_INCELL}];
  }
}
ItemConf.json
{
    "itemMap": {
        "1": {"id": 1, "propsMap": {"1": "sour", "2": "sweet", "3": "delicious"}},
        "2": {"id": 2, "propsMap": {"1": "sour", "2": "sweet"}},
        "3": {"id": 3, "propsMap": {"1": "sour"}}
    }
}

水平映射中的嵌套

水平映射嵌套水平映射(Horizontal-map in horizontal-map)

HelloWorld.xlsx 中的工作表 ItemConf

Reward1IDReward1Item1IDReward1Item1NumReward1Item2IDReward1Item2NumReward2IDReward2Item1IDReward2Item1Num
map<uint32, Reward>map<uint32, Item>int32uint32int32uint32uint32int32
Reward1 IDReward1 item1 IDReward1 item1 numReward1 item2 IDReward1 item2 numReward2 IDReward2 item1 IDReward2 item1 num
11102202330

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Reward> reward_map = 1 [(tableau.field) = {name:"Reward" key:"ID" layout:LAYOUT_HORIZONTAL}];
  message Reward {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    map<uint32, Item> item_map = 2 [(tableau.field) = {name:"Item" key:"ID" layout:LAYOUT_HORIZONTAL}];
    message Item {
      uint32 id = 1 [(tableau.field) = {name:"ID"}];
      int32 num = 2 [(tableau.field) = {name:"Num"}];
    }
  }
}
ItemConf.json
{
    "rewardMap": {
        "1": {"id": 1, "itemMap": {"1": {"id": 1, "num": 10}, "2": {"id": 2, "num": 20}}},
        "2": {"id": 2, "itemMap": {"3": {"id": 3, "num": 30}}}
    }
}

水平映射嵌套单元格内映射(Incell-map in horizontal-map)

HelloWorld.xlsx 中的工作表 ItemConf

Reward1IDReward1ItemReward2IDReward2Item
map<uint32, Reward>map<uint32, int32>uint32map<uint32, int32>
Reward1 IDReward1 itemsReward2 IDReward2 items
11:10,2:2023:30

生成结果:

hello_world.proto
// --snip--
option (tableau.workbook) = {name:"HelloWorld.xlsx" namerow:1 typerow:2 noterow:3 datarow:4};

message ItemConf {
  option (tableau.worksheet) = {name:"ItemConf"};

  map<uint32, Reward> reward_map = 1 [(tableau.field) = {name:"Reward" key:"ID" layout:LAYOUT_HORIZONTAL}];
  message Reward {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    map<uint32, int32> item_map = 2 [(tableau.field) = {name:"Item" layout:LAYOUT_INCELL}];
  }
}
ItemConf.json
{
    "rewardMap": {
        "1": {"id": 1, "itemMap": {"1": 10, "2": 20}},
        "2": {"id": 2, "itemMap": {"3": 30}}
    }
}