Skip to content

Commit

Permalink
Merge pull request #9 from calebx/main
Browse files Browse the repository at this point in the history
parseNode could parse ptr too
  • Loading branch information
huacnlee authored Dec 24, 2020
2 parents e1fdf70 + 1664a43 commit 0e4c01f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion nested_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func parseNode(db *gorm.DB, source interface{}) (tx *gorm.DB, item nodeItem, err
}

item = nodeItem{TableName: stmt.Table, DbNames: map[string]string{}}
sourceType := reflect.TypeOf(source)
sourceValue := reflect.Indirect(reflect.ValueOf(source))
sourceType := sourceValue.Type()
for i := 0; i < sourceType.NumField(); i++ {
t := sourceType.Field(i)
v := sourceValue.Field(i)
Expand Down
13 changes: 13 additions & 0 deletions nested_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ func TestNewNodeItem(t *testing.T) {
stmt.Build(clause.Where{}.Name())
assert.Equal(t, "WHERE user_id = $1 AND user_type = $2", stmt.SQL.String())

tx, node, err = parseNode(db, &source)
assert.NoError(t, err)
assert.Equal(t, source.ID, node.ID)
assert.Equal(t, source.ParentID, node.ParentID)
assert.Equal(t, source.Depth, node.Depth)
assert.Equal(t, source.Lft, node.Lft)
assert.Equal(t, source.Rgt, node.Rgt)
assert.Equal(t, source.ChildrenCount, node.ChildrenCount)
assert.Equal(t, "categories", node.TableName)
stmt = tx.Statement
stmt.Build(clause.Where{}.Name())
assert.Equal(t, "WHERE user_id = $1 AND user_type = $2", stmt.SQL.String())

dbNames := node.DbNames
assert.Equal(t, "id", dbNames["id"])
assert.Equal(t, "parent_id", dbNames["parent_id"])
Expand Down

0 comments on commit 0e4c01f

Please sign in to comment.