Skip to content

Commit

Permalink
implemented most of Field casting
Browse files Browse the repository at this point in the history
  • Loading branch information
Georges Labrèche committed Aug 19, 2017
1 parent 4f2899f commit b76c427
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 11 deletions.
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ System.out.println(isValid);
Data values can be cast to native Java objects with a Field instance. This allows formats and constraints to be defined for the field in the [field descriptor](https://specs.frictionlessdata.io/table-schema/#field-descriptors):

```java
// TODO: Casting has mostly been implemented in the TypeInferer class.
// Need to hook up with Field class and document.
Field intField = new Field("id", "integer");
int intVal = intField.castValue("242");
System.out.print(intVal);

// 242

Field datetimeField = new Field("date", "datetime");
DateTime datetimeVal = datetimeField.castValue("2008-08-30T01:45:36.123Z");
System.out.print(datetimeVal.getYear());

// 2008

Field geopointField = new Field("coordinates", "geopoint", "array");
int[] geopointVal = geopointField.castValue("[12,21]");
System.out.print("lon: " + geopointVal[0] + ", geopointVal: " + val[1]);

// lon: 12, lat: 21
```
39 changes: 35 additions & 4 deletions src/main/java/io/frictionlessdata/tableschema/Field.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package io.frictionlessdata.tableschema;

import io.frictionlessdata.tableschema.exceptions.ConstraintsException;
import io.frictionlessdata.tableschema.exceptions.InvalidCastException;
import java.lang.reflect.Method;
import org.json.JSONObject;

/**
*
*
*/
public class Field {

private String name = "";
private String type = "";
private String format = "default";
Expand Down Expand Up @@ -46,6 +50,36 @@ public Field(String name, String type, String format, String title, String descr
this.constraints = constraints;
}

/**
* Use the Field definition to cast a value into the Field type.
* @param <Any>
* @param value
* @return
* @throws InvalidCastException
* @throws ConstraintsException
*/
public <Any> Any castValue(String value) throws InvalidCastException, ConstraintsException{
if(this.type.isEmpty()){
throw new InvalidCastException();
}else{
try{
// Using reflection to invoke appropriate type casting method from the TypeInferrer class
String castMethodName = "cast" + (this.type.substring(0, 1).toUpperCase() + this.type.substring(1));
Method method = TypeInferrer.class.getMethod(castMethodName, String.class, String.class);
Object castValue = method.invoke(new TypeInferrer(), this.format, value);

return (Any)castValue;

}catch(Exception e){
throw new InvalidCastException();
}
}
}

/**
* Get the JSON representation of the Field.
* @return
*/
public JSONObject getJson(){
JSONObject json = new JSONObject();
json.put("name", this.name);
Expand All @@ -56,8 +90,5 @@ public JSONObject getJson(){
json.put("constraints", this.constraints);

return json;
}



}
}
25 changes: 23 additions & 2 deletions src/main/java/io/frictionlessdata/tableschema/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.frictionlessdata.tableschema;

import java.net.URL;
import java.time.Duration;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand All @@ -28,6 +29,7 @@ public static void main(String[] args) {
URL url = new URL("https://raw.githubusercontent.com/frictionlessdata/tableschema-java/master/src/test/resources/fixtures/simple_data.csv");
Table table = new Table(url);

/**
// Iterate table
Iterator<String[]> iter = table.iterator();
while(iter.hasNext()){
Expand All @@ -52,7 +54,7 @@ public static void main(String[] args) {
schema.addField(coordinatesField);
System.out.println(schema.getJson());

**/

// Build Schema with JSONObject instances
/**
Expand All @@ -79,6 +81,7 @@ public static void main(String[] args) {
System.out.println(schema2.getJson());
**/

/**
JSONObject schemaJsonObj3 = new JSONObject();
Field nameField3 = new Field("id", "integer");
schemaJsonObj3.put("fields", new JSONArray());
Expand All @@ -94,9 +97,27 @@ public static void main(String[] args) {
isValid = schema3.validate();
System.out.println(isValid);
**/

Field field = new Field("name", "geopoint", "default");
int[] val = field.castValue("12,21");
System.out.print("YPYOOY: " + val[0]);

/**
Field field2 = new Field("name", "geopoint", "array");
int[] val2 = field2.castValue("[12,21]");
System.out.print(val2[0]);
Field field3 = new Field("name", "geopoint", "object");
int[] val3 = field3.castValue("{\"lon\": 12, \"lat\": 21}");
System.out.print(val[30]);
Field field4 = new Field("name", "duration");
Duration val4 = field4.castValue("P2DT3H4M");
System.out.print(val4.getSeconds());
**/


}catch(Exception e){
e.printStackTrace();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/frictionlessdata/tableschema/Table.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.frictionlessdata.tableschema;

import io.frictionlessdata.tableschema.exceptions.TypeInferringException;
import io.frictionlessdata.tableschema.datasources.CsvDataSource;
import io.frictionlessdata.tableschema.datasources.DataSource;
import org.json.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.frictionlessdata.tableschema;

import io.frictionlessdata.tableschema.exceptions.TypeInferringException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -173,8 +174,10 @@ public JSONObject infer(List<String[]> data, String[] headers, int rowLimit) thr

private void findType(String header, String datum){

// Invoke all the type casting methods using reflection.
for(String[] typeInferralDefinition: TYPE_INFERRAL_ORDER_LIST){
try{
// Keep invoking the type casting methods until one doesn't throw an exception
String dataType = typeInferralDefinition[0];
String castMethodName = "cast" + (dataType.substring(0, 1).toUpperCase() + dataType.substring(1));
String format = typeInferralDefinition[1];
Expand Down Expand Up @@ -238,8 +241,7 @@ public Duration castDuration(String format, String value) throws TypeInferringEx
return Duration.parse(value);
}catch(Exception e){
throw new TypeInferringException();
}

}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.frictionlessdata.tableschema.exceptions;

/**
*
* @author pechorin
*/
public class ConstraintsException extends Exception {

/**
* Creates a new instance of <code>ConstraintsException</code> without
* detail message.
*/
public ConstraintsException() {
}

/**
* Constructs an instance of <code>ConstraintsException</code> with the
* specified detail message.
*
* @param msg the detail message.
*/
public ConstraintsException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.frictionlessdata.tableschema.exceptions;

/**
*
* @author pechorin
*/
public class InvalidCastException extends Exception {

/**
* Creates a new instance of <code>InvalidCastException</code> without
* detail message.
*/
public InvalidCastException() {
}

/**
* Constructs an instance of <code>InvalidCastException</code> with the
* specified detail message.
*
* @param msg the detail message.
*/
public InvalidCastException(String msg) {
super(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.frictionlessdata.tableschema;
package io.frictionlessdata.tableschema.exceptions;

/**
*
Expand Down

0 comments on commit b76c427

Please sign in to comment.