-
-
Save jagregory/7460725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parser | |
import "database/sql" | |
type Row struct { | |
MenuId int | |
Menu sql.NullString | |
Submenu sql.NullString | |
Item sql.NullString | |
Parent int | |
Child int | |
Position int | |
ChildType string | |
MenuRoot bool | |
ItemRoot bool | |
} | |
type Menu struct { | |
Id int | |
AssociatedRow Row | |
Parent MenuNode | |
children []MenuNode | |
} | |
type MenuItem struct { | |
Parent MenuNode | |
Id int | |
AssociatedRow Row | |
Name string | |
} | |
func (menu Menu) Children() []MenuNode { | |
return menu.children | |
} | |
func (menuItem MenuItem) Children() []MenuNode { | |
return []MenuNode{} | |
} | |
type MenuNode interface { | |
Children() []MenuNode | |
} | |
func Parse(rows []Row) []*Menu { | |
rootMenus := make([]*Menu, 0, len(rows)) | |
menus := make(map[int]*Menu) | |
for _, row := range rows { | |
if row.ChildType == "Menu" { | |
m := &Menu{Id: row.Child} | |
menus[m.Id] = m | |
if row.MenuRoot { | |
rootMenus = append(rootMenus, m) | |
} else { | |
parent := menus[row.Parent] | |
if parent != nil { | |
parent.children = append(parent.children, m) | |
} | |
} | |
} else if row.ChildType == "MenuItem" { | |
parent := menus[row.Parent] | |
if parent != nil { | |
parent.children = append(parent.children, &MenuItem{Id: row.Child}) | |
} | |
} | |
} | |
return rootMenus | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package parser | |
import ( | |
"database/sql" | |
"testing" | |
) | |
func TestCanParseRootMenus(t *testing.T) { | |
testItemName := sql.NullString{String: "Test"} | |
rows := []Row{ | |
Row{ | |
Child: 1, | |
Parent: 34, | |
MenuRoot: true, | |
ChildType: "Menu", | |
}, | |
Row{ | |
Child: 2, | |
Parent: 1, | |
MenuRoot: false, | |
ChildType: "Menu", | |
}, | |
Row{ | |
ItemRoot: false, | |
Parent: 2, | |
ChildType: "MenuItem", | |
Item: testItemName, | |
Child: 3, | |
}, | |
} | |
results := Parse(rows) | |
menu := results[0] | |
sub_menu := menu.Children()[0] | |
menu_item := sub_menu.Children()[0].(*MenuItem) | |
if menu_item.Name == "Test" { | |
t.Error("Menu structure doesn't contain expected menu item") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ha not gonna work the parent can be declared after a child :\ It's not actually a file it's recursive sql query result :D