diff --git a/outputs/text.go b/outputs/text.go index 56a0c8e..c12f692 100644 --- a/outputs/text.go +++ b/outputs/text.go @@ -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) } diff --git a/utils/utils.go b/utils/utils.go index a0105e9..afb9f47 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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 ", " } diff --git a/utils/utils_test.go b/utils/utils_test.go index 2e65663..73c4dfe 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -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{ @@ -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) +}