parameter_substitution 0.3.0.pre.1 → 0.3.0.pre.2
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 +4 -4
- data/lib/parameter_substitution/configuration.rb +39 -0
- data/lib/parameter_substitution/context.rb +74 -0
- data/lib/parameter_substitution/encoder.rb +86 -0
- data/lib/parameter_substitution/expression.rb +146 -0
- data/lib/parameter_substitution/formatters/add_prefix.rb +19 -0
- data/lib/parameter_substitution/formatters/base.rb +46 -0
- data/lib/parameter_substitution/formatters/blank_if_nil.rb +11 -0
- data/lib/parameter_substitution/formatters/cgi_unescape.rb +11 -0
- data/lib/parameter_substitution/formatters/compare_string.rb +21 -0
- data/lib/parameter_substitution/formatters/date_time_custom.rb +25 -0
- data/lib/parameter_substitution/formatters/date_time_format.rb +53 -0
- data/lib/parameter_substitution/formatters/date_time_iso8601.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_iso8601_zulu.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_strftime.rb +19 -0
- data/lib/parameter_substitution/formatters/date_time_unix_timestamp.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_us_all_slashes.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_us_normal.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_us_seconds.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_us_short_am_pm.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_us_short_year.rb +11 -0
- data/lib/parameter_substitution/formatters/date_time_utc_year_first_dashes_seconds.rb +11 -0
- data/lib/parameter_substitution/formatters/date_us_dashes.rb +11 -0
- data/lib/parameter_substitution/formatters/date_us_normal.rb +11 -0
- data/lib/parameter_substitution/formatters/date_year_first_dashes.rb +11 -0
- data/lib/parameter_substitution/formatters/downcase.rb +11 -0
- data/lib/parameter_substitution/formatters/duration_as_seconds.rb +11 -0
- data/lib/parameter_substitution/formatters/duration_as_time.rb +11 -0
- data/lib/parameter_substitution/formatters/duration_grouped_by_description.rb +23 -0
- data/lib/parameter_substitution/formatters/greater_than_value.rb +21 -0
- data/lib/parameter_substitution/formatters/if_nil.rb +19 -0
- data/lib/parameter_substitution/formatters/in_timezone.rb +23 -0
- data/lib/parameter_substitution/formatters/json_parse.rb +26 -0
- data/lib/parameter_substitution/formatters/left.rb +19 -0
- data/lib/parameter_substitution/formatters/lookup.rb +19 -0
- data/lib/parameter_substitution/formatters/lower.rb +11 -0
- data/lib/parameter_substitution/formatters/manager.rb +35 -0
- data/lib/parameter_substitution/formatters/md5.rb +11 -0
- data/lib/parameter_substitution/formatters/mid.rb +20 -0
- data/lib/parameter_substitution/formatters/parse_time.rb +23 -0
- data/lib/parameter_substitution/formatters/right.rb +23 -0
- data/lib/parameter_substitution/formatters/sha256.rb +11 -0
- data/lib/parameter_substitution/formatters/split_after_colon.rb +11 -0
- data/lib/parameter_substitution/formatters/split_and_find.rb +20 -0
- data/lib/parameter_substitution/formatters/split_before_colon.rb +11 -0
- data/lib/parameter_substitution/formatters/time_with_seconds.rb +11 -0
- data/lib/parameter_substitution/formatters/trim.rb +11 -0
- data/lib/parameter_substitution/formatters/upper.rb +11 -0
- data/lib/parameter_substitution/method_call_expression.rb +58 -0
- data/lib/parameter_substitution/parse_error.rb +6 -0
- data/lib/parameter_substitution/parser.rb +109 -0
- data/lib/parameter_substitution/substitution_expression.rb +79 -0
- data/lib/parameter_substitution/text_expression.rb +52 -0
- data/lib/parameter_substitution/transform.rb +99 -0
- data/lib/parameter_substitution/version.rb +5 -0
- metadata +56 -2
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'hobo_support/array'
|
4
|
+
|
5
|
+
class ParameterSubstitution
|
6
|
+
class SubstitutionExpression
|
7
|
+
attr_accessor :parameter_name, :method_calls, :context
|
8
|
+
|
9
|
+
def initialize(parameter_name, method_calls, context)
|
10
|
+
@context = context
|
11
|
+
@parameter_name, @method_calls = fixup_name_and_method_calls(parameter_name, method_calls)
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(_inside_quotes)
|
15
|
+
# TODO: - Chase down callers who pass allow_nil = false. Can we get rid of them?
|
16
|
+
if !@context.allow_nil && @context.mapping_has_key?(@parameter_name) && @context.mapped_value(@parameter_name).nil?
|
17
|
+
raise ParameterSubstitution::ParseError, "Replacement parameter '#{@parameter_name}' is nil"
|
18
|
+
end
|
19
|
+
|
20
|
+
method_calls.each(&:validate)
|
21
|
+
end
|
22
|
+
|
23
|
+
def unknown_parameters(inside_quotes)
|
24
|
+
if !@context.mapping_has_key?(@parameter_name) && !(@context.destination_encoding == :json && inside_quotes)
|
25
|
+
@parameter_name
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def evaluate(inside_quotes, only_expression: false)
|
30
|
+
if @context.mapping_has_key?(@parameter_name)
|
31
|
+
formatted_value = format(@context.mapped_value(@parameter_name), inside_quotes)
|
32
|
+
if @context.destination_encoding == :raw && formatted_value.nil?
|
33
|
+
only_expression ? '' : "#{@context.parameter_start}#{@parameter_name}#{@context.parameter_end}"
|
34
|
+
else
|
35
|
+
formatted_value
|
36
|
+
end
|
37
|
+
else
|
38
|
+
if @context.destination_encoding == :raw && only_expression
|
39
|
+
''
|
40
|
+
else
|
41
|
+
"#{@context.parameter_start}#{@parameter_name}#{@context.parameter_end}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def ends_inside_quotes(started_inside_quotes:)
|
47
|
+
started_inside_quotes
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
def fixup_name_and_method_calls(parameter_name, method_calls)
|
53
|
+
if args = try_parameter_name(parameter_name, method_calls)
|
54
|
+
args
|
55
|
+
else
|
56
|
+
[parameter_name, method_calls]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def try_parameter_name(parameter_name, method_calls)
|
61
|
+
if @context.mapping_has_key?(parameter_name)
|
62
|
+
[parameter_name, method_calls]
|
63
|
+
elsif !method_calls.empty? && method_calls.first.arguments.empty?
|
64
|
+
first_method = method_calls.first
|
65
|
+
remainder = method_calls[1..-1]
|
66
|
+
try_parameter_name(parameter_name + "." + first_method.name, remainder)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def format(lookup_value, inside_quotes)
|
71
|
+
raw_value = @method_calls.reduce(lookup_value) { |previous_value, method| method.call_method(previous_value) }
|
72
|
+
Encoder.encode(raw_value, @context.destination_encoding, source_encoding, @parameter_name, inside_quotes)
|
73
|
+
end
|
74
|
+
|
75
|
+
def source_encoding
|
76
|
+
@method_calls.last&.encoding || :text
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ParameterSubstitution
|
4
|
+
class TextExpression
|
5
|
+
attr_accessor :value
|
6
|
+
def initialize(value)
|
7
|
+
@value = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def evaluate(_inside_quotes, only_expression: false)
|
11
|
+
@value
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(_inside_quotes); end
|
15
|
+
|
16
|
+
def parameter_name
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def unknown_parameters(_inside_quotes)
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def ends_inside_quotes(started_inside_quotes:)
|
25
|
+
state = started_inside_quotes ? :InsideString : :OutsideString
|
26
|
+
|
27
|
+
@value.chars.each do |c|
|
28
|
+
case state
|
29
|
+
|
30
|
+
when :InsideString
|
31
|
+
if c == '"'
|
32
|
+
state = :OutsideString
|
33
|
+
elsif c == '\\'
|
34
|
+
state = :InsideStringGotBackslash
|
35
|
+
end
|
36
|
+
|
37
|
+
when :OutsideString
|
38
|
+
if c == '"'
|
39
|
+
state = :InsideString
|
40
|
+
end
|
41
|
+
|
42
|
+
when :InsideStringGotBackslash
|
43
|
+
state = :InsideString
|
44
|
+
else
|
45
|
+
raise "unexpected #{state}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
state != :OutsideString
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'parslet'
|
4
|
+
#
|
5
|
+
# The transform takes the output from the parser and constructs an Abstract Syntax Tree
|
6
|
+
# from the result. In this case, the output is a list of either TextExpression or SubstitutionExpressions.
|
7
|
+
#
|
8
|
+
# See the parslet documentation for more detail.
|
9
|
+
#
|
10
|
+
|
11
|
+
class ParameterSubstitution
|
12
|
+
class Transform < Parslet::Transform
|
13
|
+
def initialize(context)
|
14
|
+
@context = context
|
15
|
+
super
|
16
|
+
define_instance_rules
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Argument lists
|
21
|
+
#
|
22
|
+
rule float_arg: simple(:float) do
|
23
|
+
float.to_f
|
24
|
+
end
|
25
|
+
|
26
|
+
rule int_arg: simple(:int) do
|
27
|
+
int.to_i
|
28
|
+
end
|
29
|
+
|
30
|
+
rule string_arg: simple(:string) do
|
31
|
+
# Strip all leading slashes.
|
32
|
+
ParameterSubstitution::Transform.unescape(string)
|
33
|
+
end
|
34
|
+
|
35
|
+
rule string_arg: sequence(:string) do
|
36
|
+
""
|
37
|
+
end
|
38
|
+
|
39
|
+
rule nil_arg: simple(:_) do
|
40
|
+
nil
|
41
|
+
end
|
42
|
+
|
43
|
+
rule arg_list: sequence(:parameters) do
|
44
|
+
parameters
|
45
|
+
end
|
46
|
+
|
47
|
+
rule arg_list: simple(:parameter) do
|
48
|
+
[parameter]
|
49
|
+
end
|
50
|
+
|
51
|
+
#
|
52
|
+
# Method Call
|
53
|
+
#
|
54
|
+
rule(method_call: simple(:method_call), arg_list: sequence(:arg_list)) do
|
55
|
+
MethodCallExpression.new(method_call, arg_list)
|
56
|
+
end
|
57
|
+
|
58
|
+
rule(method_call: simple(:method_calls), arg_list: simple(:arg)) do
|
59
|
+
MethodCallExpression.new(method_call, [arg])
|
60
|
+
end
|
61
|
+
|
62
|
+
rule method_call: simple(:method_call) do
|
63
|
+
MethodCallExpression.new(method_call, [])
|
64
|
+
end
|
65
|
+
|
66
|
+
#
|
67
|
+
# The overall expression
|
68
|
+
#
|
69
|
+
rule text: simple(:text) do
|
70
|
+
TextExpression.new(text.to_s)
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.unescape(string)
|
74
|
+
string.to_s.gsub(/\\(.)/) { |v| v[1] }
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def define_instance_rules
|
80
|
+
# This rule must be defined on the instance so that it can access attributes on the instance.
|
81
|
+
# The definition also takes a parameter to the block so that the block is bound to the instance
|
82
|
+
rule expression: sequence(:parameters) do |dictionary|
|
83
|
+
ParameterSubstitution::Expression.new(dictionary[:parameters], @context)
|
84
|
+
end
|
85
|
+
|
86
|
+
rule raw_expression: sequence(:parameters) do |dictionary|
|
87
|
+
ParameterSubstitution::Expression.new(dictionary[:parameters], @context.duplicate_raw)
|
88
|
+
end
|
89
|
+
|
90
|
+
rule parameter_name: simple(:parameter_name), method_calls: sequence(:method_calls) do |dictionary|
|
91
|
+
SubstitutionExpression.new(
|
92
|
+
ParameterSubstitution::Transform.unescape(dictionary[:parameter_name].to_s),
|
93
|
+
dictionary[:method_calls],
|
94
|
+
@context
|
95
|
+
)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parameter_substitution
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.0.pre.
|
4
|
+
version: 0.3.0.pre.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Smith
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2020-04-
|
12
|
+
date: 2020-04-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: The substitution can be formatted using a syntax that looks like method
|
15
15
|
calls
|
@@ -22,6 +22,60 @@ extensions: []
|
|
22
22
|
extra_rdoc_files: []
|
23
23
|
files:
|
24
24
|
- lib/parameter_substitution.rb
|
25
|
+
- lib/parameter_substitution/configuration.rb
|
26
|
+
- lib/parameter_substitution/context.rb
|
27
|
+
- lib/parameter_substitution/encoder.rb
|
28
|
+
- lib/parameter_substitution/expression.rb
|
29
|
+
- lib/parameter_substitution/formatters/add_prefix.rb
|
30
|
+
- lib/parameter_substitution/formatters/base.rb
|
31
|
+
- lib/parameter_substitution/formatters/blank_if_nil.rb
|
32
|
+
- lib/parameter_substitution/formatters/cgi_unescape.rb
|
33
|
+
- lib/parameter_substitution/formatters/compare_string.rb
|
34
|
+
- lib/parameter_substitution/formatters/date_time_custom.rb
|
35
|
+
- lib/parameter_substitution/formatters/date_time_format.rb
|
36
|
+
- lib/parameter_substitution/formatters/date_time_iso8601.rb
|
37
|
+
- lib/parameter_substitution/formatters/date_time_iso8601_zulu.rb
|
38
|
+
- lib/parameter_substitution/formatters/date_time_strftime.rb
|
39
|
+
- lib/parameter_substitution/formatters/date_time_unix_timestamp.rb
|
40
|
+
- lib/parameter_substitution/formatters/date_time_us_all_slashes.rb
|
41
|
+
- lib/parameter_substitution/formatters/date_time_us_normal.rb
|
42
|
+
- lib/parameter_substitution/formatters/date_time_us_seconds.rb
|
43
|
+
- lib/parameter_substitution/formatters/date_time_us_short_am_pm.rb
|
44
|
+
- lib/parameter_substitution/formatters/date_time_us_short_year.rb
|
45
|
+
- lib/parameter_substitution/formatters/date_time_utc_year_first_dashes_seconds.rb
|
46
|
+
- lib/parameter_substitution/formatters/date_us_dashes.rb
|
47
|
+
- lib/parameter_substitution/formatters/date_us_normal.rb
|
48
|
+
- lib/parameter_substitution/formatters/date_year_first_dashes.rb
|
49
|
+
- lib/parameter_substitution/formatters/downcase.rb
|
50
|
+
- lib/parameter_substitution/formatters/duration_as_seconds.rb
|
51
|
+
- lib/parameter_substitution/formatters/duration_as_time.rb
|
52
|
+
- lib/parameter_substitution/formatters/duration_grouped_by_description.rb
|
53
|
+
- lib/parameter_substitution/formatters/greater_than_value.rb
|
54
|
+
- lib/parameter_substitution/formatters/if_nil.rb
|
55
|
+
- lib/parameter_substitution/formatters/in_timezone.rb
|
56
|
+
- lib/parameter_substitution/formatters/json_parse.rb
|
57
|
+
- lib/parameter_substitution/formatters/left.rb
|
58
|
+
- lib/parameter_substitution/formatters/lookup.rb
|
59
|
+
- lib/parameter_substitution/formatters/lower.rb
|
60
|
+
- lib/parameter_substitution/formatters/manager.rb
|
61
|
+
- lib/parameter_substitution/formatters/md5.rb
|
62
|
+
- lib/parameter_substitution/formatters/mid.rb
|
63
|
+
- lib/parameter_substitution/formatters/parse_time.rb
|
64
|
+
- lib/parameter_substitution/formatters/right.rb
|
65
|
+
- lib/parameter_substitution/formatters/sha256.rb
|
66
|
+
- lib/parameter_substitution/formatters/split_after_colon.rb
|
67
|
+
- lib/parameter_substitution/formatters/split_and_find.rb
|
68
|
+
- lib/parameter_substitution/formatters/split_before_colon.rb
|
69
|
+
- lib/parameter_substitution/formatters/time_with_seconds.rb
|
70
|
+
- lib/parameter_substitution/formatters/trim.rb
|
71
|
+
- lib/parameter_substitution/formatters/upper.rb
|
72
|
+
- lib/parameter_substitution/method_call_expression.rb
|
73
|
+
- lib/parameter_substitution/parse_error.rb
|
74
|
+
- lib/parameter_substitution/parser.rb
|
75
|
+
- lib/parameter_substitution/substitution_expression.rb
|
76
|
+
- lib/parameter_substitution/text_expression.rb
|
77
|
+
- lib/parameter_substitution/transform.rb
|
78
|
+
- lib/parameter_substitution/version.rb
|
25
79
|
homepage: https://github.com/Invoca/parameter_substitution
|
26
80
|
licenses: []
|
27
81
|
metadata:
|