embulk-filter-base58 0.1.0 → 0.1.1

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: 00cfc6ad1057cdbf2dc12e61707eae5b5dbdcb6b
4
- data.tar.gz: a0a1bbe1f72006006ecbbfd241691ddfaa485364
3
+ metadata.gz: 6f5608649f72f4f3f5aff5519f536738ada5b39f
4
+ data.tar.gz: 991a3136a80c49ca86b7bd38e75052fa74731d46
5
5
  SHA512:
6
- metadata.gz: 5a2b21c12d7381b0ac0263ceaef7da002f65c36081b6f8cd628b715e8907ff37461a1808d01162b8207bb14d6fa3131818b37d4b105f1bc71d86ca66d3a24fe4
7
- data.tar.gz: a95d96b3422597fa70a357081b67f885f3a9aa0622948a75c4888b859652c4c6f6f484083e641036bf9dc279579c3ff4c55c106ae78b8ba3f618a5834b64d9fb
6
+ metadata.gz: 75116c9073702c6afd9c7cd3120ff5dd408bfe9aee974a31fe0215723631d19aa10eb895b200e0656561e0aa2c1106dd98e808ecdd31fe2504a3cd3eabd919ca
7
+ data.tar.gz: 59a0b0fb1e295484db667b88c7dbf160ca395c22f0ca9ced0d9f19a79e4e916088155e624de130a205890a7af9e52dcf52783a8e7d4f8ab82a6690a06eebd1b4
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Base58 filter plugin for Embulk
2
2
 
3
+ [![Build Status](https://travis-ci.org/kfitzgerald/embulk-filter-base58.svg?branch=master)](https://travis-ci.org/kfitzgerald/embulk-filter-base58) [![Coverage Status](https://coveralls.io/repos/github/kfitzgerald/embulk-filter-base58/badge.svg?branch=master)](https://coveralls.io/github/kfitzgerald/embulk-filter-base58?branch=master)
4
+
3
5
  Embulk filter plugin to convert hex to base58 and vice versa.
4
6
 
5
7
  ## Overview
data/build.gradle CHANGED
@@ -2,6 +2,8 @@ plugins {
2
2
  id "com.jfrog.bintray" version "1.1"
3
3
  id "com.github.jruby-gradle.base" version "0.1.5"
4
4
  id "java"
5
+ id "jacoco"
6
+ id "com.github.kt3k.coveralls" version "2.4.0"
5
7
  }
6
8
  import com.github.jrubygradle.JRubyExec
7
9
  repositories {
@@ -12,13 +14,22 @@ configurations {
12
14
  provided
13
15
  }
14
16
 
15
- version = "0.1.0"
17
+ version = "0.1.1"
16
18
 
17
19
  dependencies {
18
- compile "org.embulk:embulk-core:0.8.8"
19
- provided "org.embulk:embulk-core:0.8.8"
20
+ compile "org.embulk:embulk-core:0.8.+"
21
+ provided "org.embulk:embulk-core:0.8.+"
20
22
  // compile "YOUR_JAR_DEPENDENCY_GROUP:YOUR_JAR_DEPENDENCY_MODULE:YOUR_JAR_DEPENDENCY_VERSION"
21
23
  testCompile "junit:junit:4.+"
24
+ testCompile "org.embulk:embulk-core:0.8.+:tests"
25
+ testCompile "org.embulk:embulk-standards:0.8.+"
26
+ }
27
+
28
+ jacocoTestReport {
29
+ reports {
30
+ xml.enabled = true // coveralls plugin depends on xml format report
31
+ html.enabled = true
32
+ }
22
33
  }
23
34
 
24
35
  task classpath(type: Copy, dependsOn: ["jar"]) {
@@ -2,27 +2,27 @@ package org.embulk.filter.base58;
2
2
 
3
3
  import java.math.BigInteger;
4
4
 
5
- public class Base58
5
+ final class Base58
6
6
  {
7
7
  private Base58()
8
8
  {
9
9
  }
10
10
 
11
- private static final String base58Chars = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; // Base-58 char library
11
+ private static final String BASE_58_CHARS = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; // Base-58 char library
12
+ private static final BigInteger ZERO = new BigInteger("0");
13
+ private static final BigInteger FIFTY_EIGHT = new BigInteger("58");
12
14
 
13
15
  public static String encode(String hex)
14
16
  {
15
17
  String originalHex = hex;
16
18
 
17
19
  BigInteger numeric = new BigInteger(hex, 16);
18
- BigInteger zero = new BigInteger("0");
19
- BigInteger fifty58 = new BigInteger("58");
20
20
  String output = "";
21
21
 
22
- while (numeric.compareTo(zero) == 1) {
23
- BigInteger remainder = numeric.mod(fifty58);
24
- numeric = numeric.divide(fifty58);
25
- output = base58Chars.charAt(Integer.parseInt(remainder.toString())) + output;
22
+ while (numeric.compareTo(ZERO) == 1) {
23
+ BigInteger remainder = numeric.mod(FIFTY_EIGHT);
24
+ numeric = numeric.divide(FIFTY_EIGHT);
25
+ output = BASE_58_CHARS.charAt(Integer.parseInt(remainder.toString())) + output;
26
26
  }
27
27
 
28
28
  //leading zeros
@@ -39,15 +39,14 @@ public class Base58
39
39
 
40
40
  // Ignore bogus base58 strings
41
41
  if (base58Value.matches("[^1-9A-HJ-NP-Za-km-z]")) {
42
- return "";
42
+ return null;
43
43
  }
44
44
 
45
45
  BigInteger output = new BigInteger("0");
46
- BigInteger fifty58 = new BigInteger("58");
47
46
 
48
47
  for (int i = 0; i < base58Value.length(); i++) {
49
- int current = base58Chars.indexOf(base58Value.charAt(i));
50
- output = output.multiply(fifty58).add(new BigInteger(current + ""));
48
+ int current = BASE_58_CHARS.indexOf(base58Value.charAt(i));
49
+ output = output.multiply(FIFTY_EIGHT).add(new BigInteger(current + ""));
51
50
  }
52
51
 
53
52
  String hex = output.toString(16);
@@ -71,6 +70,6 @@ public class Base58
71
70
 
72
71
  public static String decodeWithPrefix(String baseValue, String prefix)
73
72
  {
74
- return decode(baseValue.substring(prefix.length()));
73
+ return decode(baseValue.replace(prefix, ""));
75
74
  }
76
75
  }
@@ -1,7 +1,6 @@
1
1
  package org.embulk.filter.base58;
2
2
 
3
3
  import com.google.common.base.Optional;
4
- import com.google.common.base.Throwables;
5
4
  import com.google.common.collect.ImmutableList;
6
5
  import org.embulk.config.Config;
7
6
  import org.embulk.config.ConfigDefault;
@@ -42,7 +41,7 @@ public class Base58FilterPlugin implements FilterPlugin {
42
41
  Optional<Boolean> getIsEncode();
43
42
 
44
43
  @Config("prefix")
45
- @ConfigDefault("")
44
+ @ConfigDefault("null")
46
45
  Optional<String> getPrefix();
47
46
 
48
47
  @Config("new_name")
@@ -52,8 +51,12 @@ public class Base58FilterPlugin implements FilterPlugin {
52
51
 
53
52
  @Override
54
53
  public void transaction(ConfigSource config, Schema inputSchema, FilterPlugin.Control control) {
55
-
56
54
  PluginTask task = config.loadConfig(PluginTask.class);
55
+ Schema outputSchema = buildOutputSchema(task, inputSchema);
56
+ control.run(task.dump(), outputSchema);
57
+ }
58
+
59
+ Schema buildOutputSchema(PluginTask task, Schema inputSchema) {
57
60
  ImmutableList.Builder<Column> builder = ImmutableList.builder();
58
61
 
59
62
  // Roll through original columns
@@ -75,8 +78,7 @@ public class Base58FilterPlugin implements FilterPlugin {
75
78
  }
76
79
  }
77
80
 
78
- Schema outputSchema = new Schema(builder.build());
79
- control.run(task.dump(), outputSchema);
81
+ return new Schema(builder.build());
80
82
  }
81
83
 
82
84
  @Override
@@ -95,110 +97,11 @@ public class Base58FilterPlugin implements FilterPlugin {
95
97
  public void add(Page page) {
96
98
  reader.setPage(page);
97
99
  while (reader.nextRecord()) {
98
- setValue();
100
+ setValue(base58ColumnMap, outputColumnMap, reader, outputSchema, builder);
99
101
  builder.addRecord();
100
102
  }
101
103
  }
102
104
 
103
- private void setValue() {
104
-
105
- // Do base58 conversions ahead of iterating output columns
106
- final Map<String, String> base58OutputMap = new HashMap<>();;
107
- for (Base58Column column : base58ColumnMap.values()) {
108
- String inputValue, convertedValue = null;
109
-
110
- // If the original column is not a string, then forget about it (it should be hex)
111
- Column originalColumn = outputColumnMap.get(column.getName());
112
- if (Types.STRING.equals(originalColumn.getType())) {
113
- inputValue = reader.getString(originalColumn);
114
- } else {
115
- logger.error("cannot convert base58 value of non-string values. name: {}, type: {}, index: {}",
116
- originalColumn.getName(),
117
- originalColumn.getType(),
118
- originalColumn.getIndex());
119
- throw new DataException("Unexpected string type in column `"+originalColumn.getName()+"`. Got: " + originalColumn.getType());
120
- }
121
-
122
- // Convert the value
123
- try {
124
- convertedValue = convertValue(inputValue, column.getIsEncode().get(), column.getPrefix().get());
125
- } catch (Exception e) {
126
- // Failed to do the conversion. Probably misconfigured or malformed value
127
- logger.error("failed to encode/decode base58 column value. name: {}, type: {}, index: {}, value: {}, method: {}, prefix: {}, target_name: {}",
128
- originalColumn.getName(),
129
- originalColumn.getType(),
130
- originalColumn.getIndex(),
131
- inputValue,
132
- column.getIsEncode().get() ? "encode" : "decode",
133
- column.getPrefix(),
134
- column.getNewName().or(column.getName()));
135
- // Don't crash the import if a single value is screwed up. Just log it for now
136
- }
137
-
138
- // Add it to the output mappings
139
- if (column.getNewName().isPresent()) {
140
- base58OutputMap.put(column.getNewName().get(), convertedValue);
141
- } else {
142
- base58OutputMap.put(column.getName(), convertedValue);
143
- }
144
- }
145
-
146
- List<Column> columns = outputSchema.getColumns();
147
- for (Column outputColumn : columns) {
148
-
149
- // Did we convert the value for this column?
150
- if (base58OutputMap.containsKey(outputColumn.getName())) {
151
- String value = base58OutputMap.get(outputColumn.getName());
152
- if (value == null) {
153
- builder.setNull(outputColumn);
154
- } else {
155
- builder.setString(outputColumn, base58OutputMap.get(outputColumn.getName()));
156
- }
157
- continue;
158
- }
159
-
160
- // No value?
161
- if (reader.isNull(outputColumn)) {
162
- builder.setNull(outputColumn);
163
- continue;
164
- }
165
-
166
- // Inherit value
167
- if (Types.STRING.equals(outputColumn.getType())) {
168
- builder.setString(outputColumn, reader.getString(outputColumn));
169
- }
170
- else if (Types.BOOLEAN.equals(outputColumn.getType())) {
171
- builder.setBoolean(outputColumn, reader.getBoolean(outputColumn));
172
- }
173
- else if (Types.DOUBLE.equals(outputColumn.getType())) {
174
- builder.setDouble(outputColumn, reader.getDouble(outputColumn));
175
- }
176
- else if (Types.LONG.equals(outputColumn.getType())) {
177
- builder.setLong(outputColumn, reader.getLong(outputColumn));
178
- }
179
- else if (Types.TIMESTAMP.equals(outputColumn.getType())) {
180
- builder.setTimestamp(outputColumn, reader.getTimestamp(outputColumn));
181
- }
182
- else if (Types.JSON.equals(outputColumn.getType())) {
183
- builder.setJson(outputColumn, reader.getJson(outputColumn));
184
- }
185
- }
186
- }
187
-
188
- private String convertValue(String value, Boolean isEncode, String prefix) {
189
- String result = null;
190
- try {
191
- if (isEncode) {
192
- result = Base58.encodeWithPrefix(value, prefix);
193
- } else {
194
- result = Base58.decodeWithPrefix(value, prefix);
195
- }
196
- } catch (Exception e) {
197
- Throwables.propagate(e);
198
- }
199
- return result;
200
- }
201
-
202
105
  @Override
203
106
  public void finish() {
204
107
  builder.finish();
@@ -211,7 +114,101 @@ public class Base58FilterPlugin implements FilterPlugin {
211
114
  };
212
115
  }
213
116
 
214
- private static Map<String, Base58Column> convertBase58ColumnListToMap(List<Base58Column> base58Columns) {
117
+ void setValue(final Map<String, Base58Column> base58ColumnMap, final Map<String, Column> outputColumnMap, final PageReader reader, final Schema outputSchema, final PageBuilder builder) {
118
+
119
+ // Do base58 conversions ahead of iterating output columns
120
+ final Map<String, String> base58OutputMap = new HashMap<>();;
121
+ for (Base58Column column : base58ColumnMap.values()) {
122
+ String inputValue, convertedValue = null;
123
+
124
+ // If the original column is not a string, then forget about it (it should be hex)
125
+ Column originalColumn = outputColumnMap.get(column.getName());
126
+ if (Types.STRING.equals(originalColumn.getType())) {
127
+ inputValue = reader.getString(originalColumn);
128
+ } else {
129
+ logger.error("cannot convert base58 value of non-string values. name: {}, type: {}, index: {}",
130
+ originalColumn.getName(),
131
+ originalColumn.getType(),
132
+ originalColumn.getIndex());
133
+ throw new DataException("Unexpected string type in column `"+originalColumn.getName()+"`. Got: " + originalColumn.getType());
134
+ }
135
+
136
+ // Convert the value
137
+ try {
138
+ convertedValue = convertValue(inputValue, column.getIsEncode().or(true), column.getPrefix().or(""));
139
+ } catch (Exception e) {
140
+ // Failed to do the conversion. Probably misconfigured or malformed value
141
+ logger.error("failed to encode/decode base58 column value. name: {}, type: {}, index: {}, value: {}, method: {}, prefix: {}, target_name: {}",
142
+ originalColumn.getName(),
143
+ originalColumn.getType(),
144
+ originalColumn.getIndex(),
145
+ inputValue,
146
+ column.getIsEncode().get() ? "encode" : "decode",
147
+ column.getPrefix(),
148
+ column.getNewName().or(column.getName()));
149
+ logger.error("base58 conversion exception", e);
150
+ // Don't crash the import if a single value is screwed up. Just log it for now
151
+ }
152
+
153
+ // Add it to the output mappings
154
+ if (column.getNewName().isPresent()) {
155
+ base58OutputMap.put(column.getNewName().get(), convertedValue);
156
+ } else {
157
+ base58OutputMap.put(column.getName(), convertedValue);
158
+ }
159
+ }
160
+
161
+ List<Column> columns = outputSchema.getColumns();
162
+ for (Column outputColumn : columns) {
163
+
164
+ // Did we convert the value for this column?
165
+ if (base58OutputMap.containsKey(outputColumn.getName())) {
166
+ String value = base58OutputMap.get(outputColumn.getName());
167
+ if (value == null) {
168
+ builder.setNull(outputColumn);
169
+ } else {
170
+ builder.setString(outputColumn, base58OutputMap.get(outputColumn.getName()));
171
+ }
172
+ continue;
173
+ }
174
+
175
+ // No value?
176
+ if (reader.isNull(outputColumn)) {
177
+ builder.setNull(outputColumn);
178
+ continue;
179
+ }
180
+
181
+ // Inherit value
182
+ if (Types.STRING.equals(outputColumn.getType())) {
183
+ builder.setString(outputColumn, reader.getString(outputColumn));
184
+ }
185
+ else if (Types.BOOLEAN.equals(outputColumn.getType())) {
186
+ builder.setBoolean(outputColumn, reader.getBoolean(outputColumn));
187
+ }
188
+ else if (Types.DOUBLE.equals(outputColumn.getType())) {
189
+ builder.setDouble(outputColumn, reader.getDouble(outputColumn));
190
+ }
191
+ else if (Types.LONG.equals(outputColumn.getType())) {
192
+ builder.setLong(outputColumn, reader.getLong(outputColumn));
193
+ }
194
+ else if (Types.TIMESTAMP.equals(outputColumn.getType())) {
195
+ builder.setTimestamp(outputColumn, reader.getTimestamp(outputColumn));
196
+ }
197
+ else if (Types.JSON.equals(outputColumn.getType())) {
198
+ builder.setJson(outputColumn, reader.getJson(outputColumn));
199
+ }
200
+ }
201
+ }
202
+
203
+ String convertValue(String value, Boolean isEncode, String prefix) {
204
+ if (isEncode) {
205
+ return Base58.encodeWithPrefix(value, prefix);
206
+ } else {
207
+ return Base58.decodeWithPrefix(value, prefix);
208
+ }
209
+ }
210
+
211
+ static Map<String, Base58Column> convertBase58ColumnListToMap(List<Base58Column> base58Columns) {
215
212
  Map<String, Base58Column> result = new HashMap<>();
216
213
  for (Base58Column base58Column : base58Columns) {
217
214
  result.put(base58Column.getName(), base58Column);
@@ -219,7 +216,7 @@ public class Base58FilterPlugin implements FilterPlugin {
219
216
  return result;
220
217
  }
221
218
 
222
- private static Map<String, Column> convertColumnListToMap(List<Column> columns) {
219
+ static Map<String, Column> convertColumnListToMap(List<Column> columns) {
223
220
  Map<String, Column> result = new HashMap<>();
224
221
  for (Column column : columns) {
225
222
  result.put(column.getName(), column);
@@ -0,0 +1,332 @@
1
+ package org.embulk.filter.base58;
2
+
3
+ import org.embulk.EmbulkTestRuntime;
4
+ import org.embulk.filter.base58.Base58FilterPlugin.PluginTask;
5
+ import org.embulk.config.ConfigLoader;
6
+ import org.embulk.config.ConfigSource;
7
+ import org.embulk.spi.*;
8
+ import org.embulk.spi.TestPageBuilderReader.MockPageOutput;
9
+ import org.embulk.spi.time.Timestamp;
10
+ import org.embulk.spi.util.Pages;
11
+ import org.junit.Before;
12
+ import org.junit.Rule;
13
+ import org.junit.Test;
14
+ import org.msgpack.value.ValueFactory;
15
+
16
+ import static org.embulk.spi.type.Types.BOOLEAN;
17
+ import static org.embulk.spi.type.Types.DOUBLE;
18
+ import static org.embulk.spi.type.Types.JSON;
19
+ import static org.embulk.spi.type.Types.LONG;
20
+ import static org.embulk.spi.type.Types.STRING;
21
+ import static org.embulk.spi.type.Types.TIMESTAMP;
22
+ import static org.junit.Assert.assertEquals;
23
+
24
+ import java.util.List;
25
+ import java.util.Map;
26
+
27
+ public class TestBase58FilterImpl {
28
+
29
+ @Rule
30
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
31
+
32
+ private Base58FilterPlugin plugin;
33
+
34
+ @Before
35
+ public void createResource()
36
+ {
37
+ plugin = new Base58FilterPlugin();
38
+ }
39
+
40
+ private PluginTask taskFromYamlString(String... lines)
41
+ {
42
+ StringBuilder builder = new StringBuilder();
43
+ for (String line : lines) {
44
+ builder.append(line).append("\n");
45
+ }
46
+ String yamlString = builder.toString();
47
+
48
+ ConfigLoader loader = new ConfigLoader(Exec.getModelManager());
49
+ ConfigSource config = loader.fromYamlString(yamlString);
50
+ return config.loadConfig(PluginTask.class);
51
+ }
52
+
53
+ private List<Object[]> filter(PluginTask task, Schema inputSchema, Object ... objects)
54
+ {
55
+ MockPageOutput output = new MockPageOutput();
56
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
57
+ PageBuilder pageBuilder = new PageBuilder(runtime.getBufferAllocator(), outputSchema, output);
58
+ PageReader pageReader = new PageReader(inputSchema);
59
+ final Map<String, Base58FilterPlugin.Base58Column> base58ColumnMap = Base58FilterPlugin.convertBase58ColumnListToMap(task.getColumns());
60
+ final Map<String, Column> outputColumnMap = Base58FilterPlugin.convertColumnListToMap(outputSchema.getColumns());
61
+
62
+ List<Page> pages = PageTestUtils.buildPage(runtime.getBufferAllocator(), inputSchema, objects);
63
+ for (Page page : pages) {
64
+ pageReader.setPage(page);
65
+
66
+ while (pageReader.nextRecord()) {
67
+ plugin.setValue(base58ColumnMap, outputColumnMap, pageReader, outputSchema, pageBuilder);
68
+ pageBuilder.addRecord();
69
+ }
70
+ }
71
+ pageBuilder.finish();
72
+ pageBuilder.close();
73
+ return Pages.toObjects(outputSchema, output.pages);
74
+ }
75
+
76
+ @Test
77
+ public void basicEncoding()
78
+ {
79
+ PluginTask task = taskFromYamlString(
80
+ "type: base58",
81
+ "columns:",
82
+ " - {name: _id}");
83
+ Schema inputSchema = Schema.builder()
84
+ .add("_id", STRING)
85
+ .build();
86
+
87
+ List<Object[]> records = filter(task, inputSchema,
88
+ "54f5f8b37c158c2f12ee1c64");
89
+
90
+ assertEquals(1, records.size());
91
+
92
+ Object[] record;
93
+ {
94
+ record = records.get(0);
95
+ assertEquals(1, record.length);
96
+ assertEquals("2bzSwY8SCsogbNxZZ", record[0]);
97
+ }
98
+ }
99
+
100
+ @Test
101
+ public void basicDecoding()
102
+ {
103
+ PluginTask task = taskFromYamlString(
104
+ "type: base58",
105
+ "columns:",
106
+ " - {name: public_id, encode: false}");
107
+ Schema inputSchema = Schema.builder()
108
+ .add("public_id", STRING)
109
+ .build();
110
+
111
+ List<Object[]> records = filter(task, inputSchema,
112
+ "2bzSwY8SCsogbNxZZ");
113
+
114
+ assertEquals(1, records.size());
115
+
116
+ Object[] record;
117
+ {
118
+ record = records.get(0);
119
+ assertEquals(1, record.length);
120
+ assertEquals("54f5f8b37c158c2f12ee1c64", record[0]);
121
+ }
122
+ }
123
+
124
+
125
+
126
+ @Test
127
+ public void playsNiceWithOtherTypes()
128
+ {
129
+ PluginTask task = taskFromYamlString(
130
+ "type: base58",
131
+ "columns:",
132
+ " - {name: _id}");
133
+ Schema inputSchema = Schema.builder()
134
+ .add("_id", STRING)
135
+ .add("created", TIMESTAMP)
136
+ .add("updated", TIMESTAMP)
137
+ .add("is_dead", BOOLEAN)
138
+ .add("count", LONG)
139
+ .add("price", DOUBLE)
140
+ .add("meta", JSON)
141
+ .build();
142
+
143
+ List<Object[]> records = filter(task, inputSchema,
144
+ "54f5f8b37c158c2f12ee1c64", Timestamp.ofEpochSecond(0), null, new Boolean(true), new Long(0), new Double(0.5), ValueFactory.newString("json"));
145
+
146
+ assertEquals(1, records.size());
147
+
148
+ Object[] record;
149
+ {
150
+ record = records.get(0);
151
+ assertEquals(7, record.length);
152
+ assertEquals("2bzSwY8SCsogbNxZZ", record[0]);
153
+ assertEquals(Timestamp.ofEpochSecond(0), record[1]);
154
+ assertEquals(null, record[2]);
155
+ assertEquals(new Boolean(true), record[3]);
156
+ assertEquals(new Long(0), record[4]);
157
+ assertEquals(new Double(0.5), record[5]);
158
+ assertEquals(ValueFactory.newString("json"), record[6]);
159
+ }
160
+ }
161
+
162
+ @Test(expected = org.embulk.spi.DataException.class)
163
+ public void doesNotWorkOnNonStringColumn()
164
+ {
165
+ PluginTask task = taskFromYamlString(
166
+ "type: base58",
167
+ "columns:",
168
+ " - {name: not_id}");
169
+ Schema inputSchema = Schema.builder()
170
+ .add("not_id", BOOLEAN)
171
+ .build();
172
+
173
+ filter(task, inputSchema,
174
+ new Boolean(true));
175
+ }
176
+
177
+ @Test
178
+ public void basicEncodingWithPrefix()
179
+ {
180
+ PluginTask task = taskFromYamlString(
181
+ "type: base58",
182
+ "columns:",
183
+ " - {name: _id, prefix: obj_}");
184
+ Schema inputSchema = Schema.builder()
185
+ .add("_id", STRING)
186
+ .build();
187
+
188
+ List<Object[]> records = filter(task, inputSchema,
189
+ "54f5f8b37c158c2f12ee1c64");
190
+
191
+ assertEquals(1, records.size());
192
+
193
+ Object[] record;
194
+ {
195
+ record = records.get(0);
196
+ assertEquals(1, record.length);
197
+ assertEquals("obj_2bzSwY8SCsogbNxZZ", record[0]);
198
+ }
199
+ }
200
+
201
+ @Test
202
+ public void basicDecodingWithPrefix()
203
+ {
204
+ PluginTask task = taskFromYamlString(
205
+ "type: base58",
206
+ "columns:",
207
+ " - {name: public_id, encode: false, prefix: obj_}");
208
+ Schema inputSchema = Schema.builder()
209
+ .add("public_id", STRING)
210
+ .build();
211
+
212
+ List<Object[]> records = filter(task, inputSchema,
213
+ "obj_2bzSwY8SCsogbNxZZ");
214
+
215
+ assertEquals(1, records.size());
216
+
217
+ Object[] record;
218
+ {
219
+ record = records.get(0);
220
+ assertEquals(1, record.length);
221
+ assertEquals("54f5f8b37c158c2f12ee1c64", record[0]);
222
+ }
223
+ }
224
+
225
+ @Test
226
+ public void basicEncodingWithPrefixAndNewName()
227
+ {
228
+ PluginTask task = taskFromYamlString(
229
+ "type: base58",
230
+ "columns:",
231
+ " - {name: _id, prefix: obj_, new_name: public_id}");
232
+ Schema inputSchema = Schema.builder()
233
+ .add("_id", STRING)
234
+ .build();
235
+
236
+ List<Object[]> records = filter(task, inputSchema,
237
+ "00f5f8b37c158c2f12ee1c64");
238
+
239
+ assertEquals(1, records.size());
240
+
241
+ Object[] record;
242
+ {
243
+ record = records.get(0);
244
+ assertEquals(2, record.length);
245
+ assertEquals("00f5f8b37c158c2f12ee1c64", record[0]);
246
+ assertEquals("obj_123zhNEUWPr5ogRQP", record[1]);
247
+ }
248
+ }
249
+
250
+ @Test
251
+ public void basicDecodingWithPrefixAndNewName()
252
+ {
253
+ PluginTask task = taskFromYamlString(
254
+ "type: base58",
255
+ "columns:",
256
+ " - {name: public_id, encode: false, prefix: obj_, new_name: _id}");
257
+ Schema inputSchema = Schema.builder()
258
+ .add("public_id", STRING)
259
+ .build();
260
+
261
+ List<Object[]> records = filter(task, inputSchema,
262
+ "obj_123zhNEUWPr5ogRQP");
263
+
264
+ assertEquals(1, records.size());
265
+
266
+ Object[] record;
267
+ {
268
+ record = records.get(0);
269
+ assertEquals(2, record.length);
270
+ assertEquals("obj_123zhNEUWPr5ogRQP", record[0]);
271
+ assertEquals("00f5f8b37c158c2f12ee1c64", record[1]);
272
+ }
273
+ }
274
+
275
+ @Test
276
+ public void badBase58DecodeTurnsColumnValueNull()
277
+ {
278
+ PluginTask task = taskFromYamlString(
279
+ "type: base58",
280
+ "columns:",
281
+ " - {name: public_id, encode: false}");
282
+ Schema inputSchema = Schema.builder()
283
+ .add("public_id", STRING)
284
+ .build();
285
+
286
+ List<Object[]> records = filter(task, inputSchema,
287
+ "I");
288
+
289
+ assertEquals(1, records.size());
290
+
291
+ Object[] record;
292
+ {
293
+ record = records.get(0);
294
+ assertEquals(1, record.length);
295
+ assertEquals(null, record[0]);
296
+ }
297
+ }
298
+
299
+ @Test
300
+ public void badHexValueTurnsNull()
301
+ {
302
+ PluginTask task = taskFromYamlString(
303
+ "type: base58",
304
+ "columns:",
305
+ " - {name: _id, encode: true}");
306
+ Schema inputSchema = Schema.builder()
307
+ .add("_id", STRING)
308
+ .build();
309
+
310
+ List<Object[]> records = filter(task, inputSchema,
311
+ "nope");
312
+
313
+ assertEquals(1, records.size());
314
+
315
+ Object[] record;
316
+ {
317
+ record = records.get(0);
318
+ assertEquals(1, record.length);
319
+ assertEquals(null, record[0]);
320
+ }
321
+ }
322
+
323
+ @Test
324
+ public void base58EdgeCases()
325
+ {
326
+ assertEquals("00", Base58.decode(""));
327
+ assertEquals(null, Base58.decode("I"));
328
+ assertEquals("1", Base58.encode("00"));
329
+ assertEquals("2", Base58.encode("01"));
330
+ }
331
+
332
+ }
@@ -1,27 +1,177 @@
1
1
  package org.embulk.filter.base58;
2
2
 
3
- //import com.fasterxml.jackson.databind.ObjectMapper;
4
- //import com.google.inject.AbstractModule;
5
- //import com.google.inject.Guice;
6
- //import org.embulk.EmbulkEmbed;
7
- //import org.embulk.config.ConfigLoader;
8
- //import org.embulk.config.ModelManager;
9
- //import org.junit.Test;
10
- //
11
- //import java.io.File;
12
- //import java.io.IOException;
3
+ import org.embulk.EmbulkTestRuntime;
4
+ import org.embulk.filter.base58.Base58FilterPlugin.PluginTask;
5
+ import org.embulk.config.ConfigLoader;
6
+ import org.embulk.config.ConfigSource;
7
+ import org.embulk.spi.Column;
8
+ import org.embulk.spi.Exec;
9
+ import org.embulk.spi.Schema;
10
+ import org.junit.Before;
11
+ import org.junit.Rule;
12
+ import org.junit.Test;
13
+
14
+ import static org.embulk.spi.type.Types.STRING;
15
+ import static org.junit.Assert.assertEquals;
13
16
 
14
17
  public class TestBase58FilterPlugin
15
18
  {
16
- // @Test
17
- // public void test() throws IOException {
18
- // EmbulkEmbed embed = new EmbulkEmbed.Bootstrap().initialize();
19
- // ConfigLoader loader = new ConfigLoader(new ModelManager(Guice.createInjector(new AbstractModule() {
20
- // @Override
21
- // protected void configure() {}
22
- // }),
23
- // new ObjectMapper()
24
- // ));
25
- // embed.run(loader.fromYamlFile(new File("./test.yml")));
26
- // }
19
+ @Rule
20
+ public EmbulkTestRuntime runtime = new EmbulkTestRuntime();
21
+
22
+ private Base58FilterPlugin plugin;
23
+
24
+ @Before
25
+ public void createResource()
26
+ {
27
+ plugin = new Base58FilterPlugin();
28
+ }
29
+
30
+ private ConfigSource configFromYamlString(String... lines)
31
+ {
32
+ StringBuilder builder = new StringBuilder();
33
+ for (String line : lines) {
34
+ builder.append(line).append("\n");
35
+ }
36
+ String yamlString = builder.toString();
37
+
38
+ ConfigLoader loader = new ConfigLoader(Exec.getModelManager());
39
+ return loader.fromYamlString(yamlString);
40
+ }
41
+
42
+ private PluginTask taskFromYamlString(String... lines)
43
+ {
44
+ ConfigSource config = configFromYamlString(lines);
45
+ return config.loadConfig(PluginTask.class);
46
+ }
47
+
48
+ @Test
49
+ public void buildOutputSchema_Columns()
50
+ {
51
+ PluginTask task = taskFromYamlString(
52
+ "type: base58",
53
+ "columns:",
54
+ " - {name: _id}");
55
+ Schema inputSchema = Schema.builder()
56
+ .add("_id", STRING)
57
+ .build();
58
+
59
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
60
+ assertEquals(1, outputSchema.size());
61
+
62
+ Column column;
63
+ {
64
+ column = outputSchema.getColumn(0);
65
+ assertEquals("_id", column.getName());
66
+ }
67
+ }
68
+
69
+ @Test
70
+ public void buildOutputSchemaDecode_Columns()
71
+ {
72
+ PluginTask task = taskFromYamlString(
73
+ "type: base58",
74
+ "columns:",
75
+ " - {name: public_id, encode: false}");
76
+ Schema inputSchema = Schema.builder()
77
+ .add("public_id", STRING)
78
+ .build();
79
+
80
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
81
+ assertEquals(1, outputSchema.size());
82
+
83
+ Column column;
84
+ {
85
+ column = outputSchema.getColumn(0);
86
+ assertEquals("public_id", column.getName());
87
+ }
88
+ }
89
+
90
+ @Test
91
+ public void buildOutputSchemaPrefix_Columns()
92
+ {
93
+ PluginTask task = taskFromYamlString(
94
+ "type: base58",
95
+ "columns:",
96
+ " - {name: _id, prefix: obj_}");
97
+ Schema inputSchema = Schema.builder()
98
+ .add("_id", STRING)
99
+ .build();
100
+
101
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
102
+ assertEquals(1, outputSchema.size());
103
+
104
+ Column column;
105
+ {
106
+ column = outputSchema.getColumn(0);
107
+ assertEquals("_id", column.getName());
108
+ }
109
+ }
110
+
111
+ @Test
112
+ public void buildOutputSchemaDecodePrefix_Columns()
113
+ {
114
+ PluginTask task = taskFromYamlString(
115
+ "type: base58",
116
+ "columns:",
117
+ " - {name: public_id, encode: false, prefix: obj_}");
118
+ Schema inputSchema = Schema.builder()
119
+ .add("public_id", STRING)
120
+ .build();
121
+
122
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
123
+ assertEquals(1, outputSchema.size());
124
+
125
+ Column column;
126
+ {
127
+ column = outputSchema.getColumn(0);
128
+ assertEquals("public_id", column.getName());
129
+ }
130
+ }
131
+
132
+ @Test
133
+ public void buildOutputSchemaPrefixNewColumn_Columns()
134
+ {
135
+ PluginTask task = taskFromYamlString(
136
+ "type: base58",
137
+ "columns:",
138
+ " - {name: _id, prefix: obj_, new_name: public_id}");
139
+ Schema inputSchema = Schema.builder()
140
+ .add("_id", STRING)
141
+ .build();
142
+
143
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
144
+ assertEquals(2, outputSchema.size());
145
+
146
+ Column column;
147
+ {
148
+ column = outputSchema.getColumn(0);
149
+ assertEquals("_id", column.getName());
150
+ column = outputSchema.getColumn(1);
151
+ assertEquals("public_id", column.getName());
152
+ }
153
+ }
154
+
155
+ @Test
156
+ public void buildOutputSchemaDecodePrefixNewColumn_Columns()
157
+ {
158
+ PluginTask task = taskFromYamlString(
159
+ "type: base58",
160
+ "columns:",
161
+ " - {name: public_id, encode: false, prefix: obj_, new_name: _id}");
162
+ Schema inputSchema = Schema.builder()
163
+ .add("public_id", STRING)
164
+ .build();
165
+
166
+ Schema outputSchema = plugin.buildOutputSchema(task, inputSchema);
167
+ assertEquals(2, outputSchema.size());
168
+
169
+ Column column;
170
+ {
171
+ column = outputSchema.getColumn(0);
172
+ assertEquals("public_id", column.getName());
173
+ column = outputSchema.getColumn(1);
174
+ assertEquals("_id", column.getName());
175
+ }
176
+ }
27
177
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: embulk-filter-base58
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Fitzgerald
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-16 00:00:00.000000000 Z
11
+ date: 2016-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -57,8 +57,9 @@ files:
57
57
  - lib/embulk/filter/base58.rb
58
58
  - src/main/java/org/embulk/filter/base58/Base58.java
59
59
  - src/main/java/org/embulk/filter/base58/Base58FilterPlugin.java
60
+ - src/test/java/org/embulk/filter/base58/TestBase58FilterImpl.java
60
61
  - src/test/java/org/embulk/filter/base58/TestBase58FilterPlugin.java
61
- - classpath/embulk-filter-base58-0.1.0.jar
62
+ - classpath/embulk-filter-base58-0.1.1.jar
62
63
  homepage: https://github.com/kfitzgerald/embulk-filter-base58
63
64
  licenses:
64
65
  - MIT