File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ package nestedset
2+
3+ import "encoding/json"
4+
5+ type (
6+ // NodeCollection ::
7+ NodeCollection []* Node
8+ )
9+
10+ // Marshal ::
11+ func (nodes NodeCollection ) Marshal () ([]byte , error ) {
12+ return json .Marshal (nodes )
13+ }
14+
15+ // GenerateTree ::
16+ func (nodes NodeCollection ) GenerateTree (childStruct interface {}) NodeCollection {
17+ query := NodeCollection {}
18+ for _ , n := range nodes {
19+ if n .Depth == 0 {
20+ query = append (query , n )
21+ }
22+ }
23+ return nodes .runQueryRecursive (query , childStruct )
24+ }
25+
26+ func (nodes NodeCollection ) runQueryRecursive (query NodeCollection , v interface {}) (result NodeCollection ) {
27+ for _ , q := range query {
28+ var vTemp interface {}
29+ if q .JSONData != "" {
30+ vTemp = v
31+ b := []byte (q .JSONData )
32+ json .Unmarshal (b , & vTemp )
33+ }
34+ result = append (result , & Node {
35+ ID : q .ID ,
36+ Name : q .Name ,
37+ Data : vTemp ,
38+ Children : nodes .getChildrens (q , v ),
39+ })
40+ }
41+ return result
42+ }
43+
44+ func (nodes NodeCollection ) getChildrens (parent * Node , v interface {}) NodeCollection {
45+ query := NodeCollection {}
46+ for _ , n := range nodes {
47+ if n .Depth == parent .Depth + 1 && n .Left > parent .Left && n .Right < parent .Right {
48+ query = append (query , n )
49+ }
50+ }
51+ return nodes .runQueryRecursive (query , v )
52+ }
You can’t perform that action at this time.
0 commit comments