diff --git a/README.md b/README.md index 85faa45..b9c6b7f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: diff --git a/src/TableSchema.jl b/src/TableSchema.jl index 2275ea8..88645c4 100644 --- a/src/TableSchema.jl +++ b/src/TableSchema.jl @@ -4,10 +4,15 @@ 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" @@ -15,5 +20,6 @@ DEFAULT_FORMAT = "default" include("descriptor.jl") include("field.jl") include("schema.jl") +include("table.jl") end # module diff --git a/src/field.jl b/src/field.jl index ab8dc05..2511dc4 100644 --- a/src/field.jl +++ b/src/field.jl @@ -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 diff --git a/src/schema.jl b/src/schema.jl index 9a36d0e..7493f9e 100644 --- a/src/schema.jl +++ b/src/schema.jl @@ -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) diff --git a/src/table.jl b/src/table.jl new file mode 100644 index 0000000..1e77fe5 --- /dev/null +++ b/src/table.jl @@ -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 diff --git a/test/data.jl b/test/data.jl index e105a17..e7af57b 100644 --- a/test/data.jl +++ b/test/data.jl @@ -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" +} +""" diff --git a/test/files/table-min.csv b/test/files/table-min.csv new file mode 100644 index 0000000..b6f0bb8 --- /dev/null +++ b/test/files/table-min.csv @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 0854b28..c14e9b9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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