Skip to content

Commit

Permalink
Renaming Decode and Encode to Cast and Uncast, respectivelly. Fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfireman committed Nov 13, 2017
1 parent 664a044 commit b2ccefb
Show file tree
Hide file tree
Showing 28 changed files with 256 additions and 224 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ sch, _ := schema.LoadRemote("http://myfoobar/users/schema.json")

## Processing Tabular Data

Once you have the data, you would like to process using language data types. [schema.Encode](https://godoc.org/github.com/frictionlessdata/tableschema-go/schema#example-Schema-Encode) and [schema.EncodeTable](https://godoc.org/github.com/frictionlessdata/tableschema-go/schema#example-Schema-EncodeTable) are your friends on this journey.
Once you have the data, you would like to process using language data types. [schema.Uncast](https://godoc.org/github.com/frictionlessdata/tableschema-go/schema#example-Schema-Uncast) and [schema.UncastTable](https://godoc.org/github.com/frictionlessdata/tableschema-go/schema#example-Schema-UncastTable) are your friends on this journey.

```go
package main
Expand All @@ -112,8 +112,8 @@ func main() {
tab, _ := csv.NewTable(csv.FromFile("users.csv"), csv.LoadHeaders())
sch, _ := schema.Infer(tab)
var users []user
sch.DecodeTable(tab, &users)
// Users slice contains the table contents properly encoded into
sch.CastTable(tab, &users)
// Users slice contains the table contents properly raw into
// language types. Each row will be a new user appended to the slice.
}
```
Expand All @@ -125,8 +125,8 @@ If you have a lot of data and can no load everything in memory, you can easily i
iter, _ := sch.Iter()
for iter.Next() {
var u user
sch.Decode(iter.Row(), &u)
// Variable u is now filled with row contents properly encoded
sch.CastRow(iter.Row(), &u)
// Variable u is now filled with row contents properly raw
// to language types.
}
...
Expand All @@ -141,7 +141,7 @@ If you have a lot of data and can no load everything in memory, you can easily i

Class represents field in the schema.

For example, data values can be decoded to native Go types. Decoding a value will check if the value is of the expected type, is in the correct format, and complies with any constraints imposed by a schema.
For example, data values can be castd to native Go types. Decoding a value will check if the value is of the expected type, is in the correct format, and complies with any constraints imposed by a schema.

```javascript
{
Expand All @@ -155,15 +155,15 @@ For example, data values can be decoded to native Go types. Decoding a value wil
}
```

The following example will raise exception the passed-in is less than allowed by `minimum` constraints of the field. `Errors` will be returned as well when the user tries to decode values which are not well formatted dates.
The following example will raise exception the passed-in is less than allowed by `minimum` constraints of the field. `Errors` will be returned as well when the user tries to cast values which are not well formatted dates.

```go
date, err := field.Decode("2014-05-29")
date, err := field.Cast("2014-05-29")
// uh oh, something went wrong
```

Values that can't be decoded will return an `error`.
Decodeing a value that doesn't meet the constraints will return an `error`.
Values that can't be castd will return an `error`.
Casting a value that doesn't meet the constraints will return an `error`.

Available types, formats and resultant value of the cast:

Expand Down Expand Up @@ -215,7 +215,7 @@ func WriteSummary(summary []summaryEntry, path string) {

w.Write([]string{"Date", "AverageAge"})
for _, summ := range summary{
row, _ := sch.Encode(summ)
row, _ := sch.Uncast(summ)
w.Write(row)
}
}
Expand Down
15 changes: 14 additions & 1 deletion csv/table.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package csv

import (
"bytes"
"encoding/csv"
"fmt"
"io"
Expand Down Expand Up @@ -64,6 +65,18 @@ func (table *Table) Headers() []string {
return table.headers
}

// String returns a string version of the table.
func (table *Table) String() string {
var buf bytes.Buffer
w := csv.NewWriter(&buf)
rows, err := table.ReadAll()
if err != nil {
return ""
}
w.WriteAll(rows)
return buf.String()
}

func newIterator(source io.ReadCloser, skipHeaders bool) *csvIterator {
return &csvIterator{
source: source,
Expand Down Expand Up @@ -198,7 +211,7 @@ func errorOpts(headers ...string) CreationOpts {
}
}

// NewWriter creates a writer which appends records to a CSV encoded file.
// NewWriter creates a writer which appends records to a CSV raw file.
//
// As returned by NewWriter, a csv.Writer writes records terminated by a
// newline and uses ',' as the field delimiter. The exported fields can be
Expand Down
13 changes: 11 additions & 2 deletions examples/infer/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/frictionlessdata/tableschema-go/csv"
"github.com/frictionlessdata/tableschema-go/schema"
)
Expand All @@ -12,14 +14,21 @@ type user struct {
}

func main() {
tab, err := csv.NewTable(csv.FromFile("data_infer_utf8.csv"), csv.SetHeaders("id", "age", "name"))
tab, err := csv.NewTable(csv.FromFile("data_infer_utf8.csv"), csv.SetHeaders("ID", "Age", "Name"))
if err != nil {
panic(err)
}
fmt.Println("## Raw Table ##")
fmt.Println(tab)
sch, err := schema.Infer(tab)
if err != nil {
panic(err)
}

fmt.Println("## Schema ##")
fmt.Println(sch)
var users []user
sch.DecodeTable(tab, &users)
sch.CastTable(tab, &users)

fmt.Printf("\n## Cast Table ##\n%+v\n", users)
}
6 changes: 3 additions & 3 deletions examples/validate/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
}
field, _ := capitalSchema.GetField("URL")
if field.TestString("http://new.url.com") {
value, err := field.Decode("http://new.url.com")
value, err := field.Cast("http://new.url.com")
log.Printf("URL unmarshal to value: %v\n", value)
if err != nil {
log.Fatalf("Error casting value: %q", err)
Expand All @@ -52,9 +52,9 @@ func main() {

iter, _ := table.Iter()
for iter.Next() {
if err := capitalSchema.Decode(iter.Row(), &capitalRow); err != nil {
if err := capitalSchema.CastRow(iter.Row(), &capitalRow); err != nil {
log.Fatalf("Couldn't unmarshal row:%v err:%q", iter.Row(), err)
}
log.Printf("Unmarshal Row: %+v\n", capitalRow)
log.Printf("Cast Row: %+v\n", capitalRow)
}
}
2 changes: 1 addition & 1 deletion schema/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ func castAny(value interface{}) (interface{}, error) {
return value, nil
}

func encodeAny(value interface{}) (string, error) {
func uncastAny(value interface{}) (string, error) {
return fmt.Sprintf("%v", value), nil
}
4 changes: 2 additions & 2 deletions schema/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ func TestCastAny(t *testing.T) {
is.Equal("foo", got)
}

func TestEncodeAny(t *testing.T) {
func TestUncastAny(t *testing.T) {
is := is.New(t)
got, err := encodeAny(10)
got, err := uncastAny(10)
is.NoErr(err)
is.Equal("10", got)
}
2 changes: 1 addition & 1 deletion schema/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func castBoolean(value string, trueValues, falseValues []string) (bool, error) {
return false, fmt.Errorf("invalid boolean value:%s", value)
}

func encodeBoolean(value interface{}, trueValues, falseValues []string) (string, error) {
func uncastBoolean(value interface{}, trueValues, falseValues []string) (string, error) {
switch value.(type) {
case bool:
return fmt.Sprintf("%v", value), nil
Expand Down
6 changes: 3 additions & 3 deletions schema/boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCastBoolean_Error(t *testing.T) {
is.True(err != nil)
}

func TestEncodeBoolean(t *testing.T) {
func TestUncastBoolean(t *testing.T) {
t.Run("Success", func(t *testing.T) {
data := []struct {
desc string
Expand All @@ -51,7 +51,7 @@ func TestEncodeBoolean(t *testing.T) {
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
got, err := encodeBoolean(d.value, d.trueValues, d.falseValues)
got, err := uncastBoolean(d.value, d.trueValues, d.falseValues)
is.NoErr(err)
is.Equal(d.want, got)
})
Expand All @@ -70,7 +70,7 @@ func TestEncodeBoolean(t *testing.T) {
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := encodeBoolean(d.value, d.trueValues, d.falseValues)
_, err := uncastBoolean(d.value, d.trueValues, d.falseValues)
is.True(err != nil)
})
}
Expand Down
12 changes: 6 additions & 6 deletions schema/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ package schema

import "time"

func decodeDate(format, value string, c Constraints) (time.Time, error) {
y, err := decodeDateWithoutChecks(format, value)
func castDate(format, value string, c Constraints) (time.Time, error) {
y, err := castDateWithoutChecks(format, value)
if err != nil {
return y, err
}
var max, min time.Time
if c.Maximum != "" {
max, err = decodeDateWithoutChecks(format, c.Maximum)
max, err = castDateWithoutChecks(format, c.Maximum)
if err != nil {
return max, err
}
}
if c.Minimum != "" {
min, err = decodeDateWithoutChecks(format, c.Minimum)
min, err = castDateWithoutChecks(format, c.Minimum)
if err != nil {
return min, err
}
}
return checkConstraints(y, max, min, DateType)
}

func decodeDateWithoutChecks(format, value string) (time.Time, error) {
return decodeDefaultOrCustomTime("2006-01-02", format, value)
func castDateWithoutChecks(format, value string) (time.Time, error) {
return castDefaultOrCustomTime("2006-01-02", format, value)
}
8 changes: 4 additions & 4 deletions schema/date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import (
"github.com/matryer/is"
)

func TestDecodeDate(t *testing.T) {
func TestCastDate(t *testing.T) {
t.Run("ValidMaximum", func(t *testing.T) {
is := is.New(t)
_, err := decodeDate("2006-01-02", "2006-01-02", Constraints{Maximum: "2007-01-02"})
_, err := castDate("2006-01-02", "2006-01-02", Constraints{Maximum: "2007-01-02"})
is.NoErr(err)
})
t.Run("ValidMinimum", func(t *testing.T) {
is := is.New(t)
_, err := decodeDate("2006-01-02", "2007-01-02", Constraints{Minimum: "2006-01-02"})
_, err := castDate("2006-01-02", "2007-01-02", Constraints{Minimum: "2006-01-02"})
is.NoErr(err)
})
t.Run("Error", func(t *testing.T) {
Expand All @@ -32,7 +32,7 @@ func TestDecodeDate(t *testing.T) {
for _, d := range data {
t.Run(d.desc, func(t *testing.T) {
is := is.New(t)
_, err := decodeDate("2006-01-02", d.date, d.constraints)
_, err := castDate("2006-01-02", d.date, d.constraints)
is.True(err != nil)
})
}
Expand Down
32 changes: 16 additions & 16 deletions schema/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,78 +31,78 @@ var strftimeToGoConversionTable = map[string]string{
"%p": "PM",
}

func decodeYearMonth(value string, c Constraints) (time.Time, error) {
y, err := decodeYearMonthWithoutChecks(value)
func castYearMonth(value string, c Constraints) (time.Time, error) {
y, err := castYearMonthWithoutChecks(value)
if err != nil {
return y, err
}
var max, min time.Time
if c.Maximum != "" {
max, err = decodeYearMonthWithoutChecks(c.Maximum)
max, err = castYearMonthWithoutChecks(c.Maximum)
if err != nil {
return y, err
}
}
if c.Minimum != "" {
min, err = decodeYearMonthWithoutChecks(c.Minimum)
min, err = castYearMonthWithoutChecks(c.Minimum)
if err != nil {
return y, err
}
}
return checkConstraints(y, max, min, YearMonthType)
}

func decodeYearMonthWithoutChecks(value string) (time.Time, error) {
func castYearMonthWithoutChecks(value string) (time.Time, error) {
return time.Parse("2006-01", value)
}

func decodeYearWithoutChecks(value string) (time.Time, error) {
func castYearWithoutChecks(value string) (time.Time, error) {
return time.Parse("2006", value)
}

func decodeYear(value string, c Constraints) (time.Time, error) {
y, err := decodeYearWithoutChecks(value)
func castYear(value string, c Constraints) (time.Time, error) {
y, err := castYearWithoutChecks(value)
if err != nil {
return y, err
}
var max, min time.Time
if c.Maximum != "" {
max, err = decodeYearWithoutChecks(c.Maximum)
max, err = castYearWithoutChecks(c.Maximum)
if err != nil {
return y, err
}
}
if c.Minimum != "" {
min, err = decodeYearWithoutChecks(c.Minimum)
min, err = castYearWithoutChecks(c.Minimum)
if err != nil {
return y, err
}
}
return checkConstraints(y, max, min, YearType)
}

func decodeDateTime(value string, c Constraints) (time.Time, error) {
dt, err := decodeDateTimeWithoutChecks(value)
func castDateTime(value string, c Constraints) (time.Time, error) {
dt, err := castDateTimeWithoutChecks(value)
if err != nil {
return dt, err
}
var max, min time.Time
if c.Maximum != "" {
max, err = decodeDateTimeWithoutChecks(c.Maximum)
max, err = castDateTimeWithoutChecks(c.Maximum)
if err != nil {
return dt, err
}
}
if c.Minimum != "" {
min, err = decodeDateTimeWithoutChecks(c.Minimum)
min, err = castDateTimeWithoutChecks(c.Minimum)
if err != nil {
return dt, err
}
}
return checkConstraints(dt, max, min, DateTimeType)
}

func decodeDateTimeWithoutChecks(value string) (time.Time, error) {
func castDateTimeWithoutChecks(value string) (time.Time, error) {
return time.Parse(time.RFC3339, value)
}

Expand All @@ -116,7 +116,7 @@ func checkConstraints(v, max, min time.Time, t string) (time.Time, error) {
return v, nil
}

func decodeDefaultOrCustomTime(defaultFormat, format, value string) (time.Time, error) {
func castDefaultOrCustomTime(defaultFormat, format, value string) (time.Time, error) {
switch format {
case "", defaultFieldFormat:
t, err := time.Parse(defaultFormat, value)
Expand Down
Loading

0 comments on commit b2ccefb

Please sign in to comment.