|
| 1 | +package nestedset |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/satori/go.uuid" |
| 7 | +) |
| 8 | + |
| 9 | +type ( |
| 10 | + // Node represents a node of nested set tree |
| 11 | + Node struct { |
| 12 | + _tableName string `sql:"-"` |
| 13 | + ID uuid.UUID `sql:"type:varbinary(36);NOT NULL" gorm:"primary_key;column:id" json:"-"` // |
| 14 | + Left int32 `sql:"type:int;NOT NULL" gorm:"column:lft" json:"-"` // |
| 15 | + Right int32 `sql:"type:int;NOT NULL" gorm:"column:rgt" json:"-"` // |
| 16 | + Name string `sql:"type:varchar(50);NOT NULL" gorm:"column:name" json:"name,omitempty"` // |
| 17 | + Type int32 `sql:"type:int;NOT NULL" gorm:"column:type" json:"type,omitempty"` // |
| 18 | + JSONData string `sql:"type:JSON;default:NULL" gorm:"column:data" json:"-"` // |
| 19 | + Data interface{} `sql:"-" json:"data,omitempty"` // |
| 20 | + Depth int32 `sql:"-" json:"depth,omitempty"` // |
| 21 | + Children NodeCollection `sql:"-" json:"children,omitempty"` // |
| 22 | + |
| 23 | + } |
| 24 | + // NodeCollection :: |
| 25 | + NodeCollection []*Node |
| 26 | +) |
| 27 | + |
| 28 | +// NeWNode returns pointer to newly initialized Node |
| 29 | +func NeWNode(left, right int32, name string) *Node { |
| 30 | + n := &Node{ |
| 31 | + ID: uuid.Must(uuid.NewV4()), |
| 32 | + Left: left, |
| 33 | + Right: right, |
| 34 | + Name: name, |
| 35 | + } |
| 36 | + return n |
| 37 | +} |
| 38 | + |
| 39 | +// TableName :: |
| 40 | +func (n *Node) TableName() string { |
| 41 | + if n._tableName != "" { |
| 42 | + return n._tableName |
| 43 | + } |
| 44 | + return "nodes" |
| 45 | +} |
| 46 | + |
| 47 | +// SetTableName :: |
| 48 | +func (n *Node) SetTableName(name string) { |
| 49 | + n._tableName = name |
| 50 | +} |
| 51 | + |
| 52 | +// GenerateTree :: |
| 53 | +func (nodes NodeCollection) GenerateTree(childStruct interface{}) NodeCollection { |
| 54 | + query := NodeCollection{} |
| 55 | + for _, n := range nodes { |
| 56 | + if n.Depth == 0 { |
| 57 | + query = append(query, n) |
| 58 | + } |
| 59 | + } |
| 60 | + return nodes.runQueryRecursive(query, childStruct) |
| 61 | +} |
| 62 | + |
| 63 | +func (nodes NodeCollection) runQueryRecursive(query NodeCollection, v interface{}) (result NodeCollection) { |
| 64 | + for _, q := range query { |
| 65 | + var vTemp interface{} |
| 66 | + if q.JSONData != "" { |
| 67 | + vTemp = v |
| 68 | + b := []byte(q.JSONData) |
| 69 | + json.Unmarshal(b, &vTemp) |
| 70 | + } |
| 71 | + result = append(result, &Node{ |
| 72 | + ID: q.ID, |
| 73 | + Name: q.Name, |
| 74 | + Data: vTemp, |
| 75 | + Children: nodes.getChildrens(q, v), |
| 76 | + }) |
| 77 | + } |
| 78 | + return result |
| 79 | +} |
| 80 | + |
| 81 | +func (nodes NodeCollection) getChildrens(parent *Node, v interface{}) NodeCollection { |
| 82 | + query := NodeCollection{} |
| 83 | + for _, n := range nodes { |
| 84 | + if n.Depth == parent.Depth+1 && n.Left > parent.Left && n.Right < parent.Right { |
| 85 | + query = append(query, n) |
| 86 | + } |
| 87 | + } |
| 88 | + return nodes.runQueryRecursive(query, v) |
| 89 | +} |
0 commit comments