Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated GTStructure to use pointers to items instead of value receivers #5

Merged
merged 2 commits into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ var music gotree.GTStructure
music.Name = "5 Minutes Alone"

//Add Music to the Album
album.Items = append(album.Items, music)
album.Items = append(album.Items, &music)

//Add Album to the Artist
artist.Items = append(artist.Items, album)
artist.Items = append(artist.Items, &album)

gotree.PrintTree(artist)
gotree.PrintTree(&artist)
```

### Read folder and print tree
Expand Down
21 changes: 9 additions & 12 deletions gotree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
/*GTStructure Structure to output print */
type GTStructure struct {
Name string
Items []GTStructure
Items []*GTStructure
}

func StringTree(object GTStructure) (result string) {
func StringTree(object *GTStructure) (result string) {
result += object.Name + "\n"
var spaces []bool
result += stringObjItems(object.Items, spaces)
Expand All @@ -37,7 +37,7 @@ func stringLine(name string, spaces []bool, last bool) (result string) {
return
}

func stringObjItems(items []GTStructure, spaces []bool) (result string) {
func stringObjItems(items []*GTStructure, spaces []bool) (result string) {
for i, f := range items {
last := (i >= len(items)-1)
result += stringLine(f.Name, spaces, last)
Expand All @@ -50,29 +50,26 @@ func stringObjItems(items []GTStructure, spaces []bool) (result string) {
}

/*PrintTree - Print the tree in console */
func PrintTree(object GTStructure) {
func PrintTree(object *GTStructure) {
fmt.Println(StringTree(object))
}

/*ReadFolder - Read a folder and return the generated object */
func ReadFolder(directory string) GTStructure {

var parent GTStructure
func ReadFolder(directory string) *GTStructure {
parent := &GTStructure{}

parent.Name = directory
parent.Items = createGTReadFolder(directory)

return parent
}

func createGTReadFolder(directory string) []GTStructure {

var items []GTStructure
func createGTReadFolder(directory string) []*GTStructure {
var items []*GTStructure
files, _ := ioutil.ReadDir(directory)

for _, f := range files {

var child GTStructure
child := &GTStructure{}
child.Name = f.Name()

if f.IsDir() {
Expand Down
39 changes: 39 additions & 0 deletions gotree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gotree

import (
"testing"
)

// TestUpdatingItemsStructure should test whenever item updates in the tree structure are
// reflected correctly in the rendered structure
func TestUpdatingItemsStructure(t *testing.T) {
expected := "Pantera\n" +
"└── Far Beyond Driven\n" +
" └── 5 minutes Alone\n"

var artist GTStructure
artist.Name = "Pantera Typo0"

var album GTStructure
album.Name = "Far Beyond Driven Typo1"

var music GTStructure
music.Name = "5 Minutes Alone Typo2"

// Add Music to the Album
album.Items = append(album.Items, &music)

// Add Album to the Artist
artist.Items = append(artist.Items, &album)

// apply updates to the items that are already in the tree structure
music.Name = "5 minutes Alone"
album.Name = "Far Beyond Driven"
artist.Name = "Pantera"

actual := StringTree(&artist)

if actual != expected {
t.Fatalf("Actual tree::\n[%s]\nis not the same as expected:\n[%s]", actual, expected)
}
}