lib/gooddata/helpers/global_helpers_params.rb in gooddata-0.6.51 vs lib/gooddata/helpers/global_helpers_params.rb in gooddata-0.6.52
- old
+ new
@@ -9,12 +9,16 @@
ENCODED_PARAMS_KEY = 'gd_encoded_params'
ENCODED_HIDDEN_PARAMS_KEY = 'gd_encoded_hidden_params'
class << self
# Encodes parameters for passing them to GD execution platform.
- # Core types are kept and complex types (arrays, structures, etc) are JSON encoded into key hash "gd_encoded_params" or "gd_encoded_hidden_params", depending on the 'hidden' method param.
- # The two different keys are used because the params and hidden params are merged by the platform and if we use the same key, the param would be overwritten.
+ # Core types are kept and complex types (arrays, structures, etc) are
+ # JSON encoded into key hash "gd_encoded_params" or
+ # "gd_encoded_hidden_params", depending on the 'hidden' method param.
+ # The two different keys are used because the params and hidden params
+ # are merged by the platform and if we use the same key,
+ # the param would be overwritten.
#
# Core types are following:
# - Boolean (true, false)
# - Fixnum
# - Float
@@ -66,56 +70,30 @@
params[hidden_key]
else
'{}'
end
+ reference_values = []
# Replace reference parameters by the actual values. Use backslash to escape a reference parameter, e.g: \${not_a_param},
# the ${not_a_param} will not be replaced
if options[:resolve_reference_params]
- regexps = Regexp.union(/\\\\/, /\\\$/, /\$\{(\w+)\}/)
- resolve_reference = lambda do |v|
- if v.is_a? Hash
- Hash[
- v.map do |k, v2|
- [k, resolve_reference.call(v2)]
- end
- ]
- elsif v.is_a? Array
- v.map do |v2|
- resolve_reference.call(v2)
- end
- elsif !v.is_a?(String)
- v
- else
- v.gsub(regexps) do |match|
- if match =~ /\\\\/
- data_params.is_a?(Hash) ? '\\' : '\\\\' # rubocop: disable Metrics/BlockNesting
- elsif match =~ /\\\$/
- '$'
- elsif match =~ /\$\{(\w+)\}/
- params["#{$1}"] || raise("The gd_encoded_params parameter contains unknow reference #{$1}") # rubocop: disable Style/PerlBackrefs
- end
- end
- end
- end
-
- data_params = if data_params.is_a? Hash
- Hash[
- data_params.map do |k, v|
- [k, resolve_reference.call(v)]
- end
- ]
- else
- resolve_reference.call(data_params)
- end
+ data_params, reference_values = resolve_reference_params(data_params, params)
+ hidden_data_params, = resolve_reference_params(hidden_data_params, params)
end
begin
parsed_data_params = data_params.is_a?(Hash) ? data_params : JSON.parse(data_params)
+ rescue JSON::ParserError => exception
+ reason = exception.message
+ reference_values.each { |secret_value| reason.gsub!("\"#{secret_value}\"", '"***"') }
+ raise exception.class, "Error reading json from '#{key}', reason: #{reason}"
+ end
+
+ begin
parsed_hidden_data_params = hidden_data_params.is_a?(Hash) ? hidden_data_params : JSON.parse(hidden_data_params)
rescue JSON::ParserError => exception
- raise exception.class, "Error reading json from '#{key}' or '#{hidden_key}', reason: #{exception.message}"
+ raise exception.class, "Error reading json from '#{hidden_key}'"
end
# Add the nil on ENCODED_HIDDEN_PARAMS_KEY
# if the data was retrieved from API You will not have the actual values so encode -> decode is not losless. The nil on the key prevents the server from deleting the key
parsed_hidden_data_params[ENCODED_HIDDEN_PARAMS_KEY] = nil unless parsed_hidden_data_params.empty?
@@ -257,9 +235,53 @@
stringify_values(v)
end
else
value.to_s
end
+ end
+
+ private
+
+ def resolve_reference_params(data_params, params)
+ reference_values = []
+ regexps = Regexp.union(/\\\\/, /\\\$/, /\$\{(\w+)\}/)
+ resolve_reference = lambda do |v|
+ if v.is_a? Hash
+ Hash[
+ v.map do |k, v2|
+ [k, resolve_reference.call(v2)]
+ end
+ ]
+ elsif v.is_a? Array
+ v.map do |v2|
+ resolve_reference.call(v2)
+ end
+ elsif !v.is_a?(String)
+ v
+ else
+ v.gsub(regexps) do |match|
+ if match =~ /\\\\/
+ data_params.is_a?(Hash) ? '\\' : '\\\\' # rubocop: disable Metrics/BlockNesting
+ elsif match =~ /\\\$/
+ '$'
+ elsif match =~ /\$\{(\w+)\}/
+ val = params["#{$1}"] || raise("The gd_encoded_params parameter contains unknow reference #{$1}") # rubocop: disable Style/PerlBackrefs
+ reference_values << val
+ val
+ end
+ end
+ end
+ end
+
+ data_params = if data_params.is_a? Hash
+ Hash[data_params.map do |k, v|
+ [k, resolve_reference.call(v)]
+ end]
+ else
+ resolve_reference.call(data_params)
+ end
+
+ [data_params, reference_values]
end
end
end
end