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

CastTable reports all errors #89

Merged
merged 1 commit into from
Aug 31, 2020
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module github.com/frictionlessdata/tableschema-go

go 1.14

require (
github.com/matryer/is v0.0.0-20170112134659-c0323ceb4e99
github.com/satori/go.uuid v1.1.0
Expand Down
45 changes: 38 additions & 7 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,25 @@ type uniqueKey struct {
KeyValue interface{}
}

// CastTable loads and casts all table rows.
// RowConversionError stores information about an error converting (cast or uncasting) a single row.
type RowConversionError struct {
LineNumber int
Err error
}

// ConversionError aggregates all errors that happened during a conversion operation (i.e., CastTable or
// UncastTable).
type ConversionError struct {
Errors []RowConversionError
}

// Error returns a very simple string version of all errors found during conversion.
func (ce *ConversionError) Error() string {
return fmt.Sprintf("%v", ce.Errors)
}

// CastTable loads and casts all table rows in a best effort manner.
// Line-by-line errors will be reported as *ConversionError type
//
// The result argument must necessarily be the address for a slice. The slice
// may be nil or previously allocated.
Expand All @@ -394,31 +412,44 @@ func (s *Schema) CastTable(tab table.Table, out interface{}) error {
uniqueFieldIndexes := extractUniqueFieldIndexes(s)
uniqueCache := make(map[uniqueKey]struct{})

var cv ConversionError
slicev := outv.Elem()
slicev = slicev.Slice(0, 0) // Trucantes the passed-in slice.
elemt := slicev.Type().Elem()
i := 0
rowIndex := -1
succNum := 0
for iter.Next() {
i++
rowIndex++
elemp := reflect.New(elemt)
if err := s.CastRow(iter.Row(), elemp.Interface()); err != nil {
return err
cv.Errors = append(cv.Errors, RowConversionError{rowIndex, err})
continue
}

// Check unique field and other constraints.
for _, k := range uniqueFieldIndexes {
field := elemp.Elem().Field(k)
if _, ok := uniqueCache[uniqueKey{k, field.Interface()}]; ok {
return fmt.Errorf("field(s) '%s' duplicates in row %v", elemp.Elem().Type().Field(k).Name, i)
cv.Errors = append(cv.Errors, RowConversionError{
rowIndex,
fmt.Errorf("field(s) '%s' duplicates in row %v", elemp.Elem().Type().Field(k).Name, rowIndex),
})
break
}
uniqueCache[uniqueKey{k, field.Interface()}] = struct{}{}
}
slicev = reflect.Append(slicev, elemp.Elem())
slicev = slicev.Slice(0, slicev.Len())
succNum++
}
if iter.Err() != nil {
return iter.Err()
}
outv.Elem().Set(slicev.Slice(0, i))
return nil
outv.Elem().Set(slicev.Slice(0, succNum))
if len(cv.Errors) == 0 {
return nil
}
return &cv
}

func extractUniqueFieldIndexes(s *Schema) []int {
Expand Down
22 changes: 15 additions & 7 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,10 @@ type csvRow struct {
Name string
}

type intCSVRow struct {
Value int `tableheader:"value"`
}

func TestCastTable(t *testing.T) {
data := []struct {
desc string
Expand Down Expand Up @@ -516,6 +520,16 @@ func TestCastTable(t *testing.T) {
s := &Schema{Fields: []Field{{Name: "name", Type: StringType}}}
is.True(s.CastTable(tab, []csvRow{}) != nil)
})
t.Run("Error_ManyErrors", func(t *testing.T) {
is := is.New(t)
tab := table.FromSlices([]string{"value"}, [][]string{{"boo"}, {"1"}, {"bii"}})
s := &Schema{Fields: []Field{{Name: "value", Type: IntegerType}}} // Integer won't be cast to string
var ret []intCSVRow
err := s.CastTable(tab, &ret).(*ConversionError)
is.Equal(2, len(err.Errors))
is.Equal(0, err.Errors[0].LineNumber)
is.Equal(2, err.Errors[1].LineNumber)
})
t.Run("Error_UniqueConstrain", func(t *testing.T) {
tab := table.FromSlices(
[]string{"ID", "Point"},
Expand All @@ -530,11 +544,8 @@ func TestCastTable(t *testing.T) {
if err := s.CastTable(tab, &got); err == nil {
t.Fatalf("err want:err got:nil")
}
if len(got) != 0 {
t.Fatalf("len(got) want:0 got:%v", len(got))
}
})
t.Run("Error_PrimaryKeyAndUniqueConstrain", func(t *testing.T) {
t.Run("Error_PrimaryKeyAndUniqueConstraint", func(t *testing.T) {
tab := table.FromSlices(
[]string{"ID", "Age", "Name"},
[][]string{{"1", "39", "Paul"}, {"2", "23", "Jimmy"}, {"3", "36", "Jane"}, {"4", "28", "Judy"}, {"4", "37", "John"}})
Expand All @@ -549,9 +560,6 @@ func TestCastTable(t *testing.T) {
if err := s.CastTable(tab, &got); err == nil {
t.Fatalf("err want:nil got:%q", err)
}
if len(got) != 0 {
t.Fatalf("len(got) want:0 got:%v", len(got))
}
})
}

Expand Down