Skip to content

Commit

Permalink
Initial Table struct frictionlessdata#11
Browse files Browse the repository at this point in the history
  • Loading branch information
loleg committed Dec 5, 2017
1 parent 4c2361c commit bb4dffa
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 19 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ A library for working with [Table Schema](http://specs.frictionlessdata.io/table

# Usage

## Table

```Julia
filestream = os.open("cities.csv")
table = Table(filestream)
table.headers # ['city', 'location']
table.read(keyed=True)
# [
# {city: 'london', location: '51.50,-0.11'},
# {city: 'paris', location: '48.85,2.30'},
# {city: 'rome', location: 'N/A'},
# ]
rows = table.read()
err = table.errors # handle errors
...
```

## Schema

```Julia
Expand All @@ -33,16 +50,6 @@ schema = Schema(filestream)
err = schema.errors # handle errors
```

## Table

```Julia
filestream = os.open("data.csv")
table = Table(filestream)
rows = table.read()
err = table.errors # handle errors
...
```

## Field

Add fields to create or expand your schema like this:
Expand Down
10 changes: 8 additions & 2 deletions src/TableSchema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@ https://github.com/frictionlessdata/tableschema-jl
"""
module TableSchema

export Schema, Field, Descriptor
export add_field
export Table
export Schema
export Field
# export validate
# export infer
# export errors

using JSON
# using CSV

DEFAULT_TYPE = "string"
DEFAULT_FORMAT = "default"

include("descriptor.jl")
include("field.jl")
include("schema.jl")
include("table.jl")

end # module
7 changes: 7 additions & 0 deletions src/field.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ type Field
# cast_value = NullException()
# test_value = NullException()

function Field(d::Descriptor)
new(d)
end

function Field()
new(Descriptor())
end
end
4 changes: 1 addition & 3 deletions src/schema.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,4 @@ mutable struct Schema
end
end

function add_field(s::Schema, d::Descriptor)
push!(s.fields, Field(d))
end
add_field(s::Schema, f::Field) = push!(s.fields, f)
28 changes: 28 additions & 0 deletions src/table.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Table Schema generic data structure
https://github.com/frictionlessdata/tableschema-jl#table
"""

mutable struct Table
source::Array{Any}
# stream:: stream
schema::Schema
headers::Array{Any}
# storage::
# strict::Bool

function Table(csv_blob::String, schema::Schema)
source = readcsv(csv_blob)
schema = schema
headers = source[1,:]
new(source, schema, headers)
end
function Table(csv_blob::String)
schema = Schema()
Table(csv_blob, schema)
end
end

function validate(t::Table)
throw(ErrorException("Not implemented"))
end
23 changes: 23 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,26 @@ DESCRIPTOR_MAX_JSON = """
"missingValues": ["", "-", "null"]
}
"""

TABLE_MIN_CSV_FILE = "test/files/table-min.csv"

TABLE_MIN_DATA_CSV = """id,height,age,name,occupation
1,10.0,1,string1,2012-06-15 00:00:00
2,10.1,2,string2,2013-06-15 01:00:00
3,10.2,3,string3,2014-06-15 02:00:00
4,10.3,4,string4,2015-06-15 03:00:00
5,10.4,5,string5,2016-06-15 04:00:00
"""

TABLE_MIN_SCHEMA_JSON = """
{
"fields": [
{"name": "id", "type": "integer", "constraints": {"required": true}},
{"name": "height", "type": "number"},
{"name": "age", "type": "integer"},
{"name": "name", "type": "string", "constraints": {"unique": true}},
{"name": "occupation", "type": "datetime", "format": "any"}
],
"primaryKey": "id"
}
"""
6 changes: 6 additions & 0 deletions test/files/table-min.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
id,height,age,name,occupation
1,10.0,1,string1,2012-06-15 00:00:00
2,10.1,2,string2,2013-06-15 01:00:00
3,10.2,3,string3,2014-06-15 02:00:00
4,10.3,4,string4,2015-06-15 03:00:00
5,10.4,5,string5,2016-06-15 04:00:00
21 changes: 17 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ end

@testset "Validating a Schema" begin
@testset "Create a schema from scratch" begin
f = Field()
f.descriptor._name = "width"
f.descriptor._type = "integer"
s = Schema()
d = TableSchema.Descriptor()
d._name = "width"
d._type = "integer"
add_field(s, d)
TableSchema.add_field(s, f)
@test length(s.fields) == 1
end
@testset "Check any constraints" begin
Expand All @@ -50,3 +50,16 @@ end
# @test length(s.foreignKeys) == 1
# end
end

@testset "Loading a Table" begin
@testset "Import from a CSV" begin
# t = Table(IOBuffer(TABLE_MIN_DATA_CSV))
t = Table(TABLE_MIN_CSV_FILE)
@test length(t.headers) == 5
end
@testset "Validate with schema" begin
s = Schema(DESCRIPTOR_MAX_JSON)
t = Table(TABLE_MIN_CSV_FILE, s)
# @test TableSchema.validate(t)
end
end

0 comments on commit bb4dffa

Please sign in to comment.