Skip to content

Commit

Permalink
Create KeyValidator to verify key integrity
Browse files Browse the repository at this point in the history
  • Loading branch information
felginep committed Aug 26, 2021
1 parent e0afd8e commit 647934a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
1 change: 1 addition & 0 deletions lib/ad_localize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
require 'ad_localize/mappers/translation_group_mapper'
require 'ad_localize/mappers/android_translation_mapper'
require 'ad_localize/mappers/ios_translation_mapper'
require 'ad_localize/mappers/key_validator'

require 'ad_localize/entities/key'
require 'ad_localize/entities/translation'
Expand Down
17 changes: 2 additions & 15 deletions lib/ad_localize/mappers/csv_path_to_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,15 @@ def map(csv_path:)
@headers = CSV.foreach(csv_path).first
return unless valid?(csv_path: csv_path)
translations = []
existing_key_for_label = {}
validator = KeyValidator.new

CSV.foreach(csv_path, headers: true, skip_blanks: true) do |row|
row_translations = map_row(row: row, locales: locales)
next if row_translations.blank?

current_key = row_translations.first.key
next if validator.has_warnings?(current_key)

current_label = current_key.label
existing_key = existing_key_for_label[current_label]

unless existing_key.nil?
existing_plural_key = existing_key.label == current_key.label && existing_key.plural? && current_key.singular?
existing_singular_key = existing_key.label == current_key.label && existing_key.singular? && current_key.plural?
is_same_key = existing_key.same_as?(key: current_key)
LOGGER.warn "A plural value already exist for key '#{current_label}'. Remove duplicates." if existing_plural_key
LOGGER.warn "A singular value already exist for key '#{current_label}'. Remove duplicates." if existing_singular_key
LOGGER.warn "Some values already exist for key '#{current_label}'. Remove duplicates." if is_same_key
next if is_same_key || existing_plural_key || existing_singular_key
end

existing_key_for_label[current_label] = current_key
translations.concat(row_translations)
end

Expand Down
31 changes: 31 additions & 0 deletions lib/ad_localize/mappers/key_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module AdLocalize
module Mappers
class KeyValidator

def initialize
@existing_key_for_label = {}
end

def has_warnings?(current_key)
current_label = current_key.label
existing_key = @existing_key_for_label[current_label]

has_warnings = false

unless existing_key.nil?
existing_plural_key = existing_key.label == current_key.label && existing_key.plural? && current_key.singular?
existing_singular_key = existing_key.label == current_key.label && existing_key.singular? && current_key.plural?
is_same_key = existing_key.same_as?(key: current_key)
LOGGER.warn "A plural value already exist for key '#{current_label}'. Remove duplicates." if existing_plural_key
LOGGER.warn "A singular value already exist for key '#{current_label}'. Remove duplicates." if existing_singular_key
LOGGER.warn "Some values already exist for key '#{current_label}'. Remove duplicates." if is_same_key
has_warnings = is_same_key || existing_plural_key || existing_singular_key
end

@existing_key_for_label[current_label] = current_key

has_warnings
end
end
end
end
17 changes: 2 additions & 15 deletions lib/ad_localize/mappers/value_range_to_wording.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,15 @@ def map(value_range:)

def map_rows(values:)
translations = []
existing_key_for_label = {}
validator = KeyValidator.new

values[1..-1].each do |row|
row_translations = map_row(row: row)
next if row_translations.blank?

current_key = row_translations.first.key
next if validator.has_warnings?(current_key)

current_label = current_key.label
existing_key = existing_key_for_label[current_label]

unless existing_key.nil?
existing_plural_key = existing_key.label == current_key.label && existing_key.plural? && current_key.singular?
existing_singular_key = existing_key.label == current_key.label && existing_key.singular? && current_key.plural?
is_same_key = existing_key.same_as?(key: current_key)
LOGGER.warn "A plural value already exist for key '#{current_label}'. Remove duplicates." if existing_plural_key
LOGGER.warn "A singular value already exist for key '#{current_label}'. Remove duplicates." if existing_singular_key
LOGGER.warn "Some values already exist for key '#{current_label}'. Remove duplicates." if is_same_key
next if is_same_key || existing_plural_key || existing_singular_key
end

existing_key_for_label[current_label] = current_key
translations.concat(row_translations)
end
translations
Expand Down

0 comments on commit 647934a

Please sign in to comment.