Skip to content

Commit

Permalink
修复获取子数据报panic问题
Browse files Browse the repository at this point in the history
  • Loading branch information
deatil committed Jun 30, 2023
1 parent 8cb6f71 commit b1c3221
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 67 deletions.
17 changes: 17 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Auto-detect text files, ensure they use LF.
* text=auto eol=lf

# These files are always considered text and should use LF.
# See core.whitespace @ https://git-scm.com/docs/git-config for whitespace flags.
*.json text eol=lf whitespace=blank-at-eol,blank-at-eof,space-before-tab,tab-in-indent,tabwidth=4
*.test text eol=lf whitespace=blank-at-eol,blank-at-eof,space-before-tab,tab-in-indent,tabwidth=4
*.yml text eol=lf whitespace=blank-at-eol,blank-at-eof,space-before-tab,tab-in-indent,tabwidth=2

# Exclude non-essential files from dist
/.github export-ignore
/docs export-ignore
/tests export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.travis.yml export-ignore
/appveyor.yml export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ __debug_bin

# 临时文件 #
######################
go.sum
tmp/
.tmp/

Expand Down
61 changes: 15 additions & 46 deletions tree/get.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tree

// 所有父节点
func (this *Tree[K]) GetListParents(id K, sort ...string) []map[string]any {
func (this *Tree[K]) GetListParents(parentid K, sort ...string) []map[string]any {
if len(this.data) <= 0 {
return nil
}
Expand All @@ -11,29 +11,18 @@ func (this *Tree[K]) GetListParents(id K, sort ...string) []map[string]any {
order = sort[0]
}

self := this.GetListSelf(id)
if self == nil {
return nil
}

parentid := self[this.parentidKey].(K)

newData := make([]map[string]any, 0)
for _, v := range this.data {
// 不存在跳过
if _, ok := v[this.idKey]; !ok {
continue
}

dataId, ok2 := v[this.idKey].(K)
if !ok2 {
dataId, ok := v[this.idKey].(K)
if !ok {
continue
}

if dataId == parentid {
newData = append(newData, v)

parents := this.GetListParents(dataId, sort...)
parents := this.GetListParents(v[this.parentidKey].(K), sort...)
if len(parents) > 0{
if order == "asc" {
newData = append(newData, parents...)
Expand All @@ -57,12 +46,8 @@ func (this *Tree[K]) GetListParentIds(id K) []K {
ids := make([]K, 0)
for _, v := range data {
// 不存在跳过
if _, ok := v[this.idKey]; !ok {
continue
}

dataId, ok2 := v[this.idKey].(K)
if !ok2 {
dataId, ok := v[this.idKey].(K)
if !ok {
continue
}

Expand All @@ -86,19 +71,15 @@ func (this *Tree[K]) GetListChildren(id K, sort ...string) []map[string]any {
newData := make([]map[string]any, 0)
for _, v := range this.data {
// 不存在跳过
if _, ok := v[this.parentidKey]; !ok {
continue
}

dataParentId, ok2 := v[this.parentidKey].(K)
if !ok2 {
dataParentId, ok := v[this.parentidKey].(K)
if !ok {
continue
}

if dataParentId == id {
newData = append(newData, v)

children := this.GetListChildren(dataParentId, sort...)
children := this.GetListChildren(v[this.idKey].(K), sort...)
if len(children) > 0{
if order == "asc" {
newData = append(newData, children...)
Expand All @@ -123,12 +104,8 @@ func (this *Tree[K]) GetListChildIds(id K) []K {
ids := make([]K, 0)
for _, v := range data {
// 不存在跳过
if _, ok := v[this.idKey]; !ok {
continue
}

dataId, ok2 := v[this.idKey].(K)
if !ok2 {
dataId, ok := v[this.idKey].(K)
if !ok {
continue
}

Expand All @@ -147,12 +124,8 @@ func (this *Tree[K]) GetListChild(id K) []map[string]any {
newData := make([]map[string]any, 0)
for _, v := range this.data {
// 不存在跳过
if _, ok := v[this.parentidKey]; !ok {
continue
}

dataParentId, ok2 := v[this.parentidKey].(K)
if !ok2 {
dataParentId, ok := v[this.parentidKey].(K)
if !ok {
continue
}

Expand All @@ -172,12 +145,8 @@ func (this *Tree[K]) GetListSelf(id K) map[string]any {

for _, v := range this.data {
// 不存在跳过
if _, ok := v[this.idKey]; !ok {
continue
}

dataId, ok2 := v[this.idKey].(K)
if !ok2 {
dataId, ok := v[this.idKey].(K)
if !ok {
continue
}

Expand Down
21 changes: 0 additions & 21 deletions tree/helper.go

This file was deleted.

20 changes: 20 additions & 0 deletions tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,23 @@ type Tree[K Ordered] struct {
// 返回子级key
buildChildKey string
}

// 构造函数
func New[K Ordered]() *Tree[K] {
return &Tree[K]{
icon: []string{
"│",
"├",
"└",
},
blankspace: "&nbsp;",

idKey: "id",
parentidKey: "parentid",
spacerKey: "spacer",
depthKey: "depth",
haschildKey: "haschild",

buildChildKey: "children",
}
}

0 comments on commit b1c3221

Please sign in to comment.