embulk-parser-jsonl 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -1
  3. data/README.md +17 -14
  4. data/bench/gen_dummy.rb +5 -0
  5. data/bench/typecast.yml +17 -0
  6. data/bench/without_typecast.yml +17 -0
  7. data/build.gradle +1 -1
  8. data/embulk-parser-jsonl.gemspec +1 -1
  9. data/example/compat.yml +21 -0
  10. data/example/example.yml +5 -10
  11. data/example/example_without_typecast.yml +18 -0
  12. data/example/sample.json +2 -2
  13. data/src/main/java/org/embulk/parser/jsonl/ColumnCaster.java +97 -0
  14. data/src/main/java/org/embulk/parser/jsonl/ColumnVisitorImpl.java +164 -0
  15. data/src/main/java/org/embulk/parser/jsonl/JsonRecordValidateException.java +11 -3
  16. data/src/main/java/org/embulk/parser/jsonl/JsonlParserPlugin.java +39 -14
  17. data/src/main/java/org/embulk/parser/jsonl/cast/BooleanCast.java +39 -0
  18. data/src/main/java/org/embulk/parser/jsonl/cast/DoubleCast.java +41 -0
  19. data/src/main/java/org/embulk/parser/jsonl/cast/JsonCast.java +40 -0
  20. data/src/main/java/org/embulk/parser/jsonl/cast/LongCast.java +47 -0
  21. data/src/main/java/org/embulk/parser/jsonl/cast/StringCast.java +82 -0
  22. data/src/test/java/org/embulk/parser/jsonl/TestColumnCaster.java +256 -0
  23. data/src/test/java/org/embulk/parser/jsonl/cast/TestBooleanCast.java +56 -0
  24. data/src/test/java/org/embulk/parser/jsonl/cast/TestDoubleCast.java +50 -0
  25. data/src/test/java/org/embulk/parser/jsonl/cast/TestJsonCast.java +80 -0
  26. data/src/test/java/org/embulk/parser/jsonl/cast/TestLongCast.java +42 -0
  27. data/src/test/java/org/embulk/parser/jsonl/cast/TestStringCast.java +103 -0
  28. metadata +21 -7
  29. data/src/main/java/org/embulk/parser/jsonl/JsonlColumnOption.java +0 -16
  30. data/src/main/java/org/embulk/parser/jsonl/getter/ColumnGetterFactory.java +0 -24
  31. data/src/main/java/org/embulk/parser/jsonl/getter/CommonColumnGetter.java +0 -131
  32. data/src/main/java/org/embulk/parser/jsonl/getter/StringColumnGetter.java +0 -68
@@ -0,0 +1,56 @@
1
+ package org.embulk.parser.jsonl.cast;
2
+
3
+ import org.embulk.spi.DataException;
4
+ import org.junit.Test;
5
+
6
+ import static org.junit.Assert.assertEquals;
7
+ import static org.junit.Assert.assertTrue;
8
+ import static org.junit.Assert.fail;
9
+
10
+ public class TestBooleanCast
11
+ {
12
+ @Test
13
+ public void asBoolean()
14
+ {
15
+ assertEquals(true, BooleanCast.asBoolean(true));
16
+ assertEquals(false, BooleanCast.asBoolean(false));
17
+ }
18
+
19
+ @Test
20
+ public void asLong()
21
+ {
22
+ assertEquals(1, BooleanCast.asLong(true));
23
+ assertEquals(0, BooleanCast.asLong(false));
24
+ }
25
+
26
+ @Test
27
+ public void asDouble()
28
+ {
29
+ try {
30
+ BooleanCast.asDouble(true);
31
+ fail();
32
+ }
33
+ catch (Throwable t) {
34
+ assertTrue(t instanceof DataException);
35
+ }
36
+ }
37
+
38
+ @Test
39
+ public void asString()
40
+ {
41
+ assertEquals("true", BooleanCast.asString(true));
42
+ assertEquals("false", BooleanCast.asString(false));
43
+ }
44
+
45
+ @Test
46
+ public void asTimestamp()
47
+ {
48
+ try {
49
+ BooleanCast.asTimestamp(true);
50
+ fail();
51
+ }
52
+ catch (Throwable t) {
53
+ assertTrue(t instanceof DataException);
54
+ }
55
+ }
56
+ }
@@ -0,0 +1,50 @@
1
+ package org.embulk.parser.jsonl.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
38
+ public void asString()
39
+ {
40
+ assertEquals("0.5", DoubleCast.asString(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
+ }
50
+
@@ -0,0 +1,80 @@
1
+ package org.embulk.parser.jsonl.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
+ import static org.junit.Assert.assertTrue;
11
+ import static org.junit.Assert.fail;
12
+
13
+ public class TestJsonCast
14
+ {
15
+ public Value value;
16
+
17
+ @Before
18
+ public void createResource()
19
+ {
20
+ Value[] kvs = new Value[2];
21
+ kvs[0] = ValueFactory.newString("k");
22
+ kvs[1] = ValueFactory.newString("v");
23
+ value = ValueFactory.newMap(kvs);
24
+ }
25
+
26
+ @Test
27
+ public void asBoolean()
28
+ {
29
+ try {
30
+ JsonCast.asBoolean(value);
31
+ fail();
32
+ }
33
+ catch (Throwable t) {
34
+ assertTrue(t instanceof DataException);
35
+ }
36
+ }
37
+
38
+ @Test
39
+ public void asLong()
40
+ {
41
+ try {
42
+ JsonCast.asLong(value);
43
+ fail();
44
+ }
45
+ catch (Throwable t) {
46
+ assertTrue(t instanceof DataException);
47
+ }
48
+ }
49
+
50
+ @Test
51
+ public void asDouble()
52
+ {
53
+ try {
54
+ JsonCast.asDouble(value);
55
+ fail();
56
+ }
57
+ catch (Throwable t) {
58
+ assertTrue(t instanceof DataException);
59
+ }
60
+ }
61
+
62
+ @Test
63
+ public void asString()
64
+ {
65
+ assertEquals("{\"k\":\"v\"}", JsonCast.asString(value));
66
+ }
67
+
68
+ @Test
69
+ public void asTimestamp()
70
+ {
71
+ try {
72
+ JsonCast.asTimestamp(value);
73
+ fail();
74
+ }
75
+ catch (Throwable t) {
76
+ assertTrue(t instanceof DataException);
77
+ }
78
+ }
79
+ }
80
+
@@ -0,0 +1,42 @@
1
+ package org.embulk.parser.jsonl.cast;
2
+
3
+ import org.embulk.spi.time.Timestamp;
4
+ import org.junit.Test;
5
+
6
+ import static org.junit.Assert.assertEquals;
7
+
8
+ public class TestLongCast
9
+ {
10
+ @Test
11
+ public void asBoolean()
12
+ {
13
+ assertEquals(true, LongCast.asBoolean(1));
14
+ assertEquals(false, LongCast.asBoolean(0));
15
+ }
16
+
17
+ @Test
18
+ public void asLong()
19
+ {
20
+ assertEquals(1, LongCast.asLong(1));
21
+ }
22
+
23
+ @Test
24
+ public void asDouble()
25
+ {
26
+ assertEquals(1.0, LongCast.asDouble(1), 0.0);
27
+ }
28
+
29
+ @Test
30
+ public void asString()
31
+ {
32
+ assertEquals("1", LongCast.asString(1));
33
+ }
34
+
35
+ @Test
36
+ public void asTimestamp()
37
+ {
38
+ Timestamp expected = Timestamp.ofEpochSecond(1);
39
+ assertEquals(expected, LongCast.asTimestamp(1));
40
+ }
41
+ }
42
+
@@ -0,0 +1,103 @@
1
+ package org.embulk.parser.jsonl.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
+
13
+ import static org.junit.Assert.assertEquals;
14
+ import static org.junit.Assert.assertTrue;
15
+ import static org.junit.Assert.fail;
16
+
17
+ public class TestStringCast
18
+ {
19
+ @Rule
20
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
21
+ public ScriptingContainer jruby;
22
+
23
+ @Before
24
+ public void createResource()
25
+ {
26
+ jruby = new ScriptingContainer();
27
+ }
28
+
29
+ @Test
30
+ public void asBoolean()
31
+ {
32
+ for (String str : StringCast.TRUE_STRINGS) {
33
+ assertEquals(true, StringCast.asBoolean(str));
34
+ }
35
+ for (String str : StringCast.FALSE_STRINGS) {
36
+ assertEquals(false, StringCast.asBoolean(str));
37
+ }
38
+ try {
39
+ StringCast.asBoolean("foo");
40
+ }
41
+ catch (Throwable t) {
42
+ assertTrue(t instanceof DataException);
43
+ }
44
+ }
45
+
46
+ @Test
47
+ public void asLong()
48
+ {
49
+ assertEquals(1, StringCast.asLong("1"));
50
+ try {
51
+ StringCast.asLong("1.5");
52
+ fail();
53
+ }
54
+ catch (Throwable t) {
55
+ assertTrue(t instanceof DataException);
56
+ }
57
+ try {
58
+ StringCast.asLong("foo");
59
+ fail();
60
+ }
61
+ catch (Throwable t) {
62
+ assertTrue(t instanceof DataException);
63
+ }
64
+ }
65
+
66
+ @Test
67
+ public void asDouble()
68
+ {
69
+ assertEquals(1.0, StringCast.asDouble("1"), 0.0);
70
+ assertEquals(1.5, StringCast.asDouble("1.5"), 0.0);
71
+ try {
72
+ StringCast.asDouble("foo");
73
+ fail();
74
+ }
75
+ catch (Throwable t) {
76
+ assertTrue(t instanceof DataException);
77
+ }
78
+ }
79
+
80
+ @Test
81
+ public void asString()
82
+ {
83
+ assertEquals("1", StringCast.asString("1"));
84
+ assertEquals("1.5", StringCast.asString("1.5"));
85
+ assertEquals("foo", StringCast.asString("foo"));
86
+ }
87
+
88
+ @Test
89
+ public void asTimestamp()
90
+ {
91
+ Timestamp expected = Timestamp.ofEpochSecond(1463084053, 123456000);
92
+ TimestampParser parser = new TimestampParser(jruby, "%Y-%m-%d %H:%M:%S.%N", DateTimeZone.UTC);
93
+ assertEquals(expected, StringCast.asTimestamp("2016-05-12 20:14:13.123456", parser));
94
+
95
+ try {
96
+ StringCast.asTimestamp("foo", parser);
97
+ fail();
98
+ }
99
+ catch (Throwable t) {
100
+ assertTrue(t instanceof DataException);
101
+ }
102
+ }
103
+ }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-parser-jsonl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shunsuke Mikami
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-27 00:00:00.000000000 Z
11
+ date: 2016-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -51,9 +51,14 @@ files:
51
51
  - LICENSE.txt
52
52
  - README.md
53
53
  - Rakefile
54
+ - bench/gen_dummy.rb
55
+ - bench/typecast.yml
56
+ - bench/without_typecast.yml
54
57
  - build.gradle
55
58
  - embulk-parser-jsonl.gemspec
59
+ - example/compat.yml
56
60
  - example/example.yml
61
+ - example/example_without_typecast.yml
57
62
  - example/sample.json
58
63
  - gradle/wrapper/gradle-wrapper.jar
59
64
  - gradle/wrapper/gradle-wrapper.properties
@@ -62,15 +67,24 @@ files:
62
67
  - lib/embulk/guess/jsonl.rb
63
68
  - lib/embulk/parser/jsonl.rb
64
69
  - settings.gradle
70
+ - src/main/java/org/embulk/parser/jsonl/ColumnCaster.java
71
+ - src/main/java/org/embulk/parser/jsonl/ColumnVisitorImpl.java
65
72
  - src/main/java/org/embulk/parser/jsonl/JsonRecordValidateException.java
66
- - src/main/java/org/embulk/parser/jsonl/JsonlColumnOption.java
67
73
  - src/main/java/org/embulk/parser/jsonl/JsonlParserPlugin.java
68
- - src/main/java/org/embulk/parser/jsonl/getter/ColumnGetterFactory.java
69
- - src/main/java/org/embulk/parser/jsonl/getter/CommonColumnGetter.java
70
- - src/main/java/org/embulk/parser/jsonl/getter/StringColumnGetter.java
74
+ - src/main/java/org/embulk/parser/jsonl/cast/BooleanCast.java
75
+ - src/main/java/org/embulk/parser/jsonl/cast/DoubleCast.java
76
+ - src/main/java/org/embulk/parser/jsonl/cast/JsonCast.java
77
+ - src/main/java/org/embulk/parser/jsonl/cast/LongCast.java
78
+ - src/main/java/org/embulk/parser/jsonl/cast/StringCast.java
79
+ - src/test/java/org/embulk/parser/jsonl/TestColumnCaster.java
71
80
  - src/test/java/org/embulk/parser/jsonl/TestJsonlParserPlugin.java
81
+ - src/test/java/org/embulk/parser/jsonl/cast/TestBooleanCast.java
82
+ - src/test/java/org/embulk/parser/jsonl/cast/TestDoubleCast.java
83
+ - src/test/java/org/embulk/parser/jsonl/cast/TestJsonCast.java
84
+ - src/test/java/org/embulk/parser/jsonl/cast/TestLongCast.java
85
+ - src/test/java/org/embulk/parser/jsonl/cast/TestStringCast.java
72
86
  - src/test/resources/org/embulk/parser/jsonl/use_column_options.yml
73
- - classpath/embulk-parser-jsonl-0.1.2.jar
87
+ - classpath/embulk-parser-jsonl-0.2.0.jar
74
88
  homepage: https://github.com/shun0102/embulk-parser-jsonl
75
89
  licenses:
76
90
  - MIT
@@ -1,16 +0,0 @@
1
- package org.embulk.parser.jsonl;
2
-
3
- import org.embulk.config.Config;
4
- import org.embulk.config.ConfigDefault;
5
- import org.embulk.config.Task;
6
- import org.embulk.spi.type.Type;
7
-
8
- import com.google.common.base.Optional;
9
-
10
- public interface JsonlColumnOption
11
- extends Task
12
- {
13
- @Config("type")
14
- @ConfigDefault("null")
15
- Optional<Type> getType();
16
- }
@@ -1,24 +0,0 @@
1
- package org.embulk.parser.jsonl.getter;
2
-
3
- import org.embulk.spi.ColumnConfig;
4
- import org.embulk.spi.PageBuilder;
5
- import org.embulk.spi.time.TimestampParser;
6
-
7
- public class ColumnGetterFactory {
8
- private PageBuilder pageBuilder;
9
- private TimestampParser[] timestampParsers;
10
-
11
- public ColumnGetterFactory(PageBuilder pageBuilder, TimestampParser[] timestampParsers) {
12
- this.pageBuilder = pageBuilder;
13
- this.timestampParsers = timestampParsers;
14
- }
15
-
16
- public CommonColumnGetter newColumnGetter(ColumnConfig columnConfig) {
17
- switch (columnConfig.getType().getName()) {
18
- case "string":
19
- return new StringColumnGetter(pageBuilder, timestampParsers);
20
- default:
21
- return new CommonColumnGetter(pageBuilder, timestampParsers);
22
- }
23
- }
24
- }
@@ -1,131 +0,0 @@
1
- package org.embulk.parser.jsonl.getter;
2
-
3
- import org.embulk.parser.jsonl.JsonRecordValidateException;
4
- import org.embulk.spi.Column;
5
- import org.embulk.spi.ColumnVisitor;
6
- import org.embulk.spi.PageBuilder;
7
- import org.embulk.spi.time.TimestampParser;
8
- import org.msgpack.core.MessageTypeException;
9
- import org.msgpack.value.BooleanValue;
10
- import org.msgpack.value.FloatValue;
11
- import org.msgpack.value.IntegerValue;
12
- import org.msgpack.value.Value;
13
-
14
- public class CommonColumnGetter implements ColumnVisitor {
15
- protected final PageBuilder pageBuilder;
16
- protected final TimestampParser[] timestampParsers;
17
-
18
- protected Value value;
19
-
20
- public CommonColumnGetter(PageBuilder pageBuilder, TimestampParser[] timestampParsers)
21
- {
22
- this.pageBuilder = pageBuilder;
23
- this.timestampParsers = timestampParsers;
24
- }
25
-
26
- public void setValue(Value value)
27
- {
28
- this.value = value;
29
- }
30
-
31
- @Override
32
- public void booleanColumn(Column column)
33
- {
34
- if (isNil(value)) {
35
- pageBuilder.setNull(column);
36
- }
37
- else {
38
- try {
39
- pageBuilder.setBoolean(column, ((BooleanValue) value).getBoolean());
40
- }
41
- catch (MessageTypeException e) {
42
- throw new JsonRecordValidateException(e);
43
- }
44
- }
45
- }
46
-
47
- @Override
48
- public void longColumn(Column column)
49
- {
50
- if (isNil(value)) {
51
- pageBuilder.setNull(column);
52
- }
53
- else {
54
- try {
55
- pageBuilder.setLong(column, ((IntegerValue) value).asLong());
56
- }
57
- catch (MessageTypeException e) {
58
- throw new JsonRecordValidateException(e);
59
- }
60
- }
61
- }
62
-
63
- @Override
64
- public void doubleColumn(Column column)
65
- {
66
- if (isNil(value)) {
67
- pageBuilder.setNull(column);
68
- }
69
- else {
70
- try {
71
- pageBuilder.setDouble(column, ((FloatValue) value).toDouble());
72
- }
73
- catch (MessageTypeException e) {
74
- throw new JsonRecordValidateException(e);
75
- }
76
- }
77
- }
78
-
79
- @Override
80
- public void stringColumn(Column column)
81
- {
82
- if (isNil(value)) {
83
- pageBuilder.setNull(column);
84
- }
85
- else {
86
- try {
87
- pageBuilder.setString(column, value.toString());
88
- }
89
- catch (MessageTypeException e) {
90
- throw new JsonRecordValidateException(e);
91
- }
92
- }
93
- }
94
-
95
- @Override
96
- public void timestampColumn(Column column)
97
- {
98
- if (isNil(value)) {
99
- pageBuilder.setNull(column);
100
- }
101
- else {
102
- try {
103
- pageBuilder.setTimestamp(column, timestampParsers[column.getIndex()].parse(value.toString()));
104
- }
105
- catch (MessageTypeException e) {
106
- throw new JsonRecordValidateException(e);
107
- }
108
- }
109
- }
110
-
111
- @Override
112
- public void jsonColumn(Column column)
113
- {
114
- if (isNil(value)) {
115
- pageBuilder.setNull(column);
116
- }
117
- else {
118
- try {
119
- pageBuilder.setJson(column, value);
120
- }
121
- catch (MessageTypeException e) {
122
- throw new JsonRecordValidateException(e);
123
- }
124
- }
125
- }
126
-
127
- protected boolean isNil(Value v)
128
- {
129
- return v == null || v.isNilValue();
130
- }
131
- }