Skip to content

Commit 9558ed7

Browse files
authored
Merge pull request #14 from calebx/main
make sure we can not move node into slot related to its descendants
2 parents 4a88ec1 + 03dad52 commit 9558ed7

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ $ createdb nested-set-test
105105
$ go test ./...
106106
```
107107

108+
```SQL
109+
-- some useful sql to check status
110+
SELECT n.id,
111+
CONCAT(REPEAT('. . ', (COUNT(p.id) - 1)::int), n.title) AS t,
112+
n.title, n.lft, n.rgt, n.depth, n.children_count
113+
FROM categories AS n, categories AS p
114+
WHERE (n.lft BETWEEN p.lft AND p.rgt)
115+
GROUP BY n.id ORDER BY n.lft;
116+
```
117+
108118
## License
109119

110120
MIT

nested_set.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
// MoveDirection means where the node is going to be located
1515
type MoveDirection int
1616

17+
// MoveDirections ...
1718
const (
1819
// MoveDirectionLeft : MoveTo(db, a, n, MoveDirectionLeft) => a|n|...
1920
MoveDirectionLeft MoveDirection = -1
@@ -229,6 +230,11 @@ func MoveTo(db *gorm.DB, node, to interface{}, direction MoveDirection) error {
229230
return err
230231
}
231232

233+
err = moveIsValid(targetNode, toNode)
234+
if err != nil {
235+
return err
236+
}
237+
232238
var right, depthChange int
233239
var newParentID sql.NullInt64
234240
if direction == MoveDirectionLeft || direction == MoveDirectionRight {
@@ -248,6 +254,15 @@ func MoveTo(db *gorm.DB, node, to interface{}, direction MoveDirection) error {
248254
return moveToRightOfPosition(tx, targetNode, right, depthChange, newParentID)
249255
}
250256

257+
func moveIsValid(node, to nestedItem) error {
258+
validLft, validRgt := node.Lft, node.Rgt
259+
if (to.Lft >= validLft && to.Lft <= validRgt) || (to.Rgt >= validLft && to.Rgt <= validRgt) {
260+
return fmt.Errorf("in valid move target: %v => %v", node, to)
261+
}
262+
263+
return nil
264+
}
265+
251266
func moveToRightOfPosition(tx *gorm.DB, targetNode nestedItem, position, depthChange int, newParentID sql.NullInt64) error {
252267
return tx.Transaction(func(tx *gorm.DB) (err error) {
253268
oldParentID := targetNode.ParentID

nested_set_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,24 @@ func TestMoveToInner(t *testing.T) {
258258
assertNodeEqual(t, blouses, 19, 20, 2, 0, womens.ID)
259259
}
260260

261+
func TestMoveIsInvalid(t *testing.T) {
262+
initData()
263+
err := MoveTo(db, womens, dresses, MoveDirectionInner)
264+
assert.NotEmpty(t, err)
265+
reloadCategories()
266+
assertNodeEqual(t, womens, 10, 21, 1, 3, clothing.ID)
267+
268+
err = MoveTo(db, womens, dresses, MoveDirectionLeft)
269+
assert.NotEmpty(t, err)
270+
reloadCategories()
271+
assertNodeEqual(t, womens, 10, 21, 1, 3, clothing.ID)
272+
273+
err = MoveTo(db, womens, dresses, MoveDirectionRight)
274+
assert.NotEmpty(t, err)
275+
reloadCategories()
276+
assertNodeEqual(t, womens, 10, 21, 1, 3, clothing.ID)
277+
}
278+
261279
func assertNodeEqual(t *testing.T, target Category, left, right, depth, childrenCount int, parentID int64) {
262280
nullInt64ParentID := sql.NullInt64{Valid: false}
263281
if parentID > 0 {

0 commit comments

Comments
 (0)