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

Update to go 1.17 #25

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
pjdufour committed Mar 8, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 198d12a75015a39b80e937ed2f5b9a7fae307fae
12 changes: 11 additions & 1 deletion pkg/serializer/Serializer_serialize_test.go
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ func TestSerializerCSVMap(t *testing.T) {
}

func TestSerializerCSVSliceString(t *testing.T) {
in := []string{"a", "b", "c"}
in := []interface{}{[]string{"a", "b", "c"}}
s := New(FormatCSV).Limit(NoLimit)
out, err := s.Serialize(in)
assert.NoError(t, err)
@@ -81,6 +81,16 @@ func TestSerializerCSVSliceExpandHeader(t *testing.T) {
out, err := s.Serialize(in)
assert.NoError(t, err)
assert.Equal(t, "a,b,c\n1,,3\n,5,6\n", string(out))
//a,b,c
//1,,3
//,5,6
//
//a,b,c
//1,,3
//,5,6
//a,c
//1,3
//,6
}

func TestSerializerCSVSliceExpandHeaderWithWildcard(t *testing.T) {
1 change: 1 addition & 0 deletions pkg/sv/Write.go
Original file line number Diff line number Diff line change
@@ -169,6 +169,7 @@ func Write(input *WriteInput) error {
// if streaming and not expanding header.
p := pipe.NewBuilder().OutputLimit(input.Limit)
if inputObjectKind == reflect.Array || inputObjectKind == reflect.Slice {
fmt.Println("Input Object:", input.Header, inputObject)
it, errorIterator := pipe.NewSliceIterator(inputObject)
if errorIterator != nil {
return fmt.Errorf("error creating slice iterator: %w", errorIterator)
58 changes: 33 additions & 25 deletions pkg/sv/Writer.go
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ func NewWriter(underlying io.Writer, separator rune, columns []interface{}, keyS
}

func (w *Writer) WriteHeader() error {
fmt.Println("WriteHeader():", w.columns)
w.headerWritten = true

// Stringify columns into strings
@@ -81,35 +82,42 @@ func (w *Writer) ToRow(obj interface{}) ([]string, error) {
}

func (w *Writer) WriteObject(obj interface{}) error {
if !w.headerWritten {
if len(w.columns) == 0 {
inputObjectValue := reflect.ValueOf(obj)
for reflect.TypeOf(inputObjectValue.Interface()).Kind() == reflect.Ptr {
inputObjectValue = inputObjectValue.Elem()
}
inputObjectValue = reflect.ValueOf(inputObjectValue.Interface()) // sets value to concerete type
inputObjectKind := inputObjectValue.Type().Kind()
if inputObjectKind == reflect.Map {
w.columns = inspector.GetKeysFromValue(inputObjectValue, w.sorted, w.reversed)
} else if inputObjectKind == reflect.Struct {
fieldNames := make([]interface{}, 0)
for _, fieldName := range inspector.GetFieldNamesFromValue(inputObjectValue, w.sorted, w.reversed) {
fieldNames = append(fieldNames, fieldName)
row := make([]string, 0)
if slc, ok := obj.([]string); ok {
row = slc
} else {
if !w.headerWritten {
if len(w.columns) == 0 {
inputObjectValue := reflect.ValueOf(obj)
for reflect.TypeOf(inputObjectValue.Interface()).Kind() == reflect.Ptr {
inputObjectValue = inputObjectValue.Elem()
}
inputObjectValue = reflect.ValueOf(inputObjectValue.Interface()) // sets value to concerete type
inputObjectKind := inputObjectValue.Type().Kind()
if inputObjectKind == reflect.Map {
w.columns = inspector.GetKeysFromValue(inputObjectValue, w.sorted, w.reversed)
} else if inputObjectKind == reflect.Struct {
fieldNames := make([]interface{}, 0)
for _, fieldName := range inspector.GetFieldNamesFromValue(inputObjectValue, w.sorted, w.reversed) {
fieldNames = append(fieldNames, fieldName)
}
fmt.Println("Field Names:", fieldNames)
w.columns = fieldNames
}
w.columns = fieldNames
}
if len(w.columns) == 0 {
return fmt.Errorf("could not infer the header from the given value %#v with type %T", obj, obj)
}
err := w.WriteHeader()
if err != nil {
return fmt.Errorf("error writing header: %w", err)
}
}
if len(w.columns) == 0 {
return fmt.Errorf("could not infer the header from the given value with type %T", obj)
}
err := w.WriteHeader()
if err != nil {
return fmt.Errorf("error writing header: %w", err)
r, errorRow := w.ToRow(obj)
if errorRow != nil {
return fmt.Errorf("error serializing object as row: %w", errorRow)
}
}
row, errorRow := w.ToRow(obj)
if errorRow != nil {
return fmt.Errorf("error serializing object as row: %w", errorRow)
row = r
}
errorWrite := w.writer.Write(row)
if errorWrite != nil {