type PathEvaluationRsp_Module struct {
ModuleId int32 `protobuf:"varint,1,opt,name=moduleId,proto3" json:"moduleId"`
ModuleName string `protobuf:"bytes,2,opt,name=moduleName,proto3" json:"moduleName"`
Children []*PathEvaluationRsp_Module `protobuf:"bytes,5,rep,name=children,proto3" json:"children"`
}
//做单元测试使用 func TestPath(pathMap []string) []*pb.PathEvaluationRsp_Module { var model *pb.PathEvaluationRsp_Module var models []*pb.PathEvaluationRsp_Module //var modeuleId = 1 for _, keys := range pathMap { array := strings.Split(keys, `\`) model = buildPathModule(array, 0, model) //判断当前节点是否已经存在存在的话用最新的替代之前的 if !replaceModel(model, models) { models = append(models, model) } } return models } //判断当前节点是否已经存在存在的话不添加不存在的话添加到上级节点的子节点 func replaceModel(model *pb.PathEvaluationRsp_Module, models []*pb.PathEvaluationRsp_Module) bool { for i := 0; i < len(models); i++ { if models[i].ModuleName == model.ModuleName { //跳出递归循环 if model.Children == nil { return true } if !replaceModel(model.Children[0], models[i].Children) { model.Children = append(model.Children, models[i].Children...) //这里应该使用递归判断 models[i] = model } return true } } return false } //第一个参数为path 的array index 为当前的 层级 为index-1 models 为当前的所有的结构 rsp childModel // 构建子集model 数组 func buildPathModule(array []string, index int, models *pb.PathEvaluationRsp_Module) *pb.PathEvaluationRsp_Module { //var childModel []*pb.PathEvaluationRsp_Module if index > len(array)-1 { return nil } if models == nil { model := &pb.PathEvaluationRsp_Module{ ModuleId: int32(index), ModuleName: array[index], } child := buildPathModule(array, index+1, models) if child != nil { model.Children = append(model.Children, child) } //childModel = append(childModel, model) return model } //判断一下当前层级的节点是否已经添加 modelExist := checkExisting(index, 0, array[index], []*pb.PathEvaluationRsp_Module{models}) if modelExist == nil { model := &pb.PathEvaluationRsp_Module{ ModuleId: int32(index), ModuleName: array[index], } child := buildPathModule(array, index+1, models) if child != nil { model.Children = append(model.Children, child) } //childModel = append(childModel, model) return model } else { child := buildPathModule(array, index+1, models) if child != nil { //判断当前节点的child是否含有当前节点 if !checkChildExisting(modelExist, child) { modelExist.Children = append(modelExist.Children, child) } } return modelExist } } //判断当前节点的child是否含有当前节点 func checkChildExisting(modele, child *pb.PathEvaluationRsp_Module) bool { for i := 0; i < len(modele.Children); i++ { if modele.Children[i].ModuleName == child.ModuleName { return true } } return false } //index 为层级 //判断一下当前层级的节点是否已经添加 func checkExisting(index, seed int, pathName string, models []*pb.PathEvaluationRsp_Module) *pb.PathEvaluationRsp_Module { for i := 0; i < len(models); i++ { if seed != index { //不是一层就去下面一层找 modelExisting := checkExisting(index, seed+1, pathName, models[i].Children) if modelExisting != nil { return modelExisting } } else { if pathName == models[i].ModuleName { return models[i] } } } return nil }
//单元测试 func TestGetAnswerMap(t *testing.T) { mmodels := TestPath([]string{`AB\CD\D`, `AB\CD`, `AE\C`, `AD\DE\F`, `AD\DE\G`,`AB\CD\E\F`}) fmt.Println(mmodels) }
原文:https://www.cnblogs.com/chongyao/p/15031754.html