Skip to content

union: support enum, scalar, and predefined/reused/custom-name struct for union fields#301

Merged
wenchy merged 4 commits intomasterfrom
extend-union-field
Sep 20, 2025
Merged

union: support enum, scalar, and predefined/reused/custom-name struct for union fields#301
wenchy merged 4 commits intomasterfrom
extend-union-field

Conversation

@Kybxd
Copy link
Copy Markdown
Collaborator

@Kybxd Kybxd commented Sep 9, 2025

Union Definition

Name Alias Type Field1 Field2 #Note
Fruit Fruit enum<.FruitType> Bound to predefined enum
Point Point int32 Bound to scalar
Item Item .Item Bound to global predefined struct
Player Player ID
uint32
Name
string
Bound to local defined struct
Friend Friend Player Bound to local predefined in the same level
Monster Monster CustomMonster Health
uint32
Attack
int32
Bound to local defined struct with custom type name
Boss Boss CustomMonster Bound to local predefined struct in the same level

Generate Proto

message UnionTypedTarget {
  option (tableau.union) = {name:"UnionTypedTarget"};

  Type type = 9999 [(tableau.field) = {name:"Type"}];
  oneof value {
    option (tableau.oneof) = {field:"Field"};

    protoconf.FruitType fruit = 1; // Bound to enum value: TYPE_FRUIT.
    int32 point = 2; // Bound to enum value: TYPE_POINT.
    protoconf.Item item = 3; // Bound to enum value: TYPE_ITEM.
    Player player = 4; // Bound to enum value: TYPE_PLAYER.
    Player friend = 5; // Bound to enum value: TYPE_FRIEND.
    CustomMonster monster = 6; // Bound to enum value: TYPE_MONSTER.
    CustomMonster boss = 7; // Bound to enum value: TYPE_BOSS.
  }

  enum Type {
    TYPE_INVALID = 0;
    TYPE_FRUIT = 1 [(tableau.evalue).name = "Fruit"]; // Fruit
    TYPE_POINT = 2 [(tableau.evalue).name = "Point"]; // Point
    TYPE_ITEM = 3 [(tableau.evalue).name = "Item"]; // Item
    TYPE_PLAYER = 4 [(tableau.evalue).name = "Player"]; // Player
    TYPE_FRIEND = 5 [(tableau.evalue).name = "Friend"]; // Friend
    TYPE_MONSTER = 6 [(tableau.evalue).name = "Monster"]; // Monster
    TYPE_BOSS = 7 [(tableau.evalue).name = "Boss"]; // Boss
  }

  message Player {
    uint32 id = 1 [(tableau.field) = {name:"ID"}];
    string name = 2 [(tableau.field) = {name:"Name"}];
  }
  message CustomMonster {
    int32 health = 1 [(tableau.field) = {name:"Health"}];
    int32 attack = 2 [(tableau.field) = {name:"Attack"}];
  }
}

Usage

Type Field1 Field2
[.UnionTypedTarget]enum<.UnionTypedTarget.Type> union union
Target type Target field1 Target field2
Fruit Orange
Point 10
Item 1001 10
Player 1 Tom
Friend 2 Jerry
Monster 2000 100
Boss 20000 200

Generate Conf

{
    "unionTypedTargetList": [
        {
            "type": "TYPE_FRUIT",
            "fruit": "FRUIT_TYPE_ORANGE"
        },
        {
            "type": "TYPE_POINT",
            "point": 10
        },
        {
            "type": "TYPE_ITEM",
            "item": {
                "id": 1001,
                "num": 10
            }
        },
        {
            "type": "TYPE_PLAYER",
            "player": {
                "id": 1,
                "name": "Tom"
            }
        },
        {
            "type": "TYPE_FRIEND",
            "friend": {
                "id": 2,
                "name": "Jerry"
            }
        },
        {
            "type": "TYPE_MONSTER",
            "monster": {
                "health": 2000,
                "attack": 100
            }
        },
        {
            "type": "TYPE_BOSS",
            "boss": {
                "health": 20000,
                "attack": 200
            }
        }
    ]
}

@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 9, 2025

Codecov Report

❌ Patch coverage is 76.08696% with 22 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.27%. Comparing base (c89e1f0) to head (9bad475).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
internal/excel/excel.go 65.62% 7 Missing and 4 partials ⚠️
internal/confgen/table_parser.go 76.47% 5 Missing and 3 partials ⚠️
internal/protogen/sheet_mode.go 57.14% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #301   +/-   ##
=======================================
  Coverage   71.27%   71.27%           
=======================================
  Files          84       84           
  Lines       10371    10392   +21     
=======================================
+ Hits         7392     7407   +15     
- Misses       2409     2413    +4     
- Partials      570      572    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Kybxd Kybxd changed the title feat: support enum, scalar, and predefined/reused/custom-name struct for union fields union: support enum, scalar, and predefined/reused/custom-name struct for union fields Sep 9, 2025
Comment thread internal/x/xproto/union.go
Comment thread internal/excel/excel.go
Comment thread test/functest/proto/default/excel__union__union.proto
Comment thread proto/tableau/protobuf/internal/metabook.proto Outdated
@wenchy wenchy merged commit 3b9f75f into master Sep 20, 2025
9 checks passed
@wenchy wenchy deleted the extend-union-field branch September 20, 2025 08:36
@wenchy wenchy mentioned this pull request Aug 25, 2025
42 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

union(protogen): reuse same field type for different fields

2 participants