Skip to content

Commit

Permalink
fix issues with normal text output
Browse files Browse the repository at this point in the history
  • Loading branch information
bjesus committed Sep 27, 2024
1 parent 8f639ee commit 5239b9b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 27 deletions.
7 changes: 2 additions & 5 deletions outputs/text.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package outputs

import (
"strings"

"github.com/bjesus/pipet/common"
"github.com/bjesus/pipet/utils"
)

func OutputText(app *common.PipetApp) string {
flattenedData := utils.FlattenData(app.Data, 0)
return (strings.Join(flattenedData, utils.GetSeparator(app, 0)))

cleanData := utils.RemoveUnnecessaryNesting(app.Data)
return utils.FlattenNestedSlices(app, cleanData, 0)
}
47 changes: 33 additions & 14 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,51 @@ package utils
import (
"fmt"
"os"
"reflect"
"strconv"
"strings"

"github.com/bjesus/pipet/common"
)

func FlattenData(data interface{}, depth int) []string {
func FlattenNestedSlices(app *common.PipetApp, data interface{}, level int) string {
v := reflect.ValueOf(data)

if v.Kind() != reflect.Slice {
return fmt.Sprint(data)
}

var result []string

switch v := data.(type) {
case []interface{}:
for _, item := range v {
result = append(result, FlattenData(item, depth+1)...)
}
case map[string]interface{}:
for _, value := range v {
result = append(result, FlattenData(value, depth+1)...)
}
default:
result = append(result, fmt.Sprintf("%v", v))
for i := 0; i < v.Len(); i++ {
elem := v.Index(i).Interface()
flattened := FlattenNestedSlices(app, elem, level+1)
result = append(result, flattened)
}

return result
sep := GetSeparator(app, level)
return strings.Join(result, sep)
}

func RemoveUnnecessaryNesting(data interface{}) interface{} {
for {
val := reflect.ValueOf(data)
if val.Kind() == reflect.Slice && val.Len() == 1 {
firstElem := val.Index(0).Interface()
if reflect.ValueOf(firstElem).Kind() == reflect.Slice {
data = firstElem
continue
}
}
break
}
return data
}

func GetSeparator(app *common.PipetApp, depth int) string {
if depth < len(app.Separator) {
return app.Separator[depth]
sep, _ := strconv.Unquote(`"` + app.Separator[depth] + `"`)
return sep
}
return ", "
}
Expand Down
15 changes: 7 additions & 8 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,6 @@ import (
"github.com/stretchr/testify/assert" // Or use default `testing` if no external dependency needed
)

// Test FlattenData
func TestFlattenData(t *testing.T) {
data := []interface{}{"a", "b", []interface{}{"c", "d"}}
expected := []string{"a", "b", "c", "d"}
result := FlattenData(data, 0)
assert.Equal(t, expected, result)
}

// Test GetSeparator
func TestGetSeparator(t *testing.T) {
app := &common.PipetApp{
Expand All @@ -32,3 +24,10 @@ func TestFileExists(t *testing.T) {
assert.True(t, FileExists(f.Name()))
assert.False(t, FileExists("nonexistent.file"))
}

func TestRemoveUnnecessaryNesting(t *testing.T) {
input := [][][]interface{}{{{"foo", "bar"}}}
expected := []interface{}{"foo", "bar"}
result := RemoveUnnecessaryNesting(input)
assert.Equal(t, result, expected)
}

0 comments on commit 5239b9b

Please sign in to comment.