From 38a2313cf6844213a52b9ac6dfd05925d34b112a Mon Sep 17 00:00:00 2001 From: kynetiv Date: Wed, 13 Jul 2016 12:50:52 -0400 Subject: [PATCH 1/2] allow for multiple null_value mappings in the data dictionary --- lib/data_magic/index/document_builder.rb | 5 ++-- .../data_magic/index/document_builder_spec.rb | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/data_magic/index/document_builder.rb b/lib/data_magic/index/document_builder.rb index 3564e403..bcd9af32 100644 --- a/lib/data_magic/index/document_builder.rb +++ b/lib/data_magic/index/document_builder.rb @@ -50,16 +50,17 @@ def calculated_fields(row, config) # row: a hash (keys may be strings or symbols) # valid_types: an array of allowed types + # null_value: row values that will map to null # field_types: hash field_name : type (float, integer, string) # returns a hash where values have been coerced to the new type # TODO: move type validation to config load time instead def map_column_types(row, config) valid_types = config.valid_types - null_value = config.null_value || null_value = 'NULL' + null_value = [*config.null_value] || ['NULL'] mapped = {} row.each do |key, value| - if value == null_value + if null_value.include? value mapped[key] = nil else type = config.csv_column_type(key) diff --git a/spec/lib/data_magic/index/document_builder_spec.rb b/spec/lib/data_magic/index/document_builder_spec.rb index 744f3909..23b492d4 100644 --- a/spec/lib/data_magic/index/document_builder_spec.rb +++ b/spec/lib/data_magic/index/document_builder_spec.rb @@ -34,6 +34,33 @@ end end + context "with custom null_value" do + + describe "a single null_value mapping" do + before do + allow(config).to receive(:null_value).and_return(["NULL"]) + end + + subject {{ name: 'Smithville', sometimesNULL: 'NULL' }} + let(:expected_document) {{ 'name' => 'Smithville', + 'sometimesNULL' => nil }} + it_correctly "creates a document" + end + + describe "multiple null_value mappings" do + before do + allow(config).to receive(:null_value).and_return(["NULL","CustomNull"]) + end + + subject {{ name: 'Smithville', maybeNull: 'CustomNull', sometimesNULL: 'NULL' }} + let(:expected_document) {{ 'name' => 'Smithville', + 'maybeNull' => nil, + 'sometimesNULL' => nil }} + it_correctly "creates a document" + end + + end + context "with type mapping" do describe "integer" do before do From 98d1b29b2cf26cf235a0c3251681dac5c9194f48 Mon Sep 17 00:00:00 2001 From: kynetiv Date: Mon, 18 Jul 2016 17:56:54 -0400 Subject: [PATCH 2/2] skip downcase on null values --- lib/data_magic/index/document_builder.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/data_magic/index/document_builder.rb b/lib/data_magic/index/document_builder.rb index bcd9af32..1072e74f 100644 --- a/lib/data_magic/index/document_builder.rb +++ b/lib/data_magic/index/document_builder.rb @@ -77,6 +77,7 @@ def map_column_types(row, config) def lowercase_columns(row, field_types = {}) new_columns = {} row.each do |key, value| + next if value.nil? type = field_types[key.to_sym] || field_types[key.to_s] new_columns["_#{key}"] = value.downcase if type == "name" || type == "autocomplete" end