Skip to content

Commit

Permalink
Introducing FieldType type
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfireman committed Apr 21, 2019
1 parent 86efc58 commit 6ae701c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion schema/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func castDateTimeWithoutChecks(value string) (time.Time, error) {
return time.Parse(time.RFC3339, value)
}

func checkConstraints(v, max, min time.Time, t string) (time.Time, error) {
func checkConstraints(v, max, min time.Time, t FieldType) (time.Time, error) {
if !max.IsZero() && v.After(max) {
return v, fmt.Errorf("constraint check error: %s:%v > maximum:%v", t, v, max)
}
Expand Down
37 changes: 20 additions & 17 deletions schema/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,25 @@ var (
defaultBareNumber = true
)

// FieldType defines the field types.
type FieldType string

// Field types.
const (
IntegerType = "integer"
StringType = "string"
BooleanType = "boolean"
NumberType = "number"
DateType = "date"
ObjectType = "object"
ArrayType = "array"
DateTimeType = "datetime"
TimeType = "time"
YearMonthType = "yearmonth"
YearType = "year"
DurationType = "duration"
GeoPointType = "geopoint"
AnyType = "any"
IntegerType FieldType = "integer"
StringType FieldType = "string"
BooleanType FieldType = "boolean"
NumberType FieldType = "number"
DateType FieldType = "date"
ObjectType FieldType = "object"
ArrayType FieldType = "array"
DateTimeType FieldType = "datetime"
TimeType FieldType = "time"
YearMonthType FieldType = "yearmonth"
YearType FieldType = "year"
DurationType FieldType = "duration"
GeoPointType FieldType = "geopoint"
AnyType FieldType = "any"
)

// Formats.
Expand Down Expand Up @@ -76,9 +79,9 @@ type Constraints struct {
// More: https://specs.frictionlessdata.io/table-schema/#field-descriptors
type Field struct {
// Name of the field. It is mandatory and shuold correspond to the name of field/column in the data file (if it has a name).
Name string `json:"name"`
Type string `json:"type,omitempty"`
Format string `json:"format,omitempty"`
Name string `json:"name"`
Type FieldType `json:"type,omitempty"`
Format string `json:"format,omitempty"`
// A human readable label or title for the field.
Title string `json:"title,omitempty"`
// A description for this field e.g. "The recipient of the funds"
Expand Down
38 changes: 19 additions & 19 deletions schema/infer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ var (
// This structure is optmized for querying.
// It should point a type to what is allowed to be implicitly cast.
// The inner set must be sorted by the narrower first.
implicitCast = map[string][]string{
IntegerType: []string{IntegerType, NumberType, StringType},
NumberType: []string{NumberType, StringType},
BooleanType: []string{BooleanType, IntegerType, NumberType, StringType},
YearMonthType: []string{YearMonthType, DateType, StringType},
YearType: []string{YearType, IntegerType, NumberType, StringType},
DateType: []string{DateType, DateTimeType, StringType},
DateTimeType: []string{DateTimeType, StringType},
TimeType: []string{TimeType, StringType},
DurationType: []string{DurationType, StringType},
ObjectType: []string{ObjectType, StringType},
ArrayType: []string{ArrayType, StringType},
GeoPointType: []string{GeoPointType, ArrayType, StringType},
StringType: []string{},
implicitCast = map[FieldType][]FieldType{
IntegerType: []FieldType{IntegerType, NumberType, StringType},
NumberType: []FieldType{NumberType, StringType},
BooleanType: []FieldType{BooleanType, IntegerType, NumberType, StringType},
YearMonthType: []FieldType{YearMonthType, DateType, StringType},
YearType: []FieldType{YearType, IntegerType, NumberType, StringType},
DateType: []FieldType{DateType, DateTimeType, StringType},
DateTimeType: []FieldType{DateTimeType, StringType},
TimeType: []FieldType{TimeType, StringType},
DurationType: []FieldType{DurationType, StringType},
ObjectType: []FieldType{ObjectType, StringType},
ArrayType: []FieldType{ArrayType, StringType},
GeoPointType: []FieldType{GeoPointType, ArrayType, StringType},
StringType: []FieldType{},
}

// Types ordered from narrower to wider.
orderedTypes = []string{BooleanType, YearType, IntegerType, GeoPointType, NumberType, YearMonthType, DateType, DateTimeType, TimeType, DurationType, ArrayType, ObjectType}
orderedTypes = []FieldType{BooleanType, YearType, IntegerType, GeoPointType, NumberType, YearMonthType, DateType, DateTimeType, TimeType, DurationType, ArrayType, ObjectType}

noConstraints = Constraints{}
)
Expand Down Expand Up @@ -96,7 +96,7 @@ func sample(tab table.Table, cfg *inferConfig) ([][]string, error) {
}

func infer(headers []string, table [][]string) (*Schema, error) {
inferredTypes := make([]map[string]int, len(headers))
inferredTypes := make([]map[FieldType]int, len(headers))
for rowID := range table {
row := table[rowID]
// TODO(danielfireman): the python version does some normalization on
Expand All @@ -106,7 +106,7 @@ func infer(headers []string, table [][]string) (*Schema, error) {
}
for cellIndex, cell := range row {
if inferredTypes[cellIndex] == nil {
inferredTypes[cellIndex] = make(map[string]int)
inferredTypes[cellIndex] = make(map[FieldType]int)
}
// The list below must be ordered by the narrower field type.
t := findType(cell, orderedTypes)
Expand Down Expand Up @@ -153,7 +153,7 @@ func InferImplicitCasting(tab table.Table, opts ...InferOpts) (*Schema, error) {
}

func inferImplicitCasting(headers []string, table [][]string) (*Schema, error) {
inferredTypes := make([]string, len(headers))
inferredTypes := make([]FieldType, len(headers))
for rowID := range table {
row := table[rowID]
// TODO(danielfireman): the python version does some normalization on
Expand Down Expand Up @@ -182,7 +182,7 @@ func inferImplicitCasting(headers []string, table [][]string) (*Schema, error) {
return &schema, nil
}

func findType(value string, checkOrder []string) string {
func findType(value string, checkOrder []FieldType) FieldType {
for _, t := range checkOrder {
switch t {
case BooleanType:
Expand Down

0 comments on commit 6ae701c

Please sign in to comment.