Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions cmd/protoc-gen-cpp-tableau-loader/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func genHppIndexFinders(g *protogen.GeneratedFile, descriptor *index.IndexDescri
g.P(helper.Indent(1), "using ", mapType, " = std::unordered_map<", keyType, ", ", vectorType, ">;")
g.P(helper.Indent(1), "// Finds the index (", index.Index, ") to value (", vectorType, ") hash map.")
g.P(helper.Indent(1), "// One key may correspond to multiple values, which are contained by a vector.")
g.P(helper.Indent(1), "const ", mapType, "& Find", index.Name(), "() const;")
g.P(helper.Indent(1), "const ", mapType, "& Find", index.Name(), "Map() const;")
g.P(helper.Indent(1), "// Finds a vector of all values of the given key.")
g.P(helper.Indent(1), "const ", vectorType, "* Find", index.Name(), "(", helper.ToConstRefType(keyType), " ", helper.ParseIndexFieldNameAsFuncParam(field.FD), ") const;")
g.P(helper.Indent(1), "// Finds the first value of the given key.")
Expand All @@ -47,9 +47,15 @@ func genHppIndexFinders(g *protogen.GeneratedFile, descriptor *index.IndexDescri

// generate key struct
g.P(helper.Indent(1), "struct ", keyType, " {")
var keys []helper.MapKey
equality := ""
for i, field := range index.ColFields {
g.P(helper.Indent(2), helper.ParseCppType(field.FD), " ", helper.ParseIndexFieldNameAsKeyStructFieldName(field.FD), ";")
typ := helper.ParseCppType(field.FD)
keys = append(keys, helper.MapKey{
Type: typ,
Name: helper.ParseIndexFieldNameAsFuncParam(field.FD),
})
g.P(helper.Indent(2), typ, " ", helper.ParseIndexFieldNameAsKeyStructFieldName(field.FD), ";")
equality += helper.ParseIndexFieldNameAsKeyStructFieldName(field.FD) + " == other." + helper.ParseIndexFieldNameAsKeyStructFieldName(field.FD)
if i != len(index.ColFields)-1 {
equality += " && "
Expand Down Expand Up @@ -79,11 +85,11 @@ func genHppIndexFinders(g *protogen.GeneratedFile, descriptor *index.IndexDescri
g.P(helper.Indent(1), "using ", mapType, " = std::unordered_map<", keyType, ", ", vectorType, ", ", keyHasherType, ">;")
g.P(helper.Indent(1), "// Finds the index (", index.Index, ") to value (", vectorType, ") hash map.")
g.P(helper.Indent(1), "// One key may correspond to multiple values, which are contained by a vector.")
g.P(helper.Indent(1), "const ", mapType, "& Find", index.Name(), "() const;")
g.P(helper.Indent(1), "// Finds a vector of all values of the given key.")
g.P(helper.Indent(1), "const ", vectorType, "* Find", index.Name(), "(const ", keyType, "& key) const;")
g.P(helper.Indent(1), "// Finds the first value of the given key.")
g.P(helper.Indent(1), "const ", helper.ParseCppClassType(index.MD), "* FindFirst", index.Name(), "(const ", keyType, "& key) const;")
g.P(helper.Indent(1), "const ", mapType, "& Find", index.Name(), "Map() const;")
g.P(helper.Indent(1), "// Finds a vector of all values of the given keys.")
g.P(helper.Indent(1), "const ", vectorType, "* Find", index.Name(), "(", helper.GenGetParams(keys), ") const;")
g.P(helper.Indent(1), "// Finds the first value of the given keys.")
g.P(helper.Indent(1), "const ", helper.ParseCppClassType(index.MD), "* FindFirst", index.Name(), "(", helper.GenGetParams(keys), ") const;")
g.P()

g.P(" private:")
Expand Down Expand Up @@ -248,32 +254,32 @@ func genCppIndexFinders(g *protogen.GeneratedFile, descriptor *index.IndexDescri
indexContainerName := "index_" + strcase.ToSnake(index.Name()) + "_map_"

g.P("// Index: ", index.Index)
g.P("const ", messagerName, "::", mapType, "& ", messagerName, "::Find", index.Name(), "() const { return ", indexContainerName, " ;}")
g.P("const ", messagerName, "::", mapType, "& ", messagerName, "::Find", index.Name(), "Map() const { return ", indexContainerName, " ;}")
g.P()

var keyType, keyName string
var keys []helper.MapKey
for _, field := range index.ColFields {
keys = append(keys, helper.MapKey{
Type: helper.ParseCppType(field.FD),
Name: helper.ParseIndexFieldNameAsFuncParam(field.FD),
})
}

g.P("const ", messagerName, "::", vectorType, "* ", messagerName, "::Find", index.Name(), "(", helper.GenGetParams(keys), ") const {")
if len(index.ColFields) == 1 {
// single-column index
field := index.ColFields[0] // just take first field
keyType = helper.ParseCppType(field.FD)
keyName = helper.ParseIndexFieldNameAsFuncParam(field.FD)
g.P(helper.Indent(1), "auto iter = ", indexContainerName, ".find(", helper.GenGetArguments(keys), ");")
} else {
// multi-column index
keyType = fmt.Sprintf("const Index_%sKey&", index.Name())
keyName = "key"
g.P(helper.Indent(1), "auto iter = ", indexContainerName, ".find({", helper.GenGetArguments(keys), "});")
}

g.P("const ", messagerName, "::", vectorType, "* ", messagerName, "::Find", index.Name(), "(", helper.ToConstRefType(keyType), " ", keyName, ") const {")
g.P(helper.Indent(1), "auto iter = ", indexContainerName, ".find(", keyName, ");")
g.P(helper.Indent(1), "if (iter == ", indexContainerName, ".end()) {")
g.P(helper.Indent(2), "return nullptr;")
g.P(helper.Indent(1), "}")
g.P(helper.Indent(1), "return &iter->second;")
g.P("}")
g.P()

g.P("const ", helper.ParseCppClassType(index.MD), "* ", messagerName, "::FindFirst", index.Name(), "(", helper.ToConstRefType(keyType), " ", keyName, ") const {")
g.P(helper.Indent(1), "auto conf = Find", index.Name(), "(", keyName, ");")
g.P("const ", helper.ParseCppClassType(index.MD), "* ", messagerName, "::FindFirst", index.Name(), "(", helper.GenGetParams(keys), ") const {")
g.P(helper.Indent(1), "auto conf = Find", index.Name(), "(", helper.GenGetArguments(keys), ");")
g.P(helper.Indent(1), "if (conf == nullptr || conf->empty()) {")
g.P(helper.Indent(2), "return nullptr;")
g.P(helper.Indent(1), "}")
Expand Down
6 changes: 3 additions & 3 deletions cmd/protoc-gen-go-tableau-loader/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ParseIndexFieldNameAsFuncParam(gen *protogen.Plugin, fd protoreflect.FieldD

// ParseGoType converts a FieldDescriptor to its Go type.
// returns string if fd is scalar type, and protogen.GoIdent if fd is enum or message type.
func ParseGoType(gen *protogen.Plugin, fd protoreflect.FieldDescriptor) any {
func ParseGoType(gen *protogen.Plugin, g *protogen.GeneratedFile, fd protoreflect.FieldDescriptor) string {
switch fd.Kind() {
case protoreflect.BoolKind:
return "bool"
Expand All @@ -63,9 +63,9 @@ func ParseGoType(gen *protogen.Plugin, fd protoreflect.FieldDescriptor) any {
case protoreflect.BytesKind:
return "[]byte"
case protoreflect.EnumKind:
return FindEnumGoIdent(gen, fd.Enum())
return g.QualifiedGoIdent(FindEnumGoIdent(gen, fd.Enum()))
case protoreflect.MessageKind:
return FindMessageGoIdent(gen, fd.Message())
return g.QualifiedGoIdent(FindMessageGoIdent(gen, fd.Message()))
// case protoreflect.GroupKind:
// return "group"
default:
Expand Down
33 changes: 16 additions & 17 deletions cmd/protoc-gen-go-tableau-loader/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func genIndexTypeDef(gen *protogen.Plugin, g *protogen.GeneratedFile, descriptor
field := index.ColFields[0] // just take first field
g.P("// Index: ", index.Index)
mapType := fmt.Sprintf("%s_Index_%sMap", messagerName, index.Name())
keyType := helper.ParseGoType(gen, field.FD)
keyType := helper.ParseGoType(gen, g, field.FD)
g.P("type ", mapType, " = map[", keyType, "][]*", helper.FindMessageGoIdent(gen, index.MD))
} else {
// multi-column index
Expand All @@ -30,7 +30,7 @@ func genIndexTypeDef(gen *protogen.Plugin, g *protogen.GeneratedFile, descriptor
// KeyType must be comparable, refer https://go.dev/blog/maps
g.P("type ", keyType, " struct {")
for _, field := range index.ColFields {
g.P(helper.ParseIndexFieldNameAsKeyStructFieldName(gen, field.FD), " ", helper.ParseGoType(gen, field.FD))
g.P(helper.ParseIndexFieldNameAsKeyStructFieldName(gen, field.FD), " ", helper.ParseGoType(gen, g, field.FD))
}
g.P("}")
g.P("type ", mapType, " = map[", keyType, "][]*", helper.FindMessageGoIdent(gen, index.MD))
Expand Down Expand Up @@ -198,29 +198,28 @@ func genIndexFinders(gen *protogen.Plugin, g *protogen.GeneratedFile, descriptor
g.P("}")
g.P()

var keyType any
var keyName string
if len(index.ColFields) == 1 {
// single-column index
field := index.ColFields[0] // just take first field
keyType = helper.ParseGoType(gen, field.FD)
keyName = helper.ParseIndexFieldNameAsFuncParam(gen, field.FD)
} else {
// multi-column index
keyType = fmt.Sprintf("%s_Index_%sKey", messagerName, index.Name())
keyName = "key"
var keys []helper.MapKey
for _, field := range index.ColFields {
keys = append(keys, helper.MapKey{
Type: helper.ParseGoType(gen, g, field.FD),
Name: helper.ParseIndexFieldNameAsFuncParam(gen, field.FD),
})
}

g.P("// Find", index.Name(), " finds a slice of all values of the given key.")
g.P("func (x *", messagerName, ") Find", index.Name(), "(", keyName, " ", keyType, ") []*", helper.FindMessageGoIdent(gen, index.MD), " {")
g.P("return x.", indexContainerName, "[", keyName, "]")
g.P("func (x *", messagerName, ") Find", index.Name(), "(", helper.GenGetParams(keys), ") []*", helper.FindMessageGoIdent(gen, index.MD), " {")
if len(index.ColFields) == 1 {
g.P("return x.", indexContainerName, "[", helper.GenGetArguments(keys), "]")
} else {
g.P("return x.", indexContainerName, "[", fmt.Sprintf("%s_Index_%sKey", messagerName, index.Name()), "{", helper.GenGetArguments(keys), "}]")
}
g.P("}")
g.P()

g.P("// FindFirst", index.Name(), " finds the first value of the given key,")
g.P("// or nil if no value found.")
g.P("func (x *", messagerName, ") FindFirst", index.Name(), "(", keyName, " ", keyType, ") *", helper.FindMessageGoIdent(gen, index.MD), " {")
g.P("val := x.", indexContainerName, "[", keyName, "]")
g.P("func (x *", messagerName, ") FindFirst", index.Name(), "(", helper.GenGetParams(keys), ") *", helper.FindMessageGoIdent(gen, index.MD), " {")
g.P("val := x.Find", index.Name(), "(", helper.GenGetArguments(keys), ")")
g.P("if len(val) > 0 {")
g.P("return val[0]")
g.P("}")
Expand Down
13 changes: 3 additions & 10 deletions cmd/protoc-gen-go-tableau-loader/messager.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,16 +264,9 @@ func getNextLevelMapFD(fd protoreflect.FieldDescriptor) protoreflect.FieldDescri
}

func parseMapValueType(gen *protogen.Plugin, g *protogen.GeneratedFile, fd protoreflect.FieldDescriptor) string {
valueType := helper.ParseGoType(gen, fd.MapValue())
var valueTypeStr string
switch valueType := valueType.(type) {
case string:
valueTypeStr = valueType
case protogen.GoIdent:
valueTypeStr = g.QualifiedGoIdent(valueType)
}
valueType := helper.ParseGoType(gen, g, fd.MapValue())
if fd.MapValue().Kind() == protoreflect.MessageKind {
return "*" + valueTypeStr
return "*" + valueType
}
return valueTypeStr
return valueType
}
3 changes: 1 addition & 2 deletions test/cpp-tableau-loader/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ int main() {
// std::cout << "item1: " << item_mgr->Data().DebugString() << std::endl;

ATOM_DEBUG("-----Index: multi-column index test");
tableau::ItemConf::Index_AwardItemKey key{1, "apple"};
auto item = item_mgr->FindFirstAwardItem(key);
auto item = item_mgr->FindFirstAwardItem(1, "apple");
if (!item) {
ATOM_ERROR("ItemConf FindFirstAwardItem failed!");
return 1;
Expand Down
36 changes: 18 additions & 18 deletions test/cpp-tableau-loader/src/protoconf/item_conf.pc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const ItemConf::Item_OrderedMap* ItemConf::GetOrderedMap() const {
}

// Index: Type
const ItemConf::Index_ItemMap& ItemConf::FindItem() const { return index_item_map_ ;}
const ItemConf::Index_ItemMap& ItemConf::FindItemMap() const { return index_item_map_ ;}

const ItemConf::Index_ItemVector* ItemConf::FindItem(protoconf::FruitType type) const {
auto iter = index_item_map_.find(type);
Expand All @@ -143,7 +143,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItem(protoconf::FruitType ty
}

// Index: Param<ID>@ItemInfo
const ItemConf::Index_ItemInfoMap& ItemConf::FindItemInfo() const { return index_item_info_map_ ;}
const ItemConf::Index_ItemInfoMap& ItemConf::FindItemInfoMap() const { return index_item_info_map_ ;}

const ItemConf::Index_ItemInfoVector* ItemConf::FindItemInfo(int32_t param) const {
auto iter = index_item_info_map_.find(param);
Expand All @@ -162,7 +162,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemInfo(int32_t param) cons
}

// Index: Default@ItemDefaultInfo
const ItemConf::Index_ItemDefaultInfoMap& ItemConf::FindItemDefaultInfo() const { return index_item_default_info_map_ ;}
const ItemConf::Index_ItemDefaultInfoMap& ItemConf::FindItemDefaultInfoMap() const { return index_item_default_info_map_ ;}

const ItemConf::Index_ItemDefaultInfoVector* ItemConf::FindItemDefaultInfo(const std::string& default_) const {
auto iter = index_item_default_info_map_.find(default_);
Expand All @@ -181,7 +181,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemDefaultInfo(const std::s
}

// Index: ExtType@ItemExtInfo
const ItemConf::Index_ItemExtInfoMap& ItemConf::FindItemExtInfo() const { return index_item_ext_info_map_ ;}
const ItemConf::Index_ItemExtInfoMap& ItemConf::FindItemExtInfoMap() const { return index_item_ext_info_map_ ;}

const ItemConf::Index_ItemExtInfoVector* ItemConf::FindItemExtInfo(protoconf::FruitType ext_type) const {
auto iter = index_item_ext_info_map_.find(ext_type);
Expand All @@ -200,45 +200,45 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemExtInfo(protoconf::Fruit
}

// Index: (ID,Name)<Type,UseEffectType>@AwardItem
const ItemConf::Index_AwardItemMap& ItemConf::FindAwardItem() const { return index_award_item_map_ ;}
const ItemConf::Index_AwardItemMap& ItemConf::FindAwardItemMap() const { return index_award_item_map_ ;}

const ItemConf::Index_AwardItemVector* ItemConf::FindAwardItem(const Index_AwardItemKey& key) const {
auto iter = index_award_item_map_.find(key);
const ItemConf::Index_AwardItemVector* ItemConf::FindAwardItem(uint32_t id, const std::string& name) const {
auto iter = index_award_item_map_.find({id, name});
if (iter == index_award_item_map_.end()) {
return nullptr;
}
return &iter->second;
}

const protoconf::ItemConf::Item* ItemConf::FindFirstAwardItem(const Index_AwardItemKey& key) const {
auto conf = FindAwardItem(key);
const protoconf::ItemConf::Item* ItemConf::FindFirstAwardItem(uint32_t id, const std::string& name) const {
auto conf = FindAwardItem(id, name);
if (conf == nullptr || conf->empty()) {
return nullptr;
}
return conf->front();
}

// Index: (ID,Type,Param,ExtType)@SpecialItem
const ItemConf::Index_SpecialItemMap& ItemConf::FindSpecialItem() const { return index_special_item_map_ ;}
const ItemConf::Index_SpecialItemMap& ItemConf::FindSpecialItemMap() const { return index_special_item_map_ ;}

const ItemConf::Index_SpecialItemVector* ItemConf::FindSpecialItem(const Index_SpecialItemKey& key) const {
auto iter = index_special_item_map_.find(key);
const ItemConf::Index_SpecialItemVector* ItemConf::FindSpecialItem(uint32_t id, protoconf::FruitType type, int32_t param, protoconf::FruitType ext_type) const {
auto iter = index_special_item_map_.find({id, type, param, ext_type});
if (iter == index_special_item_map_.end()) {
return nullptr;
}
return &iter->second;
}

const protoconf::ItemConf::Item* ItemConf::FindFirstSpecialItem(const Index_SpecialItemKey& key) const {
auto conf = FindSpecialItem(key);
const protoconf::ItemConf::Item* ItemConf::FindFirstSpecialItem(uint32_t id, protoconf::FruitType type, int32_t param, protoconf::FruitType ext_type) const {
auto conf = FindSpecialItem(id, type, param, ext_type);
if (conf == nullptr || conf->empty()) {
return nullptr;
}
return conf->front();
}

// Index: PathDir@ItemPathDir
const ItemConf::Index_ItemPathDirMap& ItemConf::FindItemPathDir() const { return index_item_path_dir_map_ ;}
const ItemConf::Index_ItemPathDirMap& ItemConf::FindItemPathDirMap() const { return index_item_path_dir_map_ ;}

const ItemConf::Index_ItemPathDirVector* ItemConf::FindItemPathDir(const std::string& dir) const {
auto iter = index_item_path_dir_map_.find(dir);
Expand All @@ -257,7 +257,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemPathDir(const std::strin
}

// Index: PathName@ItemPathName
const ItemConf::Index_ItemPathNameMap& ItemConf::FindItemPathName() const { return index_item_path_name_map_ ;}
const ItemConf::Index_ItemPathNameMap& ItemConf::FindItemPathNameMap() const { return index_item_path_name_map_ ;}

const ItemConf::Index_ItemPathNameVector* ItemConf::FindItemPathName(const std::string& name) const {
auto iter = index_item_path_name_map_.find(name);
Expand All @@ -276,7 +276,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemPathName(const std::stri
}

// Index: PathFriendID@ItemPathFriendID
const ItemConf::Index_ItemPathFriendIDMap& ItemConf::FindItemPathFriendID() const { return index_item_path_friend_id_map_ ;}
const ItemConf::Index_ItemPathFriendIDMap& ItemConf::FindItemPathFriendIDMap() const { return index_item_path_friend_id_map_ ;}

const ItemConf::Index_ItemPathFriendIDVector* ItemConf::FindItemPathFriendID(uint32_t id) const {
auto iter = index_item_path_friend_id_map_.find(id);
Expand All @@ -295,7 +295,7 @@ const protoconf::ItemConf::Item* ItemConf::FindFirstItemPathFriendID(uint32_t id
}

// Index: UseEffectType@UseEffectType
const ItemConf::Index_UseEffectTypeMap& ItemConf::FindUseEffectType() const { return index_use_effect_type_map_ ;}
const ItemConf::Index_UseEffectTypeMap& ItemConf::FindUseEffectTypeMap() const { return index_use_effect_type_map_ ;}

const ItemConf::Index_UseEffectTypeVector* ItemConf::FindUseEffectType(protoconf::UseEffect::Type type) const {
auto iter = index_use_effect_type_map_.find(type);
Expand Down
Loading