embulk-filter-typecast 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ced2804053187863055f59aa064418cd0b8d7aa
4
- data.tar.gz: 447a4328482d178a479b404bfb7af62c61a2c1f3
3
+ metadata.gz: 9fa85a581cc7a32dbae99b2fbacac7150ffe930f
4
+ data.tar.gz: fd78481a512883f60c0c360cb1ffc08335e70fa5
5
5
  SHA512:
6
- metadata.gz: 2833a31678457b155864532d95de1af40594ffa7d5ffee4d489bcc4d993b444dacd18adbbcac444b2ad6b2fe85b2fbb837fa559abbdadd13010a98009894ec1d
7
- data.tar.gz: f668410813b7109c96a6277b6ecd0b576334f74261f2c5ce5d941ddc0b6b6e2b4e84d4daeedde49b3a86abc140cc99481fa7a5a0e0b330a9494efccd0cd5eaaf
6
+ metadata.gz: 2dcc900cd25a80f8cb6226e3669a7a35b04065fc1a925fd0bc55ee03ee19c90315e1bacf2e00fe35713e11dc60a4ce50feba962ad4e919a5322c67f8726c6f5b
7
+ data.tar.gz: 0b800b1434d4099d29fbe7429a97fef8a6dc108dddea0672238528edb06b75f656b07e0a5cc95a377ebab7dad0555a55dcd3068504c2a6dffddb38523614885f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 0.1.4 (2016-10-26)
2
+
3
+ Enhancements:
4
+
5
+ * Support casting Json into String **IN** Json payloads
6
+
1
7
  # 0.1.3 (2016-05-19)
2
8
 
3
9
  Enhancements:
data/build.gradle CHANGED
@@ -13,15 +13,17 @@ configurations {
13
13
  provided
14
14
  }
15
15
 
16
- version = "0.1.3"
16
+ version = "0.1.4"
17
17
  sourceCompatibility = 1.7
18
18
  targetCompatibility = 1.7
19
19
 
20
20
  dependencies {
21
21
  compile "org.embulk:embulk-core:0.8.+"
22
22
  provided "org.embulk:embulk-core:0.8.+"
23
- // compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
23
+
24
24
  testCompile "junit:junit:4.+"
25
+ testCompile "org.embulk:embulk-core:0.8.+:tests"
26
+ testCompile "org.embulk:embulk-standards:0.8.+"
25
27
  }
26
28
 
27
29
  checkstyle {
@@ -0,0 +1,18 @@
1
+ 2016-10-26 17:11:25.347 +0900: Embulk v0.8.13
2
+ 2016-10-26 17:11:26.784 +0900 [INFO] (0001:preview): Loaded plugin embulk/filter/typecast from a load path
3
+ 2016-10-26 17:11:26.812 +0900 [INFO] (0001:preview): Listing local files at directory 'example' filtering filename by prefix 'example.csv'
4
+ 2016-10-26 17:11:26.818 +0900 [INFO] (0001:preview): Loading files [example/example.csv]
5
+ +-----------------------------+-----------+-----------+---------------+---------------+--------------+------------+----------------+----------------+---------------+-----------------+
6
+ | timestamp:timestamp | null:long | long:long | string:string | double:double | json1:json | json2:json | array_str:json | array_int:json | ignore:string | boolean:boolean |
7
+ +-----------------------------+-----------+-----------+---------------+---------------+--------------+------------+----------------+----------------+---------------+-----------------+
8
+ | 2015-07-12 15:00:00.100 UTC | | 90 | l6lTsvxd | 903.4 | {"string":0} | {"long":0} | [0] | [0] | 2015-07-13 | true |
9
+ | 2015-07-12 15:00:00.100 UTC | | 91 | XoALSEQg | 394.5 | {"string":1} | {"long":1} | [1] | [1] | 2015-07-13 | true |
10
+ | 2015-07-12 15:00:00.100 UTC | | 92 | 0hgDRI_m | 810.9 | {"string":2} | {"long":2} | [2] | [2] | 2015-07-13 | true |
11
+ | 2015-07-12 15:00:00.100 UTC | | 93 | KjCRAc-A | 477.4 | {"string":3} | {"long":3} | [3] | [3] | 2015-07-13 | true |
12
+ | 2015-07-12 15:00:00.100 UTC | | 94 | fyQVGlT8 | 725.3 | {"string":4} | {"long":4} | [4] | [4] | 2015-07-13 | true |
13
+ | 2015-07-12 15:00:00.100 UTC | | 95 | FpBYRPWK | 316.6 | {"string":5} | {"long":5} | [5] | [5] | 2015-07-13 | false |
14
+ | 2015-07-12 15:00:00.100 UTC | | 96 | 9ikvnUqp | 369.5 | {"string":6} | {"long":6} | [6] | [6] | 2015-07-13 | false |
15
+ | 2015-07-12 15:00:00.100 UTC | | 97 | RRNYDAzK | 506.5 | {"string":7} | {"long":7} | [7] | [7] | 2015-07-13 | false |
16
+ | 2015-07-12 15:00:00.100 UTC | | 90 | l6lTsvxd | 903.4 | {"string":8} | {"long":8} | [8] | [8] | 2015-07-13 | false |
17
+ | 2015-07-12 15:00:00.100 UTC | | 91 | XoALSEQg | 394.5 | {"string":9} | {"long":9} | [9] | [9] | 2015-07-13 | false |
18
+ +-----------------------------+-----------+-----------+---------------+---------------+--------------+------------+----------------+----------------+---------------+-----------------+
@@ -5,7 +5,7 @@ in:
5
5
  type: csv
6
6
  charset: UTF-8
7
7
  newline: CRLF
8
- null_string: 'NULL'
8
+ null_string: ''
9
9
  skip_header_lines: 1
10
10
  comment_line_marker: '#'
11
11
  columns:
@@ -16,7 +16,10 @@ in:
16
16
  - {name: double, type: string}
17
17
  - {name: json1, type: string}
18
18
  - {name: json2, type: string}
19
- - {name: boolean, type: boolean}
19
+ - {name: array_str, type: string}
20
+ - {name: array_int, type: string}
21
+ - {name: ignore, type: string}
22
+ - {name: boolean, type: string}
20
23
  filters:
21
24
  - type: typecast
22
25
  columns:
@@ -27,8 +30,12 @@ filters:
27
30
  - {name: double, type: double}
28
31
  - {name: json1, type: json}
29
32
  - {name: json2, type: json}
33
+ - {name: array_str, type: json}
34
+ - {name: array_int, type: json}
30
35
  - {name: boolean, type: boolean}
31
36
  - {name: "$.json1.string", type: long}
32
37
  - {name: "$.json2.long", type: long}
38
+ - {name: "$.array_str[0]", type: long}
39
+ - {name: "$.array_int[*]", type: long}
33
40
  out:
34
41
  type: "null"
@@ -0,0 +1,4 @@
1
+ {"user_id":135400, "data":{"data":{"transaction_name":"1421149413_10"}},"payload":{"schemaVersion":5}}
2
+ {"user_id":133230, "data":{"data":{"transaction_name":"1420839135_16"}},"payload":{"schemaVersion":5}}
3
+ {"user_id":134410, "data":{"data":{"transaction_name":"1420683229_18"}},"payload":{"schemaVersion":5}}
4
+ {"user_id":133220, "data":{"data":{"transaction_name":"1421586269_10"}},"payload":{"schemaVersion":5}}
@@ -0,0 +1,13 @@
1
+ 2016-10-26 17:20:08.421 +0900: Embulk v0.8.13
2
+ 2016-10-26 17:20:09.812 +0900 [INFO] (0001:preview): Loaded plugin embulk/filter/typecast from a load path
3
+ 2016-10-26 17:20:09.839 +0900 [INFO] (0001:preview): Listing local files at directory 'example' filtering filename by prefix 'jsoncast.json'
4
+ 2016-10-26 17:20:09.846 +0900 [INFO] (0001:preview): Loading files [example/jsoncast.json]
5
+ 2016-10-26 17:20:09.943 +0900 [INFO] (0001:preview): Loaded plugin embulk-parser-jsonl (0.2.0)
6
+ +--------------+-----------------------------------------------------+---------------------+
7
+ | user_id:long | data:json | payload:json |
8
+ +--------------+-----------------------------------------------------+---------------------+
9
+ | 135,400 | {"data":"{\"transaction_name\":\"1421149413_10\"}"} | {"schemaVersion":5} |
10
+ | 133,230 | {"data":"{\"transaction_name\":\"1420839135_16\"}"} | {"schemaVersion":5} |
11
+ | 134,410 | {"data":"{\"transaction_name\":\"1420683229_18\"}"} | {"schemaVersion":5} |
12
+ | 133,220 | {"data":"{\"transaction_name\":\"1421586269_10\"}"} | {"schemaVersion":5} |
13
+ +--------------+-----------------------------------------------------+---------------------+
@@ -0,0 +1,20 @@
1
+ in:
2
+ type: file
3
+ path_prefix: example/jsoncast.json
4
+ parser:
5
+ type: jsonl
6
+ default_timezone: Asia/Tokyo
7
+ columns:
8
+ - name: user_id
9
+ type: long
10
+ - name: data
11
+ type: json
12
+ - name: payload
13
+ type: json
14
+ filters:
15
+ - type: typecast
16
+ columns:
17
+ - name: $.data.data
18
+ type: string
19
+ out:
20
+ type: "null"
@@ -0,0 +1,18 @@
1
+ 2016-10-26 17:06:46.161 +0900: Embulk v0.8.13
2
+ 2016-10-26 17:06:47.637 +0900 [INFO] (0001:preview): Loaded plugin embulk/filter/typecast from a load path
3
+ 2016-10-26 17:06:47.664 +0900 [INFO] (0001:preview): Listing local files at directory 'example' filtering filename by prefix 'example.csv'
4
+ 2016-10-26 17:06:47.671 +0900 [INFO] (0001:preview): Loading files [example/example.csv]
5
+ +-------------------------------+-------------+-------------+---------------+---------------+----------------+--------------+----------------+----------------+-------------------------+----------------+
6
+ | timestamp:string | null:string | long:string | string:string | double:string | json1:string | json2:string | array_str:json | array_int:json | ignore:timestamp | boolean:string |
7
+ +-------------------------------+-------------+-------------+---------------+---------------+----------------+--------------+----------------+----------------+-------------------------+----------------+
8
+ | 2015-07-13 00:00:00.100000000 | | 90 | l6lTsvxd | 903.4 | {"string":"0"} | {"long":"0"} | ["0"] | ["0"] | 2015-07-12 15:00:00 UTC | true |
9
+ | 2015-07-13 00:00:00.100000000 | | 91 | XoALSEQg | 394.5 | {"string":"1"} | {"long":"1"} | ["1"] | ["1"] | 2015-07-12 15:00:00 UTC | true |
10
+ | 2015-07-13 00:00:00.100000000 | | 92 | 0hgDRI_m | 810.9 | {"string":"2"} | {"long":"2"} | ["2"] | ["2"] | 2015-07-12 15:00:00 UTC | true |
11
+ | 2015-07-13 00:00:00.100000000 | | 93 | KjCRAc-A | 477.4 | {"string":"3"} | {"long":"3"} | ["3"] | ["3"] | 2015-07-12 15:00:00 UTC | true |
12
+ | 2015-07-13 00:00:00.100000000 | | 94 | fyQVGlT8 | 725.3 | {"string":"4"} | {"long":"4"} | ["4"] | ["4"] | 2015-07-12 15:00:00 UTC | true |
13
+ | 2015-07-13 00:00:00.100000000 | | 95 | FpBYRPWK | 316.6 | {"string":"5"} | {"long":"5"} | ["5"] | ["5"] | 2015-07-12 15:00:00 UTC | false |
14
+ | 2015-07-13 00:00:00.100000000 | | 96 | 9ikvnUqp | 369.5 | {"string":"6"} | {"long":"6"} | ["6"] | ["6"] | 2015-07-12 15:00:00 UTC | false |
15
+ | 2015-07-13 00:00:00.100000000 | | 97 | RRNYDAzK | 506.5 | {"string":"7"} | {"long":"7"} | ["7"] | ["7"] | 2015-07-12 15:00:00 UTC | false |
16
+ | 2015-07-13 00:00:00.100000000 | | 90 | l6lTsvxd | 903.4 | {"string":"8"} | {"long":"8"} | ["8"] | ["8"] | 2015-07-12 15:00:00 UTC | false |
17
+ | 2015-07-13 00:00:00.100000000 | | 91 | XoALSEQg | 394.5 | {"string":"9"} | {"long":"9"} | ["9"] | ["9"] | 2015-07-12 15:00:00 UTC | false |
18
+ +-------------------------------+-------------+-------------+---------------+---------------+----------------+--------------+----------------+----------------+-------------------------+----------------+
@@ -32,7 +32,7 @@ filters:
32
32
  - {name: boolean, type: string}
33
33
  - {name: "$.json1.string", type: string}
34
34
  - {name: "$.json2.long", type: string}
35
- - {name: "$.array_str[0]", type: long}
35
+ - {name: "$.array_str[0]", type: string}
36
36
  - {name: "$.array_int[*]", type: string}
37
37
  out:
38
38
  type: "null"
@@ -2,9 +2,9 @@ package org.embulk.filter.typecast;
2
2
 
3
3
  import org.embulk.filter.typecast.cast.BooleanCast;
4
4
  import org.embulk.filter.typecast.cast.DoubleCast;
5
+ import org.embulk.filter.typecast.cast.JsonCast;
5
6
  import org.embulk.filter.typecast.cast.LongCast;
6
7
  import org.embulk.filter.typecast.cast.StringCast;
7
- import org.embulk.spi.DataException;
8
8
  import org.embulk.spi.type.BooleanType;
9
9
  import org.embulk.spi.type.DoubleType;
10
10
  import org.embulk.spi.type.JsonType;
@@ -37,7 +37,7 @@ class JsonCaster
37
37
  return ValueFactory.newString(BooleanCast.asString(value.getBoolean()));
38
38
  }
39
39
  else if (outputType instanceof JsonType) {
40
- throw new DataException(String.format("cannot cast boolean to json: \"%s\"", value));
40
+ return BooleanCast.asJson(value.getBoolean());
41
41
  }
42
42
  else {
43
43
  assert (false);
@@ -60,7 +60,7 @@ class JsonCaster
60
60
  return ValueFactory.newString(LongCast.asString(value.asLong()));
61
61
  }
62
62
  else if (outputType instanceof JsonType) {
63
- throw new DataException(String.format("cannot cast long to json:: \"%s\"", value));
63
+ return LongCast.asJson(value.asLong());
64
64
  }
65
65
  else {
66
66
  assert false;
@@ -83,7 +83,7 @@ class JsonCaster
83
83
  return ValueFactory.newString(DoubleCast.asString(value.toDouble()));
84
84
  }
85
85
  else if (outputType instanceof JsonType) {
86
- throw new DataException(String.format("cannot cast double to json:: \"%s\"", value));
86
+ return DoubleCast.asJson(value.toDouble());
87
87
  }
88
88
  else {
89
89
  assert (false);
@@ -113,4 +113,27 @@ class JsonCaster
113
113
  return null;
114
114
  }
115
115
  }
116
+
117
+ public Value fromJson(Type outputType, Value value)
118
+ {
119
+ if (outputType instanceof BooleanType) {
120
+ return ValueFactory.newBoolean(JsonCast.asBoolean(value));
121
+ }
122
+ else if (outputType instanceof LongType) {
123
+ return ValueFactory.newInteger(JsonCast.asLong(value));
124
+ }
125
+ else if (outputType instanceof DoubleType) {
126
+ return ValueFactory.newFloat(JsonCast.asDouble(value));
127
+ }
128
+ else if (outputType instanceof StringType) {
129
+ return ValueFactory.newString(JsonCast.asString(value));
130
+ }
131
+ else if (outputType instanceof JsonType) {
132
+ return value;
133
+ }
134
+ else {
135
+ assert false;
136
+ return null;
137
+ }
138
+ }
116
139
  }
@@ -84,19 +84,43 @@ public class JsonVisitor
84
84
  return shouldVisitSet.contains(jsonPath);
85
85
  }
86
86
 
87
- public Value visit(String jsonPath, Value value)
87
+ public Value visit(String rootPath, Value value)
88
88
  {
89
- if (!shouldVisit(jsonPath)) {
89
+ if (!shouldVisit(rootPath)) {
90
90
  return value;
91
91
  }
92
+ Type outputType = jsonPathTypeMap.get(rootPath);
93
+ if (outputType != null) {
94
+ if (value.isBooleanValue()) {
95
+ return jsonCaster.fromBoolean(outputType, value.asBooleanValue());
96
+ }
97
+ else if (value.isIntegerValue()) {
98
+ return jsonCaster.fromLong(outputType, value.asIntegerValue());
99
+ }
100
+ else if (value.isFloatValue()) {
101
+ return jsonCaster.fromDouble(outputType, value.asFloatValue());
102
+ }
103
+ else if (value.isStringValue()) {
104
+ return jsonCaster.fromString(outputType, value.asStringValue());
105
+ }
106
+ else if (value.isArrayValue()) {
107
+ return jsonCaster.fromJson(outputType, value);
108
+ }
109
+ else if (value.isMapValue()) {
110
+ return jsonCaster.fromJson(outputType, value);
111
+ }
112
+ else {
113
+ return value;
114
+ }
115
+ }
92
116
  if (value.isArrayValue()) {
93
117
  ArrayValue arrayValue = value.asArrayValue();
94
118
  int size = arrayValue.size();
95
119
  Value[] newValue = new Value[size];
96
120
  for (int i = 0; i < size; i++) {
97
- String k = new StringBuilder(jsonPath).append("[").append(Integer.toString(i)).append("]").toString();
121
+ String k = new StringBuilder(rootPath).append("[").append(Integer.toString(i)).append("]").toString();
98
122
  if (!shouldVisit(k)) {
99
- k = new StringBuilder(jsonPath).append("[*]").toString(); // try [*] too
123
+ k = new StringBuilder(rootPath).append("[*]").toString(); // try [*] too
100
124
  }
101
125
  Value v = arrayValue.get(i);
102
126
  newValue[i] = visit(k, v);
@@ -111,29 +135,13 @@ public class JsonVisitor
111
135
  for (Map.Entry<Value, Value> entry : mapValue.entrySet()) {
112
136
  Value k = entry.getKey();
113
137
  Value v = entry.getValue();
114
- String newPath = new StringBuilder(jsonPath).append(".").append(k.asStringValue().asString()).toString();
138
+ String newPath = new StringBuilder(rootPath).append(".").append(k.asStringValue().asString()).toString();
115
139
  Value r = visit(newPath, v);
116
140
  newValue[i++] = k;
117
141
  newValue[i++] = r;
118
142
  }
119
143
  return ValueFactory.newMap(newValue, true);
120
144
  }
121
- else if (value.isBooleanValue()) {
122
- Type outputType = jsonPathTypeMap.get(jsonPath);
123
- return jsonCaster.fromBoolean(outputType, value.asBooleanValue());
124
- }
125
- else if (value.isIntegerValue()) {
126
- Type outputType = jsonPathTypeMap.get(jsonPath);
127
- return jsonCaster.fromLong(outputType, value.asIntegerValue());
128
- }
129
- else if (value.isFloatValue()) {
130
- Type outputType = jsonPathTypeMap.get(jsonPath);
131
- return jsonCaster.fromDouble(outputType, value.asFloatValue());
132
- }
133
- else if (value.isStringValue()) {
134
- Type outputType = jsonPathTypeMap.get(jsonPath);
135
- return jsonCaster.fromString(outputType, value.asStringValue());
136
- }
137
145
  else {
138
146
  return value;
139
147
  }
@@ -14,7 +14,7 @@ public class StringCast
14
14
  private static final JsonParser jsonParser = new JsonParser();
15
15
 
16
16
  // copy from csv plugin
17
- private static final ImmutableSet<String> TRUE_STRINGS =
17
+ public static final ImmutableSet<String> TRUE_STRINGS =
18
18
  ImmutableSet.of(
19
19
  "true", "True", "TRUE",
20
20
  "yes", "Yes", "YES",
@@ -22,7 +22,7 @@ public class StringCast
22
22
  "on", "On", "ON",
23
23
  "1");
24
24
 
25
- private static final ImmutableSet<String> FALSE_STRINGS =
25
+ public static final ImmutableSet<String> FALSE_STRINGS =
26
26
  ImmutableSet.of(
27
27
  "false", "False", "FALSE",
28
28
  "no", "No", "NO",
@@ -0,0 +1,48 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.spi.DataException;
4
+ import org.junit.Test;
5
+
6
+ import static org.junit.Assert.assertEquals;
7
+
8
+ public class TestBooleanCast
9
+ {
10
+ @Test
11
+ public void asBoolean()
12
+ {
13
+ assertEquals(true, BooleanCast.asBoolean(true));
14
+ assertEquals(false, BooleanCast.asBoolean(false));
15
+ }
16
+
17
+ @Test
18
+ public void asLong()
19
+ {
20
+ assertEquals(1, BooleanCast.asLong(true));
21
+ assertEquals(0, BooleanCast.asLong(false));
22
+ }
23
+
24
+ @Test(expected = DataException.class)
25
+ public void asDouble()
26
+ {
27
+ BooleanCast.asDouble(true);
28
+ }
29
+
30
+ @Test
31
+ public void asString()
32
+ {
33
+ assertEquals("true", BooleanCast.asString(true));
34
+ assertEquals("false", BooleanCast.asString(false));
35
+ }
36
+
37
+ @Test(expected = DataException.class)
38
+ public void asJson()
39
+ {
40
+ BooleanCast.asJson(true);
41
+ }
42
+
43
+ @Test(expected = DataException.class)
44
+ public void asTimestamp()
45
+ {
46
+ BooleanCast.asTimestamp(true);
47
+ }
48
+ }
@@ -0,0 +1,49 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.spi.DataException;
4
+ import org.embulk.spi.time.Timestamp;
5
+ import org.junit.Test;
6
+
7
+ import static org.junit.Assert.assertEquals;
8
+ import static org.junit.Assert.assertTrue;
9
+ import static org.junit.Assert.fail;
10
+
11
+ public class TestDoubleCast
12
+ {
13
+ @Test
14
+ public void asBoolean()
15
+ {
16
+ try {
17
+ DoubleCast.asBoolean(0.5);
18
+ fail();
19
+ }
20
+ catch (Throwable t) {
21
+ assertTrue(t instanceof DataException);
22
+ }
23
+ }
24
+
25
+ @Test
26
+ public void asLong()
27
+ {
28
+ assertEquals(0, DoubleCast.asLong(0.5));
29
+ }
30
+
31
+ @Test
32
+ public void asDouble()
33
+ {
34
+ assertEquals(0.5, DoubleCast.asDouble(0.5), 0.0);
35
+ }
36
+
37
+ @Test(expected = DataException.class)
38
+ public void asJson()
39
+ {
40
+ DoubleCast.asJson(0.5);
41
+ }
42
+
43
+ @Test
44
+ public void asTimestamp()
45
+ {
46
+ Timestamp expected = Timestamp.ofEpochSecond(1, 500000000);
47
+ assertEquals(expected, DoubleCast.asTimestamp(1.5));
48
+ }
49
+ }
@@ -0,0 +1,59 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.spi.DataException;
4
+ import org.junit.Before;
5
+ import org.junit.Test;
6
+ import org.msgpack.value.Value;
7
+ import org.msgpack.value.ValueFactory;
8
+
9
+ import static org.junit.Assert.assertEquals;
10
+
11
+ public class TestJsonCast
12
+ {
13
+ public Value value;
14
+
15
+ @Before
16
+ public void createResource()
17
+ {
18
+ Value[] kvs = new Value[2];
19
+ kvs[0] = ValueFactory.newString("k");
20
+ kvs[1] = ValueFactory.newString("v");
21
+ value = ValueFactory.newMap(kvs);
22
+ }
23
+
24
+ @Test(expected = DataException.class)
25
+ public void asBoolean()
26
+ {
27
+ JsonCast.asBoolean(value);
28
+ }
29
+
30
+ @Test(expected = DataException.class)
31
+ public void asLong()
32
+ {
33
+ JsonCast.asLong(value);
34
+ }
35
+
36
+ @Test(expected = DataException.class)
37
+ public void asDouble()
38
+ {
39
+ JsonCast.asDouble(value);
40
+ }
41
+
42
+ @Test
43
+ public void asString()
44
+ {
45
+ assertEquals("{\"k\":\"v\"}", JsonCast.asString(value));
46
+ }
47
+
48
+ @Test
49
+ public void asJson()
50
+ {
51
+ assertEquals(value, JsonCast.asJson(value));
52
+ }
53
+
54
+ @Test(expected = DataException.class)
55
+ public void asTimestamp()
56
+ {
57
+ JsonCast.asTimestamp(value);
58
+ }
59
+ }
@@ -0,0 +1,48 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.spi.DataException;
4
+ import org.embulk.spi.time.Timestamp;
5
+ import org.junit.Test;
6
+
7
+ import static org.junit.Assert.assertEquals;
8
+
9
+ public class TestLongCast
10
+ {
11
+ @Test
12
+ public void asBoolean()
13
+ {
14
+ assertEquals(true, LongCast.asBoolean(1));
15
+ assertEquals(false, LongCast.asBoolean(0));
16
+ }
17
+
18
+ @Test
19
+ public void asLong()
20
+ {
21
+ assertEquals(1, LongCast.asLong(1));
22
+ }
23
+
24
+ @Test
25
+ public void asDouble()
26
+ {
27
+ assertEquals(1.0, LongCast.asDouble(1), 0.0);
28
+ }
29
+
30
+ @Test
31
+ public void asString()
32
+ {
33
+ assertEquals("1", LongCast.asString(1));
34
+ }
35
+
36
+ @Test(expected = DataException.class)
37
+ public void asJson()
38
+ {
39
+ LongCast.asJson(1);
40
+ }
41
+
42
+ @Test
43
+ public void asTimestamp()
44
+ {
45
+ Timestamp expected = Timestamp.ofEpochSecond(1);
46
+ assertEquals(expected, LongCast.asTimestamp(1));
47
+ }
48
+ }
@@ -0,0 +1,116 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.EmbulkTestRuntime;
4
+ import org.embulk.spi.DataException;
5
+ import org.embulk.spi.time.Timestamp;
6
+ import org.embulk.spi.time.TimestampParser;
7
+ import org.joda.time.DateTimeZone;
8
+ import org.jruby.embed.ScriptingContainer;
9
+ import org.junit.Before;
10
+ import org.junit.Rule;
11
+ import org.junit.Test;
12
+ import org.msgpack.value.Value;
13
+ import org.msgpack.value.ValueFactory;
14
+
15
+ import static org.junit.Assert.assertEquals;
16
+ import static org.junit.Assert.assertTrue;
17
+ import static org.junit.Assert.fail;
18
+
19
+ public class TestStringCast
20
+ {
21
+ @Rule
22
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
23
+ public ScriptingContainer jruby;
24
+
25
+ @Before
26
+ public void createResource()
27
+ {
28
+ jruby = new ScriptingContainer();
29
+ }
30
+
31
+ @Test
32
+ public void asBoolean()
33
+ {
34
+ for (String str : StringCast.TRUE_STRINGS) {
35
+ assertEquals(true, StringCast.asBoolean(str));
36
+ }
37
+ for (String str : StringCast.FALSE_STRINGS) {
38
+ assertEquals(false, StringCast.asBoolean(str));
39
+ }
40
+ try {
41
+ StringCast.asBoolean("foo");
42
+ fail();
43
+ }
44
+ catch (Throwable t) {
45
+ assertTrue(t instanceof DataException);
46
+ }
47
+ }
48
+
49
+ @Test
50
+ public void asLong()
51
+ {
52
+ assertEquals(1, StringCast.asLong("1"));
53
+ try {
54
+ StringCast.asLong("1.5");
55
+ fail();
56
+ }
57
+ catch (Throwable t) {
58
+ assertTrue(t instanceof DataException);
59
+ }
60
+ try {
61
+ StringCast.asLong("foo");
62
+ fail();
63
+ }
64
+ catch (Throwable t) {
65
+ assertTrue(t instanceof DataException);
66
+ }
67
+ }
68
+
69
+ @Test
70
+ public void asDouble()
71
+ {
72
+ assertEquals(1.0, StringCast.asDouble("1"), 0.0);
73
+ assertEquals(1.5, StringCast.asDouble("1.5"), 0.0);
74
+ try {
75
+ StringCast.asDouble("foo");
76
+ fail();
77
+ }
78
+ catch (Throwable t) {
79
+ assertTrue(t instanceof DataException);
80
+ }
81
+ }
82
+
83
+ @Test
84
+ public void asString()
85
+ {
86
+ assertEquals("1", StringCast.asString("1"));
87
+ assertEquals("1.5", StringCast.asString("1.5"));
88
+ assertEquals("foo", StringCast.asString("foo"));
89
+ }
90
+
91
+ @Test
92
+ public void asJson()
93
+ {
94
+ Value[] kvs = new Value[2];
95
+ kvs[0] = ValueFactory.newString("k");
96
+ kvs[1] = ValueFactory.newString("v");
97
+ Value value = ValueFactory.newMap(kvs);
98
+ assertEquals(value, StringCast.asJson("{\"k\":\"v\"}"));
99
+ }
100
+
101
+ @Test
102
+ public void asTimestamp()
103
+ {
104
+ Timestamp expected = Timestamp.ofEpochSecond(1463084053, 123456000);
105
+ TimestampParser parser = new TimestampParser(jruby, "%Y-%m-%d %H:%M:%S.%N", DateTimeZone.UTC);
106
+ assertEquals(expected, StringCast.asTimestamp("2016-05-12 20:14:13.123456", parser));
107
+
108
+ try {
109
+ StringCast.asTimestamp("foo", parser);
110
+ fail();
111
+ }
112
+ catch (Throwable t) {
113
+ assertTrue(t instanceof DataException);
114
+ }
115
+ }
116
+ }
@@ -0,0 +1,71 @@
1
+ package org.embulk.filter.typecast.cast;
2
+
3
+ import org.embulk.EmbulkTestRuntime;
4
+ import org.embulk.spi.DataException;
5
+ import org.embulk.spi.time.Timestamp;
6
+ import org.embulk.spi.time.TimestampFormatter;
7
+ import org.embulk.spi.time.TimestampParser;
8
+ import org.joda.time.DateTimeZone;
9
+ import org.jruby.embed.ScriptingContainer;
10
+ import org.junit.Before;
11
+ import org.junit.Rule;
12
+ import org.junit.Test;
13
+ import org.msgpack.value.Value;
14
+ import org.msgpack.value.ValueFactory;
15
+
16
+ import static org.junit.Assert.assertEquals;
17
+ import static org.junit.Assert.assertTrue;
18
+ import static org.junit.Assert.fail;
19
+
20
+ public class TestTimestampCast
21
+ {
22
+ @Rule
23
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
24
+ public ScriptingContainer jruby;
25
+ public Timestamp timestamp;
26
+
27
+ @Before
28
+ public void createResource()
29
+ {
30
+ timestamp = Timestamp.ofEpochSecond(1463084053, 500000000);
31
+ jruby = new ScriptingContainer();
32
+ }
33
+
34
+ @Test(expected = DataException.class)
35
+ public void asBoolean()
36
+ {
37
+ TimestampCast.asBoolean(timestamp);
38
+ }
39
+
40
+ @Test
41
+ public void asLong()
42
+ {
43
+ assertEquals(timestamp.getEpochSecond(), TimestampCast.asLong(timestamp));
44
+ }
45
+
46
+ @Test
47
+ public void asDouble()
48
+ {
49
+ double unixtimestamp = timestamp.getEpochSecond() + timestamp.getNano() / 1000000000.0;
50
+ assertEquals(unixtimestamp, TimestampCast.asDouble(timestamp), 0.0);
51
+ }
52
+
53
+ @Test
54
+ public void asString()
55
+ {
56
+ TimestampFormatter formatter = new TimestampFormatter(jruby, "%Y-%m-%d %H:%M:%S.%6N", DateTimeZone.UTC);
57
+ assertEquals("2016-05-12 20:14:13.500000", TimestampCast.asString(timestamp, formatter));
58
+ }
59
+
60
+ @Test(expected = DataException.class)
61
+ public void asJson()
62
+ {
63
+ TimestampCast.asJson(timestamp);
64
+ }
65
+
66
+ @Test
67
+ public void asTimestamp()
68
+ {
69
+ assertEquals(timestamp, TimestampCast.asTimestamp(timestamp));
70
+ }
71
+ }
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-filter-typecast
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-19 00:00:00.000000000 Z
11
+ date: 2016-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: bundler
15
- version_requirements: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: '1.0'
20
14
  requirement: !ruby/object:Gem::Requirement
21
15
  requirements:
22
16
  - - ~>
23
17
  - !ruby/object:Gem::Version
24
18
  version: '1.0'
19
+ name: bundler
25
20
  prerelease: false
26
21
  type: :development
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
22
  version_requirements: !ruby/object:Gem::Requirement
30
23
  requirements:
31
- - - '>='
24
+ - - ~>
32
25
  - !ruby/object:Gem::Version
33
- version: '10.0'
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
34
28
  requirement: !ruby/object:Gem::Requirement
35
29
  requirements:
36
30
  - - '>='
37
31
  - !ruby/object:Gem::Version
38
32
  version: '10.0'
33
+ name: rake
39
34
  prerelease: false
40
35
  type: :development
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
41
  description: A filter plugin for Embulk to cast column type.
42
42
  email:
43
43
  - sonots@gmail.com
@@ -54,8 +54,13 @@ files:
54
54
  - config/checkstyle/checkstyle.xml
55
55
  - example/empty.yml
56
56
  - example/example.csv
57
- - example/example.yml
58
- - example/example2.yml
57
+ - example/from_string.txt
58
+ - example/from_string.yml
59
+ - example/jsoncast.json
60
+ - example/jsoncast.txt
61
+ - example/jsoncast.yml
62
+ - example/to_string.txt
63
+ - example/to_string.yml
59
64
  - gradle/wrapper/gradle-wrapper.jar
60
65
  - gradle/wrapper/gradle-wrapper.properties
61
66
  - gradlew
@@ -73,8 +78,13 @@ files:
73
78
  - src/main/java/org/embulk/filter/typecast/cast/LongCast.java
74
79
  - src/main/java/org/embulk/filter/typecast/cast/StringCast.java
75
80
  - src/main/java/org/embulk/filter/typecast/cast/TimestampCast.java
76
- - src/test/java/org/embulk/filter/TestTypecastFilterPlugin.java
77
- - classpath/embulk-filter-typecast-0.1.3.jar
81
+ - src/test/java/org/embulk/filter/typecast/cast/TestBooleanCast.java
82
+ - src/test/java/org/embulk/filter/typecast/cast/TestDoubleCast.java
83
+ - src/test/java/org/embulk/filter/typecast/cast/TestJsonCast.java
84
+ - src/test/java/org/embulk/filter/typecast/cast/TestLongCast.java
85
+ - src/test/java/org/embulk/filter/typecast/cast/TestStringCast.java
86
+ - src/test/java/org/embulk/filter/typecast/cast/TestTimestampCast.java
87
+ - classpath/embulk-filter-typecast-0.1.4.jar
78
88
  homepage: https://github.com/sonots/embulk-filter-typecast
79
89
  licenses:
80
90
  - MIT
@@ -1,5 +0,0 @@
1
- package org.embulk.filter;
2
-
3
- public class TestTypecastFilterPlugin
4
- {
5
- }