Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Map attribute and List element referencing in where condition #696

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
dynamoid (3.8.0)
dynamoid (3.9.0)
activemodel (>= 4)
aws-sdk-dynamodb (~> 1.0)
concurrent-ruby (>= 1.0)
Expand Down
1 change: 1 addition & 0 deletions lib/dynamoid/adapter_plugin/aws_sdk_v3/create_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ def attribute_definition_element(name, dynamoid_type)
# Converts from symbol to the API string for the given data type
# E.g. :number -> 'N'
def api_type(type)
# TODO: seems we need more types to support
case type
when :string then STRING_TYPE
when :number then NUM_TYPE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def initialize(conditions, name_placeholders, value_placeholders, name_placehold
private

def build
clauses = @conditions.map do |name, attribute_conditions|
clauses = @conditions.map do |path, attribute_conditions|
attribute_conditions.map do |operator, value|
name_or_placeholder = name_or_placeholder_for(name)
name_or_placeholder = name_or_placeholder_for(path)

case operator
when :eq
Expand Down Expand Up @@ -59,12 +59,21 @@ def build
@expression = clauses.join(' AND ')
end

def name_or_placeholder_for(name)
return name unless name.upcase.in?(Dynamoid::AdapterPlugin::AwsSdkV3::RESERVED_WORDS)
# Replace reserved words with placeholders
def name_or_placeholder_for(path) # TODO: support List elements
sections = path.to_s.split('.')

placeholder = @name_placeholder_sequence.call
@name_placeholders[placeholder] = name
placeholder
sanitized = sections.map do |name|
unless name.upcase.to_sym.in?(Dynamoid::AdapterPlugin::AwsSdkV3::RESERVED_WORDS)
next name
end

placeholder = @name_placeholder_sequence.call
@name_placeholders[placeholder] = name
placeholder
end

sanitized.join('.')
end

def value_placeholder_for(value)
Expand Down
44 changes: 34 additions & 10 deletions lib/dynamoid/criteria/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,37 @@ def count_via_scan
end

def field_condition(key, value_before_type_casting)
name, operator = key.to_s.split('.')
value = type_cast_condition_parameter(name, value_before_type_casting)
operator ||= 'eq'
sections = key.to_s.split('.')

if sections.size == 1
name = sections[0]
path = name
operator = nil
elsif sections.last.in? ALLOWED_FIELD_OPERATORS
name = sections[0]
path = sections[0...-1].join('.')
operator = sections[-1]
else
name = sections[0]
path = sections.join('.')
operator = nil
end

unless operator.in? ALLOWED_FIELD_OPERATORS
raise Dynamoid::Errors::Error, "Unsupported operator #{operator} in #{key}"
type = source.attributes[name.to_sym][:type]
if type != :map && name != path
raise Dynamoid::Errors::Error,
"Dereference operator '.' in '#{key}' document path is not allowed for not :map field '#{name}'"
end

# we don't know types of nested attributes
value = if name == path
type_cast_condition_parameter(name, value_before_type_casting)
else
value_before_type_casting
end

operator ||= 'eq'

condition = \
case operator
# NULL/NOT_NULL operators don't have parameters
Expand All @@ -606,7 +629,7 @@ def field_condition(key, value_before_type_casting)
[operator.to_sym, value]
end

[name.to_sym, condition]
[path, condition]
end

def query_key_conditions
Expand Down Expand Up @@ -725,10 +748,11 @@ def scan_conditions
end

{}.tap do |opts|
@where_conditions.keys.map(&:to_sym).each do |key|
name, condition = field_condition(key, @where_conditions[key])
opts[name] ||= []
opts[name] << condition
@where_conditions.keys.map(&:to_sym).each do |key| # TODO: remove map to_sym
selector, condition = field_condition(key, @where_conditions[key])

opts[selector] ||= []
opts[selector] << condition
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/dynamoid/criteria/where_conditions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def empty?

def [](key)
hash = @conditions.find { |h| h.key?(key) }
hash[key] if hash

return nil unless hash

hash[key]
end
end
end
Expand Down
Loading
Loading