Skip to content

Commit

Permalink
Merge pull request #5 from smartrecruiters/master
Browse files Browse the repository at this point in the history
Updated GTStructure to use pointers to items instead of value receivers
  • Loading branch information
disiqueira authored Mar 26, 2018
2 parents 4c287e3 + 3979068 commit 50d8646
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
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)
}
}

0 comments on commit 50d8646

Please sign in to comment.