From e915e8029909e2ef3ff910cd7891dfb1add397a7 Mon Sep 17 00:00:00 2001 From: Kybxd <627940450@qq.com> Date: Tue, 26 Aug 2025 11:40:35 +0800 Subject: [PATCH] fix: unittest and duplicate check --- internal/index/descriptor.go | 4 +- internal/index/descriptor_test.go | 87 ++++++++++++++++++++++++++++--- 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/internal/index/descriptor.go b/internal/index/descriptor.go index 44d1d81a..f6fb4eaf 100644 --- a/internal/index/descriptor.go +++ b/internal/index/descriptor.go @@ -66,6 +66,7 @@ type LevelIndex struct { MD protoreflect.MessageDescriptor ColFields []*LevelField SortedColFields []*LevelField + NameConflict *Index } func (l *LevelIndex) Name() string { @@ -190,7 +191,8 @@ func ParseIndexDescriptor(md protoreflect.MessageDescriptor) *IndexDescriptor { // check duplicate index name indexNameMap := map[string]*Index{} for levelMessage := descriptor.LevelMessage; levelMessage != nil; levelMessage = levelMessage.NextLevel { - for _, index := range levelMessage.Indexes { + allIndexes := append(levelMessage.Indexes, levelMessage.OrderedIndexes...) + for _, index := range allIndexes { name := index.Name() if existingIndex, ok := indexNameMap[name]; ok { panic(fmt.Sprintf("duplicate index name on %v in %v: %v and %v", md.Name(), md.ParentFile().Path(), index, existingIndex)) diff --git a/internal/index/descriptor_test.go b/internal/index/descriptor_test.go index 8b74d987..d07423bf 100644 --- a/internal/index/descriptor_test.go +++ b/internal/index/descriptor_test.go @@ -54,9 +54,9 @@ func Test_ParseIndexDescriptor(t *testing.T) { }, { Index: &Index{ - Cols: []string{"Param"}, + Cols: []string{"Param"}, SortedCols: []string{"ID"}, - Name: "ItemInfo", + Name: "ItemInfo", }, MD: md[*protoconf.ItemConf_Item](), ColFields: []*LevelField{ @@ -108,9 +108,9 @@ func Test_ParseIndexDescriptor(t *testing.T) { }, { Index: &Index{ - Cols: []string{"ID", "Name"}, + Cols: []string{"ID", "Name"}, SortedCols: []string{"Type", "UseEffectType"}, - Name: "AwardItem", + Name: "AwardItem", }, MD: md[*protoconf.ItemConf_Item](), ColFields: []*LevelField{ @@ -326,9 +326,9 @@ func Test_ParseIndexDescriptor(t *testing.T) { }, { Index: &Index{ - Cols: []string{"ChapterName"}, + Cols: []string{"ChapterName"}, SortedCols: []string{"AwardID"}, - Name: "NamedChapter", + Name: "NamedChapter", }, MD: md[*protoconf.ActivityConf_Activity_Chapter](), ColFields: []*LevelField{ @@ -390,9 +390,9 @@ func Test_ParseIndexDescriptor(t *testing.T) { Indexes: []*LevelIndex{ { Index: &Index{ - Cols: []string{"ActivityID"}, + Cols: []string{"ActivityID"}, SortedCols: []string{"Goal", "ID"}, - Name: "", + Name: "", }, MD: md[*protoconf.TaskConf_Task](), ColFields: []*LevelField{ @@ -419,6 +419,77 @@ func Test_ParseIndexDescriptor(t *testing.T) { }, }, }, + OrderedIndexes: []*LevelIndex{ + { + Index: &Index{ + Cols: []string{"Goal"}, + SortedCols: []string{"ID"}, + Name: "OrderedTask", + }, + MD: md[*protoconf.TaskConf_Task](), + ColFields: []*LevelField{ + { + FD: fd[*protoconf.TaskConf_Task]("goal"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("goal"), + }, + }, + }, + SortedColFields: []*LevelField{ + { + FD: fd[*protoconf.TaskConf_Task]("id"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("id"), + }, + }, + }, + }, + { + Index: &Index{ + Cols: []string{"Expiry"}, + Name: "TaskExpiry", + }, + MD: md[*protoconf.TaskConf_Task](), + ColFields: []*LevelField{ + { + FD: fd[*protoconf.TaskConf_Task]("expiry"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("expiry"), + }, + }, + }, + }, + { + Index: &Index{ + Cols: []string{"Expiry"}, + SortedCols: []string{"Goal", "ID"}, + Name: "SortedTaskExpiry", + }, + MD: md[*protoconf.TaskConf_Task](), + ColFields: []*LevelField{ + { + FD: fd[*protoconf.TaskConf_Task]("expiry"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("expiry"), + }, + }, + }, + SortedColFields: []*LevelField{ + { + FD: fd[*protoconf.TaskConf_Task]("goal"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("goal"), + }, + }, + { + FD: fd[*protoconf.TaskConf_Task]("id"), + LeveledFDList: []protoreflect.FieldDescriptor{ + fd[*protoconf.TaskConf_Task]("id"), + }, + }, + }, + }, + }, }, }, },