ruby-cdk 0.1.0
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 +7 -0
- data/CHANGELOG.md +87 -0
- data/LICENSE +21 -0
- data/README.md +303 -0
- data/THIRD_PARTY_NOTICES.md +327 -0
- data/data/spec/META.json +33 -0
- data/data/spec/model.json.gz +0 -0
- data/exe/ruby-cdk +9 -0
- data/lib/ruby-cdk.rb +4 -0
- data/lib/ruby_cdk/assets/builder.rb +122 -0
- data/lib/ruby_cdk/assets/hasher.rb +175 -0
- data/lib/ruby_cdk/assets/record.rb +49 -0
- data/lib/ruby_cdk/assets/uploader.rb +139 -0
- data/lib/ruby_cdk/assets/zipper.rb +108 -0
- data/lib/ruby_cdk/assets.rb +116 -0
- data/lib/ruby_cdk/bootstrap.rb +289 -0
- data/lib/ruby_cdk/cfn/resource.rb +93 -0
- data/lib/ruby_cdk/cfn.rb +10 -0
- data/lib/ruby_cdk/cli/arguments.rb +41 -0
- data/lib/ruby_cdk/cli/aws.rb +35 -0
- data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
- data/lib/ruby_cdk/cli/diff.rb +98 -0
- data/lib/ruby_cdk/cli/failure.rb +80 -0
- data/lib/ruby_cdk/cli/io.rb +70 -0
- data/lib/ruby_cdk/cli/list.rb +46 -0
- data/lib/ruby_cdk/cli/new.rb +313 -0
- data/lib/ruby_cdk/cli/operation.rb +124 -0
- data/lib/ruby_cdk/cli/pairs.rb +32 -0
- data/lib/ruby_cdk/cli/report.rb +118 -0
- data/lib/ruby_cdk/cli/selection.rb +134 -0
- data/lib/ruby_cdk/cli/session.rb +290 -0
- data/lib/ruby_cdk/cli/shell.rb +30 -0
- data/lib/ruby_cdk/cli/synth.rb +58 -0
- data/lib/ruby_cdk/cli/validate.rb +101 -0
- data/lib/ruby_cdk/cli.rb +242 -0
- data/lib/ruby_cdk/constructs/bucket.rb +191 -0
- data/lib/ruby_cdk/constructs/function.rb +163 -0
- data/lib/ruby_cdk/constructs/grant.rb +90 -0
- data/lib/ruby_cdk/constructs/log_group.rb +115 -0
- data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
- data/lib/ruby_cdk/constructs/queue.rb +118 -0
- data/lib/ruby_cdk/constructs/role.rb +282 -0
- data/lib/ruby_cdk/constructs/table.rb +160 -0
- data/lib/ruby_cdk/constructs/topic.rb +88 -0
- data/lib/ruby_cdk/constructs.rb +56 -0
- data/lib/ruby_cdk/core/app.rb +146 -0
- data/lib/ruby_cdk/core/construct.rb +105 -0
- data/lib/ruby_cdk/core/elements.rb +107 -0
- data/lib/ruby_cdk/core/ids.rb +117 -0
- data/lib/ruby_cdk/core/stack.rb +291 -0
- data/lib/ruby_cdk/core/stateful.rb +156 -0
- data/lib/ruby_cdk/core.rb +15 -0
- data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
- data/lib/ruby_cdk/deploy/deployer.rb +805 -0
- data/lib/ruby_cdk/deploy/errors.rb +72 -0
- data/lib/ruby_cdk/deploy/fakes.rb +546 -0
- data/lib/ruby_cdk/deploy/output/base.rb +167 -0
- data/lib/ruby_cdk/deploy/output/human.rb +37 -0
- data/lib/ruby_cdk/deploy/output/json.rb +80 -0
- data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
- data/lib/ruby_cdk/deploy/parameters.rb +86 -0
- data/lib/ruby_cdk/deploy/poller.rb +72 -0
- data/lib/ruby_cdk/deploy/ports.rb +205 -0
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
- data/lib/ruby_cdk/deploy/states.rb +62 -0
- data/lib/ruby_cdk/deploy.rb +25 -0
- data/lib/ruby_cdk/diff/compare.rb +141 -0
- data/lib/ruby_cdk/diff/normalizer.rb +83 -0
- data/lib/ruby_cdk/diff/render.rb +242 -0
- data/lib/ruby_cdk/diff/security.rb +322 -0
- data/lib/ruby_cdk/diff/statement.rb +256 -0
- data/lib/ruby_cdk/diff/template.rb +524 -0
- data/lib/ruby_cdk/diff.rb +62 -0
- data/lib/ruby_cdk/env.rb +42 -0
- data/lib/ruby_cdk/errors.rb +38 -0
- data/lib/ruby_cdk/fn.rb +409 -0
- data/lib/ruby_cdk/lazy.rb +55 -0
- data/lib/ruby_cdk/nag/config.rb +303 -0
- data/lib/ruby_cdk/nag/engine.rb +152 -0
- data/lib/ruby_cdk/nag/report.rb +86 -0
- data/lib/ruby_cdk/nag/rule.rb +171 -0
- data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
- data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
- data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
- data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
- data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
- data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
- data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
- data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
- data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
- data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
- data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
- data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
- data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
- data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
- data/lib/ruby_cdk/nag/rules.rb +51 -0
- data/lib/ruby_cdk/nag/values.rb +305 -0
- data/lib/ruby_cdk/nag.rb +60 -0
- data/lib/ruby_cdk/schema/normalizer.rb +164 -0
- data/lib/ruby_cdk/schema/property_schema.rb +22 -0
- data/lib/ruby_cdk/schema/schema.rb +195 -0
- data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
- data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
- data/lib/ruby_cdk/schema/sources/download.rb +86 -0
- data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
- data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
- data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
- data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
- data/lib/ruby_cdk/schema/sources/update.rb +178 -0
- data/lib/ruby_cdk/schema/sources.rb +20 -0
- data/lib/ruby_cdk/schema/type_schema.rb +109 -0
- data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
- data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
- data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
- data/lib/ruby_cdk/schema/validator/report.rb +92 -0
- data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
- data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
- data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
- data/lib/ruby_cdk/schema/validator/template.rb +203 -0
- data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
- data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
- data/lib/ruby_cdk/schema/validator.rb +159 -0
- data/lib/ruby_cdk/schema.rb +19 -0
- data/lib/ruby_cdk/synth/manifest.rb +128 -0
- data/lib/ruby_cdk/synth/resolver.rb +326 -0
- data/lib/ruby_cdk/synth/rules.rb +83 -0
- data/lib/ruby_cdk/synth/serializer.rb +120 -0
- data/lib/ruby_cdk/synth/writer.rb +31 -0
- data/lib/ruby_cdk/synth.rb +255 -0
- data/lib/ruby_cdk/test/capture.rb +133 -0
- data/lib/ruby_cdk/test/match.rb +426 -0
- data/lib/ruby_cdk/test/matcher.rb +290 -0
- data/lib/ruby_cdk/test/private/absent.rb +34 -0
- data/lib/ruby_cdk/test/private/conditions.rb +39 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
- data/lib/ruby_cdk/test/private/error.rb +48 -0
- data/lib/ruby_cdk/test/private/js.rb +326 -0
- data/lib/ruby_cdk/test/private/mappings.rb +39 -0
- data/lib/ruby_cdk/test/private/outputs.rb +39 -0
- data/lib/ruby_cdk/test/private/parameters.rb +39 -0
- data/lib/ruby_cdk/test/private/resources.rb +116 -0
- data/lib/ruby_cdk/test/private/section.rb +98 -0
- data/lib/ruby_cdk/test/private/sorting.rb +40 -0
- data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
- data/lib/ruby_cdk/test/private/type.rb +23 -0
- data/lib/ruby_cdk/test/template.rb +364 -0
- data/lib/ruby_cdk/test.rb +116 -0
- data/lib/ruby_cdk/values.rb +8 -0
- data/lib/ruby_cdk/version.rb +5 -0
- data/lib/ruby_cdk.rb +47 -0
- metadata +256 -0
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "core"
|
|
4
|
+
require_relative "synth/resolver"
|
|
5
|
+
require_relative "synth/rules"
|
|
6
|
+
require_relative "synth/serializer"
|
|
7
|
+
require_relative "synth/manifest"
|
|
8
|
+
require_relative "synth/writer"
|
|
9
|
+
|
|
10
|
+
module RubyCdk
|
|
11
|
+
# Synthesis (architecture §5): prepare, resolve, normalize, validate,
|
|
12
|
+
# serialize, write.
|
|
13
|
+
#
|
|
14
|
+
# The pipeline knows the normalizers, the validators and what the manifest
|
|
15
|
+
# says about other components only through what is registered here. A later
|
|
16
|
+
# component adds a normalizer or validators with register_normalizer and
|
|
17
|
+
# register_validator, and the pipeline does not change. The two validators of
|
|
18
|
+
# this component are registered the same way, at the end of this file.
|
|
19
|
+
#
|
|
20
|
+
# Resolver the resolve phase
|
|
21
|
+
# Rules V2 on logical IDs and V13 on the stack name
|
|
22
|
+
# Serializer the bytes, and the serialization contract
|
|
23
|
+
# Manifest what was written, as manifest.json says it
|
|
24
|
+
# Writer a file is written only when its bytes changed
|
|
25
|
+
module Synth
|
|
26
|
+
# What a normalizer and a validator receive.
|
|
27
|
+
#
|
|
28
|
+
# stack the RubyCdk::Stack, live
|
|
29
|
+
# template the template as it stands: a plain Hash with String keys
|
|
30
|
+
# issues what the phases before found
|
|
31
|
+
Context = Data.define(:stack, :template, :issues)
|
|
32
|
+
|
|
33
|
+
# What one stack's synthesis produced. issues is empty unless something is
|
|
34
|
+
# wrong; warnings is what a validator answered as a warning: reported on
|
|
35
|
+
# the result, but it neither fails the synthesis nor stops the write. The
|
|
36
|
+
# manifest ignores warnings; a command reads them off the result.
|
|
37
|
+
Result = Data.define(:stack, :template, :bytes, :issues, :warnings)
|
|
38
|
+
|
|
39
|
+
class << self
|
|
40
|
+
# --- what other components register
|
|
41
|
+
|
|
42
|
+
# normalizer answers call(context) with the normalized template, a new
|
|
43
|
+
# Hash: the one it was given is not changed. Normalizers run in the
|
|
44
|
+
# order they were registered, after resolve and before the validators.
|
|
45
|
+
def register_normalizer(normalizer)
|
|
46
|
+
check_callable(normalizer, "A normalizer")
|
|
47
|
+
normalizers_registry << normalizer
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def unregister_normalizer(normalizer)
|
|
52
|
+
normalizers_registry.delete(normalizer)
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def normalizers
|
|
57
|
+
normalizers_registry.dup.freeze
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# validator answers call(context) with an Array of RubyCdk::Issue. The
|
|
61
|
+
# template it sees is exactly what would be written. Validators run in
|
|
62
|
+
# the order they were registered, and every issue of every validator of
|
|
63
|
+
# every stack is reported in one ValidationError.
|
|
64
|
+
def register_validator(validator)
|
|
65
|
+
check_callable(validator, "A validator")
|
|
66
|
+
validators_registry << validator
|
|
67
|
+
nil
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def unregister_validator(validator)
|
|
71
|
+
validators_registry.delete(validator)
|
|
72
|
+
nil
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def validators
|
|
76
|
+
validators_registry.dup.freeze
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# provider answers call with what manifest.json writes under key at the
|
|
80
|
+
# top level: "schema_version". A key nobody registered is written as null.
|
|
81
|
+
def register_manifest_fact(key, &provider)
|
|
82
|
+
check_fact(key, provider, "A manifest fact")
|
|
83
|
+
manifest_facts[key] = provider
|
|
84
|
+
nil
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def manifest_fact(key)
|
|
88
|
+
manifest_facts[key]&.call
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def manifest_fact_keys
|
|
92
|
+
manifest_facts.keys.freeze
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def unregister_manifest_fact(key)
|
|
96
|
+
manifest_facts.delete(key)
|
|
97
|
+
nil
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# provider answers call(stack, template) with what every stack's entry
|
|
101
|
+
# writes under key: the hashes of its assets.
|
|
102
|
+
def register_entry_fact(key, &provider)
|
|
103
|
+
check_fact(key, provider, "An entry fact")
|
|
104
|
+
entry_facts[key] = provider
|
|
105
|
+
nil
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def entry_fact(key, stack, template)
|
|
109
|
+
entry_facts[key]&.call(stack, template)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def unregister_entry_fact(key)
|
|
113
|
+
entry_facts.delete(key)
|
|
114
|
+
nil
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# --- the phases
|
|
118
|
+
|
|
119
|
+
# Every phase except write, for one stack. Never raises ValidationError:
|
|
120
|
+
# result.issues says what is wrong, and result.warnings what a validator
|
|
121
|
+
# meant as a warning.
|
|
122
|
+
#
|
|
123
|
+
# prepare the prepare hooks, children first, once per node
|
|
124
|
+
# resolve the live tree becomes a plain template; Lazy values run here
|
|
125
|
+
# normalize the registered normalizers, in order
|
|
126
|
+
# serialize the template is put in the order it is written in
|
|
127
|
+
# validate the registered validators, on exactly that template
|
|
128
|
+
#
|
|
129
|
+
# validate: false skips the validators: no issues and no warnings from
|
|
130
|
+
# them. The value model is not validation — the resolver's V0 issues fire
|
|
131
|
+
# either way, because what they reject cannot be written at all.
|
|
132
|
+
def run(stack, validate: true)
|
|
133
|
+
issues = []
|
|
134
|
+
warnings = []
|
|
135
|
+
stack.prepare_tree # prepare
|
|
136
|
+
template = Resolver.new(stack, issues:).call # resolve
|
|
137
|
+
normalizers.each do |normalizer| # normalize
|
|
138
|
+
template = normalizer.call(context(stack, template, issues))
|
|
139
|
+
unless template.is_a?(Hash)
|
|
140
|
+
raise Error, "#{normalizer} returned a #{template.class}, and a normalizer returns the template as a Hash"
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
template = Serializer.ordered(template) # serialize, the template as it is written
|
|
144
|
+
if validate # validate
|
|
145
|
+
validators.each do |validator|
|
|
146
|
+
Array(validator.call(context(stack, template, issues))).each do |issue|
|
|
147
|
+
# A warning is an issue that answers warning? with true; this tool
|
|
148
|
+
# does not name the class, the answer is the contract.
|
|
149
|
+
if issue.respond_to?(:warning?) && issue.warning?
|
|
150
|
+
warnings << issue
|
|
151
|
+
else
|
|
152
|
+
issues << issue
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
Result.new(stack:, template:, bytes: Serializer.render(template), issues: issues.freeze,
|
|
158
|
+
warnings: warnings.freeze) # serialize, the bytes
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
# Every phase except write, for one stack. Raises one ValidationError
|
|
162
|
+
# holding every issue when anything is wrong; a warning never raises.
|
|
163
|
+
def synthesize(stack)
|
|
164
|
+
result = run(stack)
|
|
165
|
+
raise ValidationError.new(result.issues) unless result.issues.empty?
|
|
166
|
+
|
|
167
|
+
result
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Every phase on every stack of the app, in the order the stacks were
|
|
171
|
+
# created. One ValidationError holds every issue of every stack, and
|
|
172
|
+
# nothing is written when there is one. A warning rides along on its
|
|
173
|
+
# result, which the manifest takes and ignores.
|
|
174
|
+
#
|
|
175
|
+
# validate: false, the --no-validate of the command line, skips the
|
|
176
|
+
# validators on every stack: the templates are written whatever the
|
|
177
|
+
# schema rules would have said. The value model still is.
|
|
178
|
+
def synthesize_app(app, validate: true)
|
|
179
|
+
results = app.stacks.map { |stack| run(stack, validate: validate) }
|
|
180
|
+
failing = results.select { |result| result.issues.any? }
|
|
181
|
+
if failing.any?
|
|
182
|
+
raise ValidationError.new(failing.flat_map { |result| qualify(result, results.length > 1) })
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
Manifest.write(app, results)
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
private
|
|
189
|
+
|
|
190
|
+
def normalizers_registry
|
|
191
|
+
@normalizers_registry ||= []
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def validators_registry
|
|
195
|
+
@validators_registry ||= []
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def manifest_facts
|
|
199
|
+
@manifest_facts ||= {}
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def entry_facts
|
|
203
|
+
@entry_facts ||= {}
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def check_callable(object, what)
|
|
207
|
+
raise ArgumentError, "#{what} must answer call, got #{object.inspect}" unless object.respond_to?(:call)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def check_fact(key, provider, what)
|
|
211
|
+
unless key.is_a?(String) && !key.empty? && provider
|
|
212
|
+
raise ArgumentError, "#{what} needs a name and a block that answers its value"
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def context(stack, template, issues)
|
|
217
|
+
Context.new(stack:, template:, issues: issues.dup.freeze)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# With several stacks an issue says which one it is about: "blog:Resources.Table".
|
|
221
|
+
def qualify(result, name_the_stack)
|
|
222
|
+
return result.issues unless name_the_stack
|
|
223
|
+
|
|
224
|
+
result.issues.map do |issue|
|
|
225
|
+
Issue.new(rule: issue.rule, path: "#{result.stack.stack_name}:#{issue.path}", message: issue.message, hint: issue.hint)
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# The tree's contract declares these; here are their implementations, in the
|
|
232
|
+
# component that knows the pipeline.
|
|
233
|
+
class Stack
|
|
234
|
+
# Every phase of architecture §5 except write, as the template the stack would write.
|
|
235
|
+
def to_template
|
|
236
|
+
Synth.synthesize(self).template
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# One value the way the template holds it: Lazy values resolved, symbols
|
|
240
|
+
# as strings, an Fn::Join at its shortest. Raises ValidationError when the
|
|
241
|
+
# value cannot be written.
|
|
242
|
+
def resolve(value)
|
|
243
|
+
Synth::Resolver.new(self).resolve(value)
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
class App
|
|
248
|
+
def synth(validate: true)
|
|
249
|
+
Synth.synthesize_app(self, validate: validate)
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
RubyCdk::Synth.register_validator(RubyCdk::Synth::Rules::LogicalIds.new)
|
|
255
|
+
RubyCdk::Synth.register_validator(RubyCdk::Synth::Rules::StackName.new)
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ported from the AWS CDK.
|
|
4
|
+
#
|
|
5
|
+
# Origin: packages/aws-cdk-lib/matching/lib/capture.ts
|
|
6
|
+
# Commit: 025a6f67fec3d37a6ffdc6168f8a751a04918f66 (aws/aws-cdk, main)
|
|
7
|
+
# Licence: Apache-2.0
|
|
8
|
+
# Changed: the constructor's optional pattern is RubyCdk::Test::UNDEFINED when
|
|
9
|
+
# it is not given, which is what `pattern?: any` is there. The array the CDK
|
|
10
|
+
# keeps in a public field marked @internal (`_captured`) is private here; the
|
|
11
|
+
# engine fills it through _append_captures, which is internal as well.
|
|
12
|
+
|
|
13
|
+
require_relative "match"
|
|
14
|
+
require_relative "matcher"
|
|
15
|
+
require_relative "private/error"
|
|
16
|
+
require_relative "private/js"
|
|
17
|
+
require_relative "private/type"
|
|
18
|
+
|
|
19
|
+
module RubyCdk
|
|
20
|
+
module Test
|
|
21
|
+
# Capture values while matching templates.
|
|
22
|
+
# Using an instance of this class within a Matcher will capture the matching value.
|
|
23
|
+
# The `as*()` APIs on the instance can be used to get the captured value.
|
|
24
|
+
class Capture < Matcher
|
|
25
|
+
attr_reader :name
|
|
26
|
+
|
|
27
|
+
# Initialize a new capture.
|
|
28
|
+
#
|
|
29
|
+
# pattern: a nested pattern or Matcher. If a nested pattern is provided,
|
|
30
|
+
# `object_like()` matching is applied.
|
|
31
|
+
def initialize(pattern = UNDEFINED)
|
|
32
|
+
@name = "Capture"
|
|
33
|
+
@pattern = pattern
|
|
34
|
+
@captured = []
|
|
35
|
+
@index = 0
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def test(actual)
|
|
39
|
+
result = MatchResult.new(actual)
|
|
40
|
+
if Private::Js.nullish?(actual)
|
|
41
|
+
return result.record_failure(matcher: self, path: [], message: "Can only capture non-nullish values. Found #{Private::Js.string(actual)}")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
unless @pattern.equal?(UNDEFINED)
|
|
45
|
+
inner_matcher = Matcher.matcher?(@pattern) ? @pattern : Match.object_like(@pattern)
|
|
46
|
+
inner_result = inner_matcher.test(actual)
|
|
47
|
+
return inner_result if inner_result.has_failed?
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
result.record_capture(capture: self, value: actual)
|
|
51
|
+
result
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# When multiple results are captured, move the iterator to the next result.
|
|
55
|
+
# Returns true if another capture is present, false otherwise.
|
|
56
|
+
def next
|
|
57
|
+
if @index < @captured.length - 1
|
|
58
|
+
@index += 1
|
|
59
|
+
return true
|
|
60
|
+
end
|
|
61
|
+
false
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Retrieve the captured value as a string.
|
|
65
|
+
# An error is generated if no value is captured or if the value is not a string.
|
|
66
|
+
def as_string
|
|
67
|
+
validate
|
|
68
|
+
value = @captured[@index]
|
|
69
|
+
return value if Private.get_type(value) == "string"
|
|
70
|
+
|
|
71
|
+
report_incorrect_type("string")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# Retrieve the captured value as a number.
|
|
75
|
+
# An error is generated if no value is captured or if the value is not a number.
|
|
76
|
+
def as_number
|
|
77
|
+
validate
|
|
78
|
+
value = @captured[@index]
|
|
79
|
+
return value if Private.get_type(value) == "number"
|
|
80
|
+
|
|
81
|
+
report_incorrect_type("number")
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Retrieve the captured value as a boolean.
|
|
85
|
+
# An error is generated if no value is captured or if the value is not a boolean.
|
|
86
|
+
def as_boolean
|
|
87
|
+
validate
|
|
88
|
+
value = @captured[@index]
|
|
89
|
+
return value if Private.get_type(value) == "boolean"
|
|
90
|
+
|
|
91
|
+
report_incorrect_type("boolean")
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# Retrieve the captured value as an array.
|
|
95
|
+
# An error is generated if no value is captured or if the value is not an array.
|
|
96
|
+
def as_array
|
|
97
|
+
validate
|
|
98
|
+
value = @captured[@index]
|
|
99
|
+
return value if Private.get_type(value) == "array"
|
|
100
|
+
|
|
101
|
+
report_incorrect_type("array")
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Retrieve the captured value as a JSON object.
|
|
105
|
+
# An error is generated if no value is captured or if the value is not an object.
|
|
106
|
+
def as_object
|
|
107
|
+
validate
|
|
108
|
+
value = @captured[@index]
|
|
109
|
+
return value if Private.get_type(value) == "object"
|
|
110
|
+
|
|
111
|
+
report_incorrect_type("object")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Internal to the engine, not part of the public API: what MatchResult#finished hands over.
|
|
115
|
+
def _append_captures(values)
|
|
116
|
+
@captured.concat(values)
|
|
117
|
+
nil
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
def validate
|
|
123
|
+
raise AssertionError, "No value captured" if @captured.empty?
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def report_incorrect_type(expected)
|
|
127
|
+
raise AssertionError,
|
|
128
|
+
"Captured value is expected to be #{expected} but found #{Private.get_type(@captured[@index])}. " \
|
|
129
|
+
"Value is #{Private::Js.json(@captured[@index], 2)}"
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|