runtime_analysis 1.0.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 +115 -0
- data/LICENSE.txt +96 -0
- data/README.md +164 -0
- data/editor/nvim/README.md +278 -0
- data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
- data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
- data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
- data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
- data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
- data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
- data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
- data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
- data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
- data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
- data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
- data/editor/nvim/plugin/runtime_analysis.lua +71 -0
- data/examples/trackers/PROMPT.md +86 -0
- data/examples/trackers/honeybadger.rb +19 -0
- data/examples/trackers/sentry.rb +16 -0
- data/ext/runtime_analysis/extconf.rb +68 -0
- data/ext/runtime_analysis/fast.c +775 -0
- data/ext/runtime_analysis/fast.h +59 -0
- data/ext/runtime_analysis/hold.c +560 -0
- data/ext/runtime_analysis/init.c +36 -0
- data/ext/runtime_analysis/names.c +78 -0
- data/ext/runtime_analysis/names.h +28 -0
- data/ext/runtime_analysis/novelty.c +139 -0
- data/ext/runtime_analysis/novelty.h +22 -0
- data/ext/runtime_analysis/os.c +73 -0
- data/ext/runtime_analysis/os.h +32 -0
- data/ext/runtime_analysis/ring.c +445 -0
- data/ext/runtime_analysis/rows.c +304 -0
- data/ext/runtime_analysis/sink.c +485 -0
- data/ext/runtime_analysis/sink.h +240 -0
- data/ext/runtime_analysis/state.c +254 -0
- data/ext/runtime_analysis/state.h +57 -0
- data/ext/runtime_analysis/wrapper.c +278 -0
- data/ext/runtime_analysis/wrapper.h +24 -0
- data/lib/runtime_analysis/errors.rb +34 -0
- data/lib/runtime_analysis/failsafe.rb +123 -0
- data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
- data/lib/runtime_analysis/host/declared.rb +152 -0
- data/lib/runtime_analysis/host/inject.rb +90 -0
- data/lib/runtime_analysis/host/job_runners.rb +27 -0
- data/lib/runtime_analysis/host/middleware.rb +23 -0
- data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
- data/lib/runtime_analysis/host/railtie.rb +42 -0
- data/lib/runtime_analysis/host/reports.rb +54 -0
- data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
- data/lib/runtime_analysis/host/web.rb +22 -0
- data/lib/runtime_analysis/host.rb +34 -0
- data/lib/runtime_analysis/inject.rb +5 -0
- data/lib/runtime_analysis/minitest.rb +9 -0
- data/lib/runtime_analysis/native/build.rb +17 -0
- data/lib/runtime_analysis/native/byte_size.rb +27 -0
- data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
- data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
- data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
- data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
- data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
- data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
- data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
- data/lib/runtime_analysis/native/sink.rb +12 -0
- data/lib/runtime_analysis/native.rb +74 -0
- data/lib/runtime_analysis/platform.rb +30 -0
- data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
- data/lib/runtime_analysis/query/index/graph.rb +107 -0
- data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
- data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
- data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
- data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
- data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
- data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
- data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
- data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
- data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
- data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
- data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
- data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
- data/lib/runtime_analysis/query/index/reading.rb +80 -0
- data/lib/runtime_analysis/query/index/shapes.rb +142 -0
- data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
- data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
- data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
- data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
- data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
- data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
- data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
- data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
- data/lib/runtime_analysis/query/index/signatures.rb +93 -0
- data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
- data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
- data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
- data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
- data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
- data/lib/runtime_analysis/query/index/static/method.rb +19 -0
- data/lib/runtime_analysis/query/index/static/params.rb +52 -0
- data/lib/runtime_analysis/query/index/static/source.rb +69 -0
- data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
- data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
- data/lib/runtime_analysis/query/index/static/value.rb +17 -0
- data/lib/runtime_analysis/query/index/static/values.rb +71 -0
- data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
- data/lib/runtime_analysis/query/index/static.rb +85 -0
- data/lib/runtime_analysis/query/index.rb +32 -0
- data/lib/runtime_analysis/query/nodes.rb +60 -0
- data/lib/runtime_analysis/query/project/config.rb +108 -0
- data/lib/runtime_analysis/query/project.rb +40 -0
- data/lib/runtime_analysis/query/report/document.rb +84 -0
- data/lib/runtime_analysis/query/report/finding.rb +15 -0
- data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
- data/lib/runtime_analysis/query/report/list.rb +15 -0
- data/lib/runtime_analysis/query/report/location.rb +14 -0
- data/lib/runtime_analysis/query/report/one_row.rb +14 -0
- data/lib/runtime_analysis/query/report/outline.rb +15 -0
- data/lib/runtime_analysis/query/report/paint.rb +34 -0
- data/lib/runtime_analysis/query/report/related.rb +11 -0
- data/lib/runtime_analysis/query/report/table.rb +15 -0
- data/lib/runtime_analysis/query/report/text.rb +13 -0
- data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
- data/lib/runtime_analysis/query/report.rb +35 -0
- data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
- data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
- data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
- data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
- data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
- data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
- data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
- data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
- data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
- data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
- data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
- data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
- data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
- data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
- data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
- data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
- data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
- data/lib/runtime_analysis/query/surfaces.rb +14 -0
- data/lib/runtime_analysis/query/tools/base.rb +86 -0
- data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
- data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
- data/lib/runtime_analysis/query/tools/origins.rb +47 -0
- data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
- data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
- data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
- data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
- data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
- data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
- data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
- data/lib/runtime_analysis/query/tools.rb +34 -0
- data/lib/runtime_analysis/query.rb +33 -0
- data/lib/runtime_analysis/recording/blob.rb +13 -0
- data/lib/runtime_analysis/recording/format.rb +32 -0
- data/lib/runtime_analysis/recording/opcodes.rb +30 -0
- data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
- data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
- data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
- data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
- data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
- data/lib/runtime_analysis/recording/seen.rb +63 -0
- data/lib/runtime_analysis/recording/sink.rb +19 -0
- data/lib/runtime_analysis/recording/source_blob.rb +20 -0
- data/lib/runtime_analysis/recording.rb +52 -0
- data/lib/runtime_analysis/rspec.rb +8 -0
- data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
- data/lib/runtime_analysis/ruby_parsing.rb +98 -0
- data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
- data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
- data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
- data/lib/runtime_analysis/trace/configuration.rb +115 -0
- data/lib/runtime_analysis/trace/entries.rb +47 -0
- data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
- data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
- data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
- data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
- data/lib/runtime_analysis/trace/hold.rb +142 -0
- data/lib/runtime_analysis/trace/inclusion.rb +46 -0
- data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
- data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
- data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
- data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
- data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
- data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
- data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
- data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
- data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
- data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
- data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
- data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
- data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
- data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
- data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
- data/lib/runtime_analysis/trace/installer.rb +149 -0
- data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
- data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
- data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
- data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
- data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
- data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
- data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
- data/lib/runtime_analysis/trace/mechanism.rb +28 -0
- data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
- data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
- data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
- data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
- data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
- data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
- data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
- data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
- data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
- data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
- data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
- data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
- data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
- data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
- data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
- data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
- data/lib/runtime_analysis/trace/novelty.rb +17 -0
- data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
- data/lib/runtime_analysis/trace/pure/state.rb +94 -0
- data/lib/runtime_analysis/trace/pure.rb +15 -0
- data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
- data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
- data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
- data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
- data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
- data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
- data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
- data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
- data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
- data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
- data/lib/runtime_analysis/trace/recorder.rb +149 -0
- data/lib/runtime_analysis/trace/state.rb +16 -0
- data/lib/runtime_analysis/trace.rb +142 -0
- data/lib/runtime_analysis/version.rb +6 -0
- data/lib/runtime_analysis/web.rb +6 -0
- data/lib/runtime_analysis.rb +125 -0
- metadata +330 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through trace/nil_origin/stored_spec.rb, shape by shape.
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
class NilOrigin
|
|
8
|
+
class Receiver
|
|
9
|
+
Known = Struct.new(:klass, :id)
|
|
10
|
+
HELD = ::Kernel.instance_method(:instance_variable_get)
|
|
11
|
+
DEPTH = 4
|
|
12
|
+
|
|
13
|
+
def self.known(klass, id) = Known.new(klass, id)
|
|
14
|
+
|
|
15
|
+
def initialize(world, source)
|
|
16
|
+
@world = world
|
|
17
|
+
@source = source
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def of(shape, call, nil_passes:)
|
|
21
|
+
@nil_passes = nil_passes
|
|
22
|
+
known(shape, call, 0)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def known(shape, call, depth)
|
|
28
|
+
return nil if depth > DEPTH
|
|
29
|
+
return Known.new(frame(call).first, call.receiver) if shape.nil?
|
|
30
|
+
|
|
31
|
+
__send__(shape.first, shape, call, depth) if %i[ivar local call].include?(shape.first)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def ivar(shape, call, depth)
|
|
35
|
+
assigned_by(shape.fetch(1), call, depth) || held(shape.fetch(1), call) || named(shape.fetch(1))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def held(name, call)
|
|
39
|
+
object = Stored.alive(call.receiver) or return nil
|
|
40
|
+
value = HELD.bind_call(object, name)
|
|
41
|
+
recorded(Recorder.class_names.of(value), Recorder.class_names.id_of(value))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def assigned_by(name, call, depth)
|
|
45
|
+
made, assigned = assignment(name, call)
|
|
46
|
+
return unless made
|
|
47
|
+
|
|
48
|
+
by = @world.tree.call(made.tree, made.step)
|
|
49
|
+
assigned.param ? given(assigned.param, by) : shaped(assigned.shape, by, depth)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def shaped(shape, call, depth)
|
|
53
|
+
known(shape, call, depth + 1) if shape
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def assignment(name, call)
|
|
57
|
+
@world.objects.calls_on(call.receiver, ROW_CALL, call.tree, call.step).reverse_each do |made|
|
|
58
|
+
found = assigned_in(made).find { |assigned| assigned.ivar == name && assigned.always }
|
|
59
|
+
return [made, found] if found
|
|
60
|
+
end
|
|
61
|
+
nil
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def assigned_in(made)
|
|
65
|
+
_klass, name, file, line = @world.paths.frame_of(made.path)
|
|
66
|
+
@source.assigned_in(file, line, name)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def local(shape, call, depth)
|
|
70
|
+
passed(shape.fetch(1), call, depth) || named(shape.fetch(1))
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def passed(name, call, depth)
|
|
74
|
+
_klass, method, file, line = frame(call)
|
|
75
|
+
param = @source.param(file, line, method, name)
|
|
76
|
+
param ? given(param, call) : shaped(@source.local_shape(file, line, method, name), call, depth)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def given(param, call)
|
|
80
|
+
klass, id = value(param, call)
|
|
81
|
+
recorded(klass, id)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def recorded(klass, id)
|
|
85
|
+
Known.new(klass, id) if klass && (@nil_passes || !klass.eql?(NIL))
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def value(param, call)
|
|
89
|
+
values = @world.given
|
|
90
|
+
param.keyword ? values.keywords(call)[param.name.to_s] : values.arguments(call).at(param.index)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def call(shape, call, depth)
|
|
94
|
+
returned(shape.fetch(1), call) || through(shape, call, depth) || named(shape.fetch(1))
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def returned(name, call)
|
|
98
|
+
made = call.children.select { |child| answered?(child, name) }.last
|
|
99
|
+
Known.new(*@world.given.result(made)) if made
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def answered?(child, name)
|
|
103
|
+
child.returned && !child.returned_nil && frame(child).fetch(1).to_s == name
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def through(shape, call, depth)
|
|
107
|
+
owner = known(shape.fetch(2), call, depth + 1) or return nil
|
|
108
|
+
model = @world.index.model(owner.klass)&.dig("assocs", shape.fetch(1), "model")
|
|
109
|
+
Known.new(model) if model
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def named(name)
|
|
113
|
+
word = name.delete_prefix("@").split("_").map(&:capitalize).join
|
|
114
|
+
Known.new(@world.index.model_named(word))
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def frame(call) = @world.paths.frame_of(call.path)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# no-unit-test: asserted through every spec that reads a held request, since each reads its rows by these.
|
|
4
|
+
|
|
5
|
+
require "runtime_analysis/native"
|
|
6
|
+
|
|
7
|
+
module RuntimeAnalysis
|
|
8
|
+
module Trace
|
|
9
|
+
class NilOrigin
|
|
10
|
+
module Rows
|
|
11
|
+
KIND_BYTE = 0xff
|
|
12
|
+
CALL_FIXED_WORDS = 10
|
|
13
|
+
KEYWORDS_FIXED_WORDS = 7
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
def kind(word) = word & KIND_BYTE
|
|
18
|
+
|
|
19
|
+
def length(word) = word >> ROW_SHIFT
|
|
20
|
+
|
|
21
|
+
def each(words, &block)
|
|
22
|
+
at = 0
|
|
23
|
+
words.size.times { at = at < words.size ? row(words, at, &block) : break }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def row(words, at)
|
|
27
|
+
yield kind(words.fetch(at)), at
|
|
28
|
+
at + length(words.fetch(at))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def arguments(words, at)
|
|
32
|
+
values = words[(at + CALL_FIXED_WORDS)...(at + length(words.fetch(at)))]
|
|
33
|
+
[values.first(values.size / 2), values.last(values.size / 2)]
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def keywords(row)
|
|
37
|
+
values = row.drop(KEYWORDS_FIXED_WORDS)
|
|
38
|
+
values.each_slice(values.size / 3).to_a
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "runtime_analysis/ruby_parsing"
|
|
4
|
+
|
|
5
|
+
module RuntimeAnalysis
|
|
6
|
+
module Trace
|
|
7
|
+
class NilOrigin
|
|
8
|
+
class Source
|
|
9
|
+
MOST_FILES = 6
|
|
10
|
+
MOST_BYTES = 256 * 1024
|
|
11
|
+
READERS = %i[attr_reader attr_accessor attr].freeze
|
|
12
|
+
WRITERS = %i[attr_writer attr_accessor].freeze
|
|
13
|
+
WRITES = RubyParsing.kinds(:instance_variable_write_node, :instance_variable_or_write_node,
|
|
14
|
+
:instance_variable_and_write_node, :instance_variable_operator_write_node,
|
|
15
|
+
:instance_variable_target_node)
|
|
16
|
+
|
|
17
|
+
LITERALS = RubyParsing.kinds(:string_node, :symbol_node, :integer_node, :float_node, :true_node,
|
|
18
|
+
:false_node, :array_node, :hash_node)
|
|
19
|
+
|
|
20
|
+
CONTAINERS = RubyParsing.kinds(:class_node, :module_node)
|
|
21
|
+
|
|
22
|
+
Param = Struct.new(:name, :keyword, :index, :default_nil)
|
|
23
|
+
|
|
24
|
+
Assigned = Struct.new(:ivar, :line, :param, :nil_literal, :always, :shape)
|
|
25
|
+
|
|
26
|
+
class Assignments
|
|
27
|
+
def initialize(method, under)
|
|
28
|
+
@method = method
|
|
29
|
+
@under = under
|
|
30
|
+
@params = parameters
|
|
31
|
+
@top = Source.statements(method)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def param(name) = @params[name.to_sym]
|
|
35
|
+
|
|
36
|
+
def at(index) = @params.each_value.find { |param| param.index == index }
|
|
37
|
+
|
|
38
|
+
def all
|
|
39
|
+
@under.select { |node| WRITES.include?(RubyParsing.kind(node)) }.map { |node| assigned(node) }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def assigned(node)
|
|
45
|
+
value = node.value if RubyParsing.kind(node) == :instance_variable_write_node
|
|
46
|
+
line = node.start_line
|
|
47
|
+
Assigned.new(node.name.to_s, line, from(value), RubyParsing.nil_literal?(value),
|
|
48
|
+
always?(node, value, line), Shape.of(value))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def always?(node, value, line)
|
|
52
|
+
!value.nil? && @top.include?(node) && !returns_before?(line)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def from(value)
|
|
56
|
+
@params[value.name] if RubyParsing.kind(value) == :local_variable_read_node
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def returns_before?(line)
|
|
60
|
+
@under.any? { |inner| RubyParsing.kind(inner) == :return_node && inner.start_line < line }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def parameters
|
|
64
|
+
list = @method.parameters or return {}
|
|
65
|
+
(positionals(list) + list.keywords.map { |param| keyword(param) }).to_h { |param| [param.name, param] }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def positionals(list)
|
|
69
|
+
[*list.requireds, *list.optionals].each_with_index.filter_map { |param, index| positional(param, index) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def positional(param, index)
|
|
73
|
+
Param.new(param.name, false, index, default_nil?(param)) if param.respond_to?(:name)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def keyword(param)
|
|
77
|
+
Param.new(param.name, true, nil, default_nil?(param))
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def default_nil?(param)
|
|
81
|
+
return unless param.respond_to?(:value)
|
|
82
|
+
return true if RubyParsing.nil_literal?(param.value)
|
|
83
|
+
|
|
84
|
+
false if LITERALS.include?(RubyParsing.kind(param.value))
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
module Shape
|
|
89
|
+
module_function
|
|
90
|
+
|
|
91
|
+
def of(node)
|
|
92
|
+
case RubyParsing.kind(node)
|
|
93
|
+
when :nil_node then [:nil]
|
|
94
|
+
when :call_node then [:call, node.name.to_s, of(node.receiver), *(:safe if node.safe_navigation?)]
|
|
95
|
+
else variable(node)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def variable(node)
|
|
100
|
+
case RubyParsing.kind(node)
|
|
101
|
+
when :instance_variable_read_node then [:ivar, node.name.to_s]
|
|
102
|
+
when :local_variable_read_node then [:local, node.name.to_s]
|
|
103
|
+
when :constant_read_node, :constant_path_node then [:constant]
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def symbols(call)
|
|
108
|
+
call.arguments&.arguments.to_a.select { |argument| RubyParsing.symbol?(argument) }.map(&:unescaped)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def definition?(node, line, name)
|
|
112
|
+
case RubyParsing.kind(node)
|
|
113
|
+
when :def_node then node.name == name && node.start_line == line
|
|
114
|
+
when :call_node then attr?(node) && (node.start_line..node.end_line).cover?(line)
|
|
115
|
+
end
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def attr?(node)
|
|
119
|
+
node.receiver.nil? && (READERS | WRITERS).include?(node.name)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def callback?(node)
|
|
123
|
+
RubyParsing.call?(node) && node.receiver.nil? && node.name.match?(/\A(?:before|after|around)_/)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def argument(call, param)
|
|
127
|
+
arguments = call.arguments&.arguments.to_a
|
|
128
|
+
param.keyword ? keyword(arguments, param.name.to_s) : arguments.at(param.index)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def keyword(arguments, name)
|
|
132
|
+
passed = arguments.select { |each| RubyParsing.keyword_hash?(each) }.flat_map(&:elements)
|
|
133
|
+
passed.find { |pair| named?(pair, name) }&.value
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def named?(pair, name)
|
|
137
|
+
RubyParsing.pair?(pair) && RubyParsing.symbol?(pair.key) && pair.key.unescaped == name
|
|
138
|
+
end
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
module Written
|
|
142
|
+
module_function
|
|
143
|
+
|
|
144
|
+
def in(node, klass, scope = [])
|
|
145
|
+
node.compact_child_nodes.flat_map do |child|
|
|
146
|
+
inner = container?(child) ? [*scope, child.constant_path.slice] : scope
|
|
147
|
+
inner.join("::") == klass ? own(child) : self.in(child, klass, inner)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def own(node)
|
|
152
|
+
node.compact_child_nodes.reject { |child| container?(child) }.flat_map do |child|
|
|
153
|
+
RubyParsing.definition?(child) ? child : own(child)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def container?(node) = CONTAINERS.include?(RubyParsing.kind(node))
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
class Files
|
|
161
|
+
def initialize
|
|
162
|
+
@read = {}
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def nodes(file)
|
|
166
|
+
return [] unless file
|
|
167
|
+
|
|
168
|
+
@read.fetch(file) { @read[file] = @read.size < MOST_FILES ? read(file) : [] }
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
def self.under(node, found = [], inside: false)
|
|
172
|
+
return found unless node
|
|
173
|
+
|
|
174
|
+
found << node
|
|
175
|
+
children(node, inside).each { |child| under(child, found, inside: inside) }
|
|
176
|
+
found
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def self.children(node, inside)
|
|
180
|
+
children = node.compact_child_nodes
|
|
181
|
+
inside ? children.reject { |child| RubyParsing.definition?(child) } : children
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
private
|
|
185
|
+
|
|
186
|
+
def read(file)
|
|
187
|
+
return [] if File.size(file) > MOST_BYTES
|
|
188
|
+
return Files.under(RubyParsing.tree_of_file(file)) if file.end_with?(".rb", ".rake")
|
|
189
|
+
|
|
190
|
+
file.end_with?(".erb") ? embedded(File.readlines(file)) : []
|
|
191
|
+
rescue SystemCallError
|
|
192
|
+
[]
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def embedded(lines)
|
|
196
|
+
lines.each_with_index.flat_map do |text, at|
|
|
197
|
+
tags = text.scan(/<%(.+?)%>/).flatten
|
|
198
|
+
tags.flat_map { |code| Files.under(RubyParsing.tree_of(code, line: at + 1)) }
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def self.statements(method)
|
|
204
|
+
body = method.body
|
|
205
|
+
RubyParsing.statements?(body) ? body.body : []
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def initialize
|
|
209
|
+
@files = Files.new
|
|
210
|
+
@defined = {}
|
|
211
|
+
@assigned = {}
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def read_by(file, line, name)
|
|
215
|
+
found = defined_at(file, line, name) or return nil
|
|
216
|
+
RubyParsing.definition?(found) ? sole_read(found) : attr_named(found, READERS, name)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def assigned_in(file, line, name)
|
|
220
|
+
@assigned[[file, line, name]] ||= assignments_at(file, line, name)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def assignments_at(file, line, name)
|
|
224
|
+
found = defined_at(file, line, name) or return []
|
|
225
|
+
return assignments(found).all if RubyParsing.definition?(found)
|
|
226
|
+
|
|
227
|
+
[written_by(found, name.to_s, line)].compact
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def written_by(attr, name, line)
|
|
231
|
+
ivar = attr_named(attr, WRITERS, name.delete_suffix("=")) if name.end_with?("=")
|
|
232
|
+
Assigned.new(ivar, line, Param.new(:value, false, 0), false, true) if ivar
|
|
233
|
+
end
|
|
234
|
+
|
|
235
|
+
def receiver_at(file, line, called)
|
|
236
|
+
calls = nodes(file).select { |node| call_at?(node, line, called) }
|
|
237
|
+
Shape.of(calls.first.receiver) if calls.size == 1
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def returned_shape(file, line, name)
|
|
241
|
+
found = defined_at(file, line, name)
|
|
242
|
+
Shape.of(Source.statements(found).last) if RubyParsing.definition?(found)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def param(file, line, name, local)
|
|
246
|
+
found = defined_at(file, line, name)
|
|
247
|
+
assignments(found).param(local) if RubyParsing.definition?(found)
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def param_at(file, line, name, index)
|
|
251
|
+
found = defined_at(file, line, name)
|
|
252
|
+
assignments(found).at(index) if RubyParsing.definition?(found)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def assigners(file, klass, ivar)
|
|
256
|
+
nodes(file).first(1).flat_map { |root| Written.in(root, klass) }.filter_map do |method|
|
|
257
|
+
found = assigning(method, ivar)
|
|
258
|
+
[method.name, found.line] if found
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
def assigning(method, ivar)
|
|
263
|
+
assignments(method).all.find { |assigned| assigned.ivar == ivar }
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def declared(file, name)
|
|
267
|
+
call = nodes(file).find { |node| Shape.callback?(node) && Shape.symbols(node).include?(name.to_s) }
|
|
268
|
+
[call.start_line, call.slice] if call
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
def local_shape(file, line, name, local)
|
|
272
|
+
method = defined_at(file, line, name)
|
|
273
|
+
writes = under(method).select { |node| local_write?(node, local) }
|
|
274
|
+
Shape.of(writes.last&.value)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def argument_at(file, line, called, param)
|
|
278
|
+
calls = nodes(file).select { |node| RubyParsing.call?(node) && node.name.to_s == called && on?(node, line) }
|
|
279
|
+
Shape.of(Shape.argument(calls.first, param)) if calls.size == 1
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
private
|
|
283
|
+
|
|
284
|
+
def on?(node, line) = (node.message_loc || node).start_line == line
|
|
285
|
+
|
|
286
|
+
def assignments(method) = Assignments.new(method, under(method))
|
|
287
|
+
|
|
288
|
+
def defined_at(file, line, name)
|
|
289
|
+
@defined[[file, line, name]] ||= nodes(file).find { |node| Shape.definition?(node, line, name) }
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
def attr_named(call, kinds, name)
|
|
293
|
+
"@#{name}" if kinds.include?(call.name) && Shape.symbols(call).include?(name.to_s)
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def sole_read(method)
|
|
297
|
+
body = Source.statements(method)
|
|
298
|
+
body.first.name.to_s if body.size == 1 && RubyParsing.kind(body.first) == :instance_variable_read_node
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
def call_at?(node, line, called)
|
|
302
|
+
RubyParsing.call?(node) && node.name.to_s == called && !node.receiver.nil? && on?(node, line)
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
def nodes(file) = @files.nodes(file)
|
|
306
|
+
|
|
307
|
+
def local_write?(node, local)
|
|
308
|
+
RubyParsing.kind(node) == :local_variable_write_node && node.name.to_s == local
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
def under(node) = Files.under(node, inside: true)
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
end
|
|
315
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "index"
|
|
4
|
+
require_relative "receiver"
|
|
5
|
+
require_relative "writers"
|
|
6
|
+
|
|
7
|
+
module RuntimeAnalysis
|
|
8
|
+
module Trace
|
|
9
|
+
class NilOrigin
|
|
10
|
+
class Stored
|
|
11
|
+
def self.alive(id) = Row.alive(id)
|
|
12
|
+
|
|
13
|
+
def initialize(index)
|
|
14
|
+
@index = index
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def cause(owner, attr)
|
|
18
|
+
klass = owner.klass || sole_model_with(attr)
|
|
19
|
+
facts = @index.model(klass) or return nil
|
|
20
|
+
Attribute.new(@index, klass, facts, attr, Row.find(owner.id)).told
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def sole_model_with(attr)
|
|
26
|
+
found = @index.models_with(attr)
|
|
27
|
+
found.first if found.size == 1
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module Row
|
|
31
|
+
module_function
|
|
32
|
+
|
|
33
|
+
def find(id)
|
|
34
|
+
object = alive(id)
|
|
35
|
+
object if object.respond_to?(:read_attribute_before_type_cast)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def alive(id)
|
|
39
|
+
ObjectSpace._id2ref(id) if id
|
|
40
|
+
rescue RangeError, NoMethodError
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def set?(row, name)
|
|
45
|
+
!row.read_attribute_before_type_cast(name).nil?
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def facts(row, added_at)
|
|
49
|
+
created, updated = %w[created_at updated_at].map { |name| row.read_attribute(name) }
|
|
50
|
+
return [] unless created && updated
|
|
51
|
+
|
|
52
|
+
[("created_before_column_added" if before?(created, added_at)), since(created, updated)].compact
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def since(created, updated)
|
|
56
|
+
created == updated ? "never_updated" : "updated_after_created"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def before?(created, added_at)
|
|
60
|
+
stamp = added_at.to_s
|
|
61
|
+
stamp.size == 14 && created.utc.strftime("%Y%m%d%H%M%S") < stamp
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
class Attribute
|
|
66
|
+
def initialize(index, klass, facts, attr, row)
|
|
67
|
+
@index = index
|
|
68
|
+
@klass = klass
|
|
69
|
+
@facts = facts
|
|
70
|
+
@attr = attr
|
|
71
|
+
@row = row
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def told
|
|
75
|
+
return unsaved if @row.respond_to?(:new_record?) && @row.new_record?
|
|
76
|
+
|
|
77
|
+
assoc = @index.association(@klass, @attr)
|
|
78
|
+
assoc ? linked(assoc) : value
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
82
|
+
|
|
83
|
+
def table = @facts.fetch("table")
|
|
84
|
+
|
|
85
|
+
def unsaved
|
|
86
|
+
{ kind: "never_assigned",
|
|
87
|
+
because: "this #{@klass} was built in this request and is not saved, and was given no #{@attr}" }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def linked(assoc)
|
|
91
|
+
association(assoc) unless assoc.fetch("kind") == "has_many"
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def association(assoc)
|
|
95
|
+
return other_side(assoc) unless assoc.fetch("kind") == "belongs_to"
|
|
96
|
+
return unread(assoc.fetch("key")) unless @row
|
|
97
|
+
|
|
98
|
+
key = assoc.fetch("key")
|
|
99
|
+
Row.set?(@row, key) ? gone(key, assoc.fetch("model")) : null(key)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def other_side(assoc)
|
|
103
|
+
{ kind: "nothing_matched", because: "no row of #{assoc.fetch("model")} belongs to this #{@klass}" }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def gone(key, model)
|
|
107
|
+
{ kind: "nothing_matched", because: "#{table}.#{key} is set and no row of #{model} has that id" }
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def unread(key)
|
|
111
|
+
return unless nullable(key)
|
|
112
|
+
|
|
113
|
+
{ because: "#{table}.#{key} could not be read: it is NULL, or set with no such row",
|
|
114
|
+
writers: writers(key, []) }
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def value
|
|
118
|
+
return null(@attr) unless @facts["enums"].to_a.include?(@attr)
|
|
119
|
+
return unless @row
|
|
120
|
+
|
|
121
|
+
Row.set?(@row, @attr) ? unnamed : null(@attr)
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def unnamed
|
|
125
|
+
{ kind: "nothing_matched", because: "#{table}.#{@attr} holds a value the enum does not name" }
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def nullable(column)
|
|
129
|
+
facts = @facts.fetch("columns")[column]
|
|
130
|
+
facts if facts && facts["null"]
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def null(column)
|
|
134
|
+
facts = nullable(column) or return nil
|
|
135
|
+
stored(column, facts, @row ? Row.facts(@row, facts["added_at"]) : [])
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def stored(column, facts, row)
|
|
139
|
+
{ kind: "stored_null", because: "#{table}.#{column} #{read}", schema: schema(facts),
|
|
140
|
+
row: (row unless row.empty?), audit: @facts["audit"], writers: writers(column, row) }
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
def read
|
|
144
|
+
return "is NULL in this #{@klass}" if @row
|
|
145
|
+
|
|
146
|
+
"read nil on this #{@klass}; no row was read, so it is NULL if the record was saved"
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
def writers(column, row) = Writers.new(@index.writers(@klass, column), row).told
|
|
150
|
+
|
|
151
|
+
def schema(facts)
|
|
152
|
+
{ nullable: true, default: facts["default"] == true, added_by: facts["added_by"]&.transform_keys(&:to_sym),
|
|
153
|
+
backfilled: facts["backfilled"] }.compact
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|