serega 0.42.0 → 0.51.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 +4 -4
- data/README.md +124 -1207
- data/VERSION +1 -1
- data/lib/serega/attribute/serializer_reference.rb +27 -0
- data/lib/serega/attribute/value_code.rb +63 -0
- data/lib/serega/attribute.rb +173 -140
- data/lib/serega/attribute_normalizer.rb +236 -391
- data/lib/serega/attribute_normalizers/batch.rb +45 -0
- data/lib/serega/attribute_normalizers/block.rb +81 -0
- data/lib/serega/attribute_normalizers/delegate.rb +34 -0
- data/lib/serega/attribute_normalizers/hash_access.rb +37 -0
- data/lib/serega/attribute_normalizers/preloads.rb +47 -0
- data/lib/serega/attribute_value_resolvers/base.rb +35 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +18 -54
- data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
- data/lib/serega/attribute_value_resolvers/const.rb +12 -24
- data/lib/serega/attribute_value_resolvers/delegate.rb +27 -28
- data/lib/serega/attribute_value_resolvers/hash_access.rb +49 -52
- data/lib/serega/attribute_value_resolvers/keyword.rb +17 -25
- data/lib/serega/attribute_values.rb +32 -0
- data/lib/serega/batch_loader.rb +37 -0
- data/lib/serega/conditions.rb +81 -0
- data/lib/serega/config.rb +148 -148
- data/lib/serega/engine/pulls.rb +51 -0
- data/lib/serega/engine/run.rb +101 -0
- data/lib/serega/errors.rb +60 -8
- data/lib/serega/formatters/callable.rb +44 -0
- data/lib/serega/formatters/method.rb +55 -0
- data/lib/serega/formatters.rb +27 -0
- data/lib/serega/plan/cache.rb +119 -0
- data/lib/serega/plan/depth_limit.rb +36 -0
- data/lib/serega/plan/per_ractor_cache.rb +28 -0
- data/lib/serega/plan/point.rb +67 -0
- data/lib/serega/plan/presets.rb +116 -0
- data/lib/serega/plan/selection.rb +94 -0
- data/lib/serega/plan/selection_planner.rb +50 -0
- data/lib/serega/plan.rb +92 -133
- data/lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb +13 -80
- data/lib/serega/plugins/activerecord_preloads/lib/preloader.rb +8 -75
- data/lib/serega/plugins/context_metadata/context_metadata.rb +40 -123
- data/lib/serega/plugins/metadata/check_params.rb +80 -0
- data/lib/serega/plugins/metadata/meta_attribute.rb +53 -76
- data/lib/serega/plugins/metadata/metadata.rb +38 -175
- data/lib/serega/plugins/root/root.rb +94 -235
- data/lib/serega/plugins.rb +16 -49
- data/lib/serega/presenter.rb +81 -19
- data/lib/serega/result_builder.rb +68 -0
- data/lib/serega/result_code/attribute_point_code.rb +117 -0
- data/lib/serega/result_code/point_code.rb +54 -0
- data/lib/serega/result_code/relation_point_code.rb +112 -0
- data/lib/serega/result_code.rb +159 -0
- data/lib/serega/shareable.rb +52 -0
- data/lib/serega/source_group.rb +156 -0
- data/lib/serega/utils/camel_case.rb +22 -0
- data/lib/serega/utils/collection_detector.rb +20 -13
- data/lib/serega/utils/deep_merge.rb +26 -0
- data/lib/serega/utils/enum_deep_dup.rb +10 -16
- data/lib/serega/utils/enum_deep_freeze.rb +8 -14
- data/lib/serega/utils/method_signature.rb +6 -15
- data/lib/serega/utils/parse_string_selection.rb +63 -0
- data/lib/serega/utils/ractor_local.rb +36 -0
- data/lib/serega/utils/ruby_literal.rb +28 -0
- data/lib/serega/utils/serialized_attribute_error.rb +60 -20
- data/lib/serega/utils/to_hash.rb +5 -26
- data/lib/serega/validations/attribute/check_block.rb +3 -15
- data/lib/serega/validations/attribute/check_name.rb +4 -15
- data/lib/serega/validations/attribute/check_opt_base_serializer.rb +3 -15
- data/lib/serega/validations/attribute/check_opt_batch.rb +26 -56
- data/lib/serega/validations/attribute/check_opt_condition.rb +55 -0
- data/lib/serega/validations/attribute/check_opt_delegate.rb +10 -63
- data/lib/serega/validations/attribute/check_opt_format.rb +36 -0
- data/lib/serega/validations/attribute/check_opt_hash_access.rb +11 -59
- data/lib/serega/validations/attribute/check_opt_hide.rb +3 -14
- data/lib/serega/validations/attribute/check_opt_many.rb +18 -20
- data/lib/serega/validations/attribute/check_opt_method.rb +3 -25
- data/lib/serega/validations/attribute/check_opt_preload.rb +3 -26
- data/lib/serega/validations/attribute/check_opt_preset.rb +34 -0
- data/lib/serega/validations/attribute/check_opt_selection.rb +41 -0
- data/lib/serega/validations/attribute/check_opt_serializer.rb +4 -18
- data/lib/serega/validations/attribute/check_opt_value.rb +3 -21
- data/lib/serega/validations/check_attribute_params.rb +92 -71
- data/lib/serega/validations/check_batch_loader_params.rb +43 -62
- data/lib/serega/validations/check_definition_selection.rb +21 -0
- data/lib/serega/validations/check_formatter.rb +80 -0
- data/lib/serega/validations/check_preset_params.rb +103 -0
- data/lib/serega/validations/check_relation_selection.rb +31 -0
- data/lib/serega/validations/check_selection.rb +89 -0
- data/lib/serega/validations/check_serialize_keywords.rb +23 -0
- data/lib/serega/validations/utils/check_allowed_keys.rb +3 -13
- data/lib/serega/validations/utils/check_hash_access_value.rb +44 -0
- data/lib/serega/validations/utils/check_opt_is_bool.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_hash.rb +3 -13
- data/lib/serega/validations/utils/check_opt_is_string_or_symbol.rb +3 -13
- data/lib/serega/version.rb +1 -4
- data/lib/serega.rb +525 -390
- data/sig/serega/plugins.rbs +49 -0
- data/sig/serega.rbs +158 -0
- metadata +64 -40
- data/lib/serega/data_builder.rb +0 -54
- data/lib/serega/engine/level.rb +0 -59
- data/lib/serega/engine/level_queue.rb +0 -56
- data/lib/serega/engine/loader.rb +0 -70
- data/lib/serega/helpers/serializer_class_helper.rb +0 -19
- data/lib/serega/object_serializer.rb +0 -125
- data/lib/serega/plan_point.rb +0 -165
- data/lib/serega/plugins/camel_case/camel_case.rb +0 -213
- data/lib/serega/plugins/depth_limit/depth_limit.rb +0 -206
- data/lib/serega/plugins/explicit_many_option/explicit_many_option.rb +0 -69
- data/lib/serega/plugins/explicit_many_option/validations/check_opt_many.rb +0 -38
- data/lib/serega/plugins/formatters/formatters.rb +0 -322
- data/lib/serega/plugins/if/if.rb +0 -287
- data/lib/serega/plugins/if/validations/check_opt_if.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_if_value.rb +0 -77
- data/lib/serega/plugins/if/validations/check_opt_unless.rb +0 -72
- data/lib/serega/plugins/if/validations/check_opt_unless_value.rb +0 -77
- data/lib/serega/plugins/metadata/validations/check_block.rb +0 -47
- data/lib/serega/plugins/metadata/validations/check_opt_const.rb +0 -39
- data/lib/serega/plugins/metadata/validations/check_opt_hide_empty.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_hide_nil.rb +0 -35
- data/lib/serega/plugins/metadata/validations/check_opt_value.rb +0 -61
- data/lib/serega/plugins/metadata/validations/check_opts.rb +0 -59
- data/lib/serega/plugins/metadata/validations/check_path.rb +0 -50
- data/lib/serega/plugins/string_modifiers/parse_string_modifiers.rb +0 -111
- data/lib/serega/plugins/string_modifiers/string_modifiers.rb +0 -63
- data/lib/serega/utils/symbol_name.rb +0 -39
- data/lib/serega/validations/attribute/check_opt_const.rb +0 -40
- data/lib/serega/validations/check_initiate_params.rb +0 -57
- data/lib/serega/validations/check_serialize_params.rb +0 -54
- data/lib/serega/validations/initiate/check_modifiers.rb +0 -93
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Generates the Ruby code of the `SeregaResultBuilder#call` method of one
|
|
6
|
+
# plan in one serialization mode. The method reads all values of a source,
|
|
7
|
+
# then makes its serialized object in one step: a Hash literal,
|
|
8
|
+
# `Struct.new` or `Data.new`. The code reads plain attributes directly,
|
|
9
|
+
# for example `source.name`.
|
|
10
|
+
#
|
|
11
|
+
# Points without relations and batch loaders get a `#call_one(source,
|
|
12
|
+
# context)` method too. It returns the serialized object of one source,
|
|
13
|
+
# without the Array and the loop of `#call`.
|
|
14
|
+
#
|
|
15
|
+
# Generated code for a plan with `attribute :id` and
|
|
16
|
+
# `attribute :posts, serializer: PostSerializer` in the :hash mode:
|
|
17
|
+
#
|
|
18
|
+
# def call(sources, context, batches, relations)
|
|
19
|
+
# relation_values_1 = relations[@points[1]]
|
|
20
|
+
# size = sources.size
|
|
21
|
+
# serialized = Array.new(size)
|
|
22
|
+
# source_index = 0
|
|
23
|
+
# while source_index < size
|
|
24
|
+
# source = sources[source_index]
|
|
25
|
+
# value_0 =
|
|
26
|
+
# begin
|
|
27
|
+
# source.id
|
|
28
|
+
# rescue => error
|
|
29
|
+
# Serega::SeregaUtils::SerializedAttributeError.call(error, @points[0])
|
|
30
|
+
# end
|
|
31
|
+
# value_1 = relation_values_1[source_index]
|
|
32
|
+
# serialized[source_index] = {:id => value_0, :posts => value_1}
|
|
33
|
+
# source_index += 1
|
|
34
|
+
# end
|
|
35
|
+
# serialized
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
class SeregaResultCode # :nodoc:
|
|
39
|
+
# Method names called directly in generated code
|
|
40
|
+
PLAIN_METHOD_NAME = /\A[a-z_][a-zA-Z0-9_]*[?!]?\z/
|
|
41
|
+
|
|
42
|
+
# Serialization mode
|
|
43
|
+
attr_reader :mode
|
|
44
|
+
|
|
45
|
+
# `compact` leaves out the keys with `nil` values from serialized Hashes
|
|
46
|
+
def initialize(mode, points, compact: false)
|
|
47
|
+
@mode = mode
|
|
48
|
+
@compact = compact
|
|
49
|
+
@point_codes = points.each_with_index.map do |point, index|
|
|
50
|
+
point.child_plan ? RelationPointCode.new(point, index) : AttributePointCode.new(point, index)
|
|
51
|
+
end
|
|
52
|
+
@relation_codes = @point_codes.grep(RelationPointCode)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Returns the code of the `#call` method, and of the `#call_one`
|
|
56
|
+
# method for points without relations and batch loaders. The methods
|
|
57
|
+
# have local variables for the points, and they have the point index
|
|
58
|
+
# in their names:
|
|
59
|
+
# - `attribute_N` and `batches_N` read values with `SeregaAttribute#value`.
|
|
60
|
+
# - `conditions_N` checks the conditions of a conditional attribute.
|
|
61
|
+
# - `relation_values_N` holds the relation values of all sources.
|
|
62
|
+
# - `value_N` holds the value of the current source.
|
|
63
|
+
def to_s
|
|
64
|
+
variables = variables_code.join("\n ")
|
|
65
|
+
values = values_code
|
|
66
|
+
[call_code(variables, values), collect_code, call_one_code(variables, values)].compact.join("\n")
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
# A `while` loop fills an Array made with the size of the sources: no
|
|
72
|
+
# block call and no Array growth per source.
|
|
73
|
+
def call_code(variables, values)
|
|
74
|
+
<<~RUBY
|
|
75
|
+
def call(sources, context, batches, relations)
|
|
76
|
+
#{variables}
|
|
77
|
+
size = sources.size
|
|
78
|
+
serialized = Array.new(size)
|
|
79
|
+
source_index = 0
|
|
80
|
+
while source_index < size
|
|
81
|
+
source = sources[source_index]
|
|
82
|
+
#{[*values, *build_serialized_code("serialized[source_index] = ")].join("\n").gsub("\n", "\n ")}
|
|
83
|
+
source_index += 1
|
|
84
|
+
end
|
|
85
|
+
serialized
|
|
86
|
+
end
|
|
87
|
+
RUBY
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# One loop per relation point, in the order of the relation points.
|
|
91
|
+
# Returns the child sources and the pulls of each relation point, one
|
|
92
|
+
# after another. nil without relation points.
|
|
93
|
+
def collect_code
|
|
94
|
+
return if @relation_codes.empty?
|
|
95
|
+
|
|
96
|
+
loops = @relation_codes.map(&:collect)
|
|
97
|
+
collected = @relation_codes.map(&:collected)
|
|
98
|
+
<<~RUBY
|
|
99
|
+
def collect(sources, context, batches)
|
|
100
|
+
size = sources.size
|
|
101
|
+
#{loops.join("\n").gsub("\n", "\n ")}
|
|
102
|
+
[#{collected.join(", ")}]
|
|
103
|
+
end
|
|
104
|
+
RUBY
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# nil when a point needs the source group
|
|
108
|
+
def call_one_code(variables, values)
|
|
109
|
+
return unless @point_codes.all?(&:call_one?)
|
|
110
|
+
|
|
111
|
+
<<~RUBY
|
|
112
|
+
def call_one(source, context)
|
|
113
|
+
#{variables}
|
|
114
|
+
#{[*values, *build_serialized_code("")].join("\n").gsub("\n", "\n ")}
|
|
115
|
+
end
|
|
116
|
+
RUBY
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Code that sets the local variables of a method: only the variables
|
|
120
|
+
# that the code of the points reads
|
|
121
|
+
def variables_code
|
|
122
|
+
point_variables = @point_codes.flat_map(&:variables)
|
|
123
|
+
return point_variables if mode == :hash
|
|
124
|
+
|
|
125
|
+
["result_class = @result_class", *point_variables]
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Code that reads all values of the source, one entry per point
|
|
129
|
+
def values_code
|
|
130
|
+
@point_codes.map(&:assign_value)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Code that builds the serialized object of the source. `assign` is the
|
|
134
|
+
# code that takes the serialized object, empty for the return value.
|
|
135
|
+
def build_serialized_code(assign)
|
|
136
|
+
return build_serialized_hash_code(assign) if mode == :hash
|
|
137
|
+
|
|
138
|
+
arguments = @point_codes.map(&:argument)
|
|
139
|
+
["#{assign}result_class.new(#{arguments.join(", ")})"]
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Code that builds the serialized Hash of the source, after `assign`.
|
|
143
|
+
# A Hash literal holds the values up to the first value that can leave
|
|
144
|
+
# out its key: a conditional value, or any value of a compact Hash. Then
|
|
145
|
+
# the code adds the other values one by one, thus the Hash keys keep the
|
|
146
|
+
# order of the points.
|
|
147
|
+
def build_serialized_hash_code(assign)
|
|
148
|
+
compact = @compact
|
|
149
|
+
size = @point_codes.size
|
|
150
|
+
literal_size = compact ? 0 : (@point_codes.index(&:conditional?) || size)
|
|
151
|
+
pairs = @point_codes.first(literal_size).map(&:hash_pair)
|
|
152
|
+
literal = "{#{pairs.join(", ")}}"
|
|
153
|
+
return ["#{assign}#{literal}"] if literal_size == size
|
|
154
|
+
|
|
155
|
+
assigns = @point_codes.drop(literal_size).map { |point_code| point_code.hash_assign(compact) }
|
|
156
|
+
["serialized_hash = #{literal}", *assigns, "#{assign}serialized_hash"]
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Makes a serializer shareable between Ractors
|
|
6
|
+
#
|
|
7
|
+
class SeregaShareable # :nodoc:
|
|
8
|
+
class << self
|
|
9
|
+
# Makes the serializer definitions shareable between Ractors, except the
|
|
10
|
+
# plans cache. A definition that can not be shared stays as it is.
|
|
11
|
+
def call(serializer_class)
|
|
12
|
+
reasons = []
|
|
13
|
+
|
|
14
|
+
serializer_class.attributes.each_value do |attribute|
|
|
15
|
+
share(attribute, reasons) do |message|
|
|
16
|
+
"Attribute :#{attribute.name} of #{serializer_class} can not be shared between Ractors (#{attribute.location}): #{message}"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
serializer_class.batch_loaders.each_value do |loader|
|
|
21
|
+
share(loader, reasons) { |message| "Batch loader :#{loader.name} of #{serializer_class} can not be shared between Ractors: #{message}" }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
serializer_class.formatters.each do |name, formatter|
|
|
25
|
+
share(formatter, reasons) { |message| "Formatter :#{name} of #{serializer_class} can not be shared between Ractors: #{message}" }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
(serializer_class.instance_variables - [:@plan_cache]).each do |name|
|
|
29
|
+
value = serializer_class.instance_variable_get(name)
|
|
30
|
+
share(value, reasons) { |message| "#{serializer_class} can not be shared between Ractors: #{message}" }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
share_constants(serializer_class::SeregaAttributeValues, reasons)
|
|
34
|
+
reasons.first
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def share_constants(values_class, reasons)
|
|
40
|
+
values_class.constants(false).each do |name|
|
|
41
|
+
share(values_class.const_get(name, false), reasons) { |message| "#{values_class} can not be shared between Ractors: #{message}" }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def share(value, reasons)
|
|
46
|
+
Ractor.make_shareable(value)
|
|
47
|
+
rescue Ractor::Error, ArgumentError => error
|
|
48
|
+
reasons << -yield(error.message)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# All sources that one plan serializes in one run, for example the posts of
|
|
6
|
+
# all users. A source is an object to serialize. A serialized object is its
|
|
7
|
+
# output: a Hash, Struct or Data object.
|
|
8
|
+
#
|
|
9
|
+
# The group serializes its sources in two steps:
|
|
10
|
+
# - `#discover` runs the preloads, reads the relation sources with the
|
|
11
|
+
# generated `#collect` of the result builder and adds one child group per
|
|
12
|
+
# relation.
|
|
13
|
+
# - `#build` builds the serialized objects. It runs after the child groups
|
|
14
|
+
# are built, thus the relation values are ready.
|
|
15
|
+
#
|
|
16
|
+
class SeregaSourceGroup # :nodoc:
|
|
17
|
+
# Serialization run
|
|
18
|
+
attr_reader :run
|
|
19
|
+
|
|
20
|
+
# Serialization plan
|
|
21
|
+
attr_reader :plan
|
|
22
|
+
|
|
23
|
+
# Serialization context
|
|
24
|
+
attr_reader :context
|
|
25
|
+
|
|
26
|
+
# Sources (or their presenters)
|
|
27
|
+
attr_reader :sources
|
|
28
|
+
|
|
29
|
+
# Serialized object of each
|
|
30
|
+
# source, after #build
|
|
31
|
+
attr_reader :serialized
|
|
32
|
+
|
|
33
|
+
# Pulls from the generated `#collect` of the parent group, one per
|
|
34
|
+
# source of the parent group
|
|
35
|
+
attr_reader :pulls
|
|
36
|
+
|
|
37
|
+
# Each source is wrapped in the presenter of the plan, when it has one,
|
|
38
|
+
# so value reading and batch loaders alike see presenters.
|
|
39
|
+
def initialize(run, plan, sources, pulls)
|
|
40
|
+
@run = run
|
|
41
|
+
@plan = plan
|
|
42
|
+
@context = run.context
|
|
43
|
+
presenter = plan.presenter
|
|
44
|
+
@sources = presenter ? sources.map { |source| presenter.new(source, @context) } : sources
|
|
45
|
+
@pulls = pulls
|
|
46
|
+
@child_groups = nil
|
|
47
|
+
@serialized = nil
|
|
48
|
+
@loaded_batches = nil
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Runs the preloads, and makes one child group per relation point, in the
|
|
52
|
+
# order of the relation points. The generated `#collect` reads the
|
|
53
|
+
# relation sources, and gives the child sources and the pulls of each
|
|
54
|
+
# relation point.
|
|
55
|
+
def discover
|
|
56
|
+
run_preloads
|
|
57
|
+
|
|
58
|
+
relation_points = plan.relation_points
|
|
59
|
+
return FROZEN_EMPTY_ARRAY if relation_points.empty?
|
|
60
|
+
|
|
61
|
+
collected = plan.result_builder(run.mode).collect(@sources, @context, relation_batches)
|
|
62
|
+
@child_groups = child_groups(relation_points, collected)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Builds the serialized objects.
|
|
66
|
+
def build
|
|
67
|
+
batches = plan.points.map { |point| batches_for(point) } if plan.batch_points?
|
|
68
|
+
@serialized = plan.result_builder(run.mode).call(@sources, @context, batches, relations)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Serialized value of a root group, after #build: one serialized object
|
|
72
|
+
# for a SINGLE_SOURCE pull, or all serialized objects
|
|
73
|
+
def root_value
|
|
74
|
+
serialized = @serialized
|
|
75
|
+
SeregaEngine::Pulls::SINGLE_SOURCE.equal?(@pulls[0]) ? serialized[0] : serialized
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# Relation values of each relation point, taken from its built child
|
|
81
|
+
# group, or nil without relation points
|
|
82
|
+
def relations
|
|
83
|
+
child_groups = @child_groups
|
|
84
|
+
return unless child_groups
|
|
85
|
+
|
|
86
|
+
relation_points = plan.relation_points
|
|
87
|
+
relations = {}
|
|
88
|
+
index = 0
|
|
89
|
+
|
|
90
|
+
while index < relation_points.size
|
|
91
|
+
child_group = child_groups[index]
|
|
92
|
+
relations[relation_points[index]] = SeregaEngine::Pulls.take!(child_group.serialized, child_group.pulls)
|
|
93
|
+
index += 1
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
relations
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Runs the preloads of all points with the serializer's preload handler.
|
|
100
|
+
# Preload handlers get the sources without presenters.
|
|
101
|
+
def run_preloads
|
|
102
|
+
points = plan.preload_points
|
|
103
|
+
return if points.empty?
|
|
104
|
+
|
|
105
|
+
serializer_class = plan.serializer_class
|
|
106
|
+
handler = serializer_class.preload_with
|
|
107
|
+
sources = plan.presenter ? @sources.map(&:__getobj__) : @sources
|
|
108
|
+
|
|
109
|
+
points.each do |point|
|
|
110
|
+
unless handler
|
|
111
|
+
raise SeregaError, "The :preload option requires a preload handler. Register one with `preload_with` (the :activerecord_preloads plugin does this for you)."
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
handler.call(sources, point.preloads)
|
|
115
|
+
rescue => error
|
|
116
|
+
SeregaUtils::SerializedAttributeError.call(error, point)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# One child group per relation point, with the child sources and the
|
|
121
|
+
# pulls that `#collect` gave for it
|
|
122
|
+
def child_groups(relation_points, collected)
|
|
123
|
+
Array.new(relation_points.size) do |position|
|
|
124
|
+
offset = position * 2
|
|
125
|
+
run.new_source_group(relation_points[position].child_plan, collected[offset], collected[offset + 1])
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Loaded batches of the relation points, by point index, or nil
|
|
130
|
+
# without batch points. Other points load their batches in #build.
|
|
131
|
+
def relation_batches
|
|
132
|
+
return unless plan.batch_points?
|
|
133
|
+
|
|
134
|
+
plan.points.map { |point| batches_for(point) if point.child_plan }
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Loads the batch loaders of the point, each once for all sources of
|
|
138
|
+
# the group. An attribute with an inline batch loader uses it, other
|
|
139
|
+
# attributes use the named batch loaders of the serializer. Returns nil
|
|
140
|
+
# when the point has no batch loaders.
|
|
141
|
+
def batches_for(point)
|
|
142
|
+
names = point.batch_loaders
|
|
143
|
+
return if names.empty?
|
|
144
|
+
|
|
145
|
+
inline_loader = point.attribute.inline_batch_loader
|
|
146
|
+
loaders = plan.serializer_class.batch_loaders
|
|
147
|
+
loaded_batches = (@loaded_batches ||= {}.compare_by_identity)
|
|
148
|
+
names.to_h do |name|
|
|
149
|
+
loader = inline_loader || loaders[name]
|
|
150
|
+
[name, loaded_batches[loader] ||= loader.load(@sources, @context)]
|
|
151
|
+
end
|
|
152
|
+
rescue => error
|
|
153
|
+
SeregaUtils::SerializedAttributeError.call(error, point)
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Utilities
|
|
6
|
+
#
|
|
7
|
+
module SeregaUtils # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Utility to write snake_case names in camelCase
|
|
10
|
+
#
|
|
11
|
+
class CamelCase # :nodoc:
|
|
12
|
+
class << self
|
|
13
|
+
# Returns the name in camelCase: drops each underscore before a
|
|
14
|
+
# lowercase letter or a digit and upcases the letter. Other characters
|
|
15
|
+
# stay as they are.
|
|
16
|
+
def call(name)
|
|
17
|
+
name.to_s.gsub(/_[a-z\d]/) { |match| match[1].upcase }.to_sym
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Serega
|
|
4
|
-
#
|
|
5
|
-
module SeregaUtils
|
|
4
|
+
module SeregaUtils # :nodoc:
|
|
6
5
|
#
|
|
7
6
|
# Utility to check if an object should be serialized as a collection.
|
|
8
7
|
#
|
|
9
8
|
# Hashes and Structs are Enumerable, but enumerate their own member
|
|
10
9
|
# values, so they are treated as single objects.
|
|
11
10
|
#
|
|
12
|
-
#
|
|
13
|
-
|
|
11
|
+
# Callers check `instance_of?(Array)` before they call the detector, thus
|
|
12
|
+
# an Array needs no lookup.
|
|
13
|
+
#
|
|
14
|
+
class CollectionDetector # :nodoc:
|
|
15
|
+
# Number of classes a Hash of known classes keeps
|
|
16
|
+
MAX_KNOWN_CLASSES = 8
|
|
17
|
+
|
|
14
18
|
class << self
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
# Checks if provided object is a collection of objects. Remembers the
|
|
20
|
+
# answer for the class of the object in `known_classes`, up to
|
|
21
|
+
# MAX_KNOWN_CLASSES classes: `is_a?(Enumerable)` checks all
|
|
22
|
+
# ancestors of a class that does not include Enumerable.
|
|
23
|
+
def call(object, known_classes)
|
|
24
|
+
object_class = object.class
|
|
25
|
+
collection = known_classes[object_class]
|
|
26
|
+
return collection unless collection.nil?
|
|
27
|
+
|
|
28
|
+
collection = object.is_a?(Enumerable) && !object.is_a?(Hash) && !object.is_a?(Struct)
|
|
29
|
+
known_classes[object_class] = collection if known_classes.size < MAX_KNOWN_CLASSES
|
|
30
|
+
collection
|
|
24
31
|
end
|
|
25
32
|
end
|
|
26
33
|
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaUtils # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Merges nested hashes
|
|
7
|
+
#
|
|
8
|
+
class DeepMerge # :nodoc:
|
|
9
|
+
class << self
|
|
10
|
+
# Merges `other` into `hash` in place. Nested hashes merge into new
|
|
11
|
+
# hashes, thus `other` stays unchanged. Other values of `other` win.
|
|
12
|
+
def call(hash, other)
|
|
13
|
+
hash.merge!(other) { |_key, this_val, other_val| merge_values(this_val, other_val) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def merge_values(this_val, other_val)
|
|
19
|
+
return other_val unless this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
|
20
|
+
|
|
21
|
+
this_val.merge(other_val) { |_key, this_nested, other_nested| merge_values(this_nested, other_nested) }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -4,23 +4,15 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Utilities
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module SeregaUtils
|
|
7
|
+
module SeregaUtils # :nodoc:
|
|
9
8
|
#
|
|
10
|
-
# Duplicates nested hashes and
|
|
11
|
-
# It does not duplicate any
|
|
9
|
+
# Duplicates nested hashes, arrays and strings
|
|
10
|
+
# It does not duplicate any other values
|
|
12
11
|
#
|
|
13
|
-
#
|
|
14
|
-
class EnumDeepDup
|
|
12
|
+
class EnumDeepDup # :nodoc:
|
|
15
13
|
class << self
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
# It does not duplicate any non-Array and non-Hash values
|
|
19
|
-
#
|
|
20
|
-
# @param data [Hash, Array] Data to duplicate
|
|
21
|
-
#
|
|
22
|
-
# @return [Hash, Array] Duplicated data
|
|
23
|
-
#
|
|
14
|
+
# Deeply duplicate provided Array, Hash or String data
|
|
15
|
+
# It does not duplicate any other values
|
|
24
16
|
def call(data)
|
|
25
17
|
case data
|
|
26
18
|
when Hash
|
|
@@ -30,9 +22,11 @@ class Serega
|
|
|
30
22
|
when Array
|
|
31
23
|
data = data.dup
|
|
32
24
|
dup_array_values(data)
|
|
25
|
+
when String
|
|
26
|
+
data.dup
|
|
27
|
+
else
|
|
28
|
+
data
|
|
33
29
|
end
|
|
34
|
-
|
|
35
|
-
data
|
|
36
30
|
end
|
|
37
31
|
|
|
38
32
|
private
|
|
@@ -4,21 +4,13 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Utilities
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module SeregaUtils
|
|
7
|
+
module SeregaUtils # :nodoc:
|
|
9
8
|
#
|
|
10
|
-
# Utility to freeze nested hashes and
|
|
9
|
+
# Utility to freeze nested hashes, arrays and strings
|
|
11
10
|
#
|
|
12
|
-
#
|
|
13
|
-
class EnumDeepFreeze
|
|
11
|
+
class EnumDeepFreeze # :nodoc:
|
|
14
12
|
class << self
|
|
15
|
-
#
|
|
16
|
-
# Freezes nested hashes and arrays
|
|
17
|
-
#
|
|
18
|
-
# @param data[Hash, Array] data to freeze
|
|
19
|
-
#
|
|
20
|
-
# @return [Hash, Array] same deeply frozen data
|
|
21
|
-
#
|
|
13
|
+
# Freezes nested hashes and arrays, replaces strings with frozen copies
|
|
22
14
|
def call(data)
|
|
23
15
|
case data
|
|
24
16
|
when Hash
|
|
@@ -27,9 +19,11 @@ class Serega
|
|
|
27
19
|
when Array
|
|
28
20
|
data.map! { |value| call(value) }
|
|
29
21
|
data.freeze
|
|
22
|
+
when String
|
|
23
|
+
-data
|
|
24
|
+
else
|
|
25
|
+
data
|
|
30
26
|
end
|
|
31
|
-
|
|
32
|
-
data
|
|
33
27
|
end
|
|
34
28
|
end
|
|
35
29
|
end
|
|
@@ -4,31 +4,22 @@ class Serega
|
|
|
4
4
|
#
|
|
5
5
|
# Utilities
|
|
6
6
|
#
|
|
7
|
-
#
|
|
8
|
-
module SeregaUtils
|
|
7
|
+
module SeregaUtils # :nodoc:
|
|
9
8
|
#
|
|
10
9
|
# Utility to make method arguments signature
|
|
11
10
|
#
|
|
12
|
-
#
|
|
13
|
-
class MethodSignature
|
|
11
|
+
class MethodSignature # :nodoc:
|
|
14
12
|
SYMBOL_TO_PROC_SIGNATURE_RUBY2 = [[:rest]]
|
|
15
13
|
SYMBOL_TO_PROC_SIGNATURE_RUBY3 = [[:req], [:rest]]
|
|
16
14
|
private_constant :SYMBOL_TO_PROC_SIGNATURE_RUBY2
|
|
17
15
|
private_constant :SYMBOL_TO_PROC_SIGNATURE_RUBY3
|
|
18
16
|
|
|
19
17
|
class << self
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
# @param callable [#call] callable object
|
|
24
|
-
# @param pos_limit [Integer] Max count of positional parameters
|
|
25
|
-
# @param keyword_args [Array<Symbol>] List of accepted keyword argument names
|
|
26
|
-
#
|
|
27
|
-
# @return [String] Method signature which consist of number of positional parameters and
|
|
28
|
-
# keyword parameter names joined with undescrore.
|
|
29
|
-
#
|
|
18
|
+
# Generates method arguments signature: the count of positional
|
|
19
|
+
# parameters and the keyword parameter names, joined with underscores,
|
|
20
|
+
# like "1_ctx"
|
|
30
21
|
def call(callable, pos_limit:, keyword_args: [])
|
|
31
|
-
params = callable.is_a?(Proc) ? callable.parameters : callable.method(:call).parameters
|
|
22
|
+
params = (callable.is_a?(Proc) || callable.is_a?(Method)) ? callable.parameters : callable.method(:call).parameters
|
|
32
23
|
|
|
33
24
|
# Procs (but not lambdas) can accept all provided parameters
|
|
34
25
|
return full_signature(pos_limit, keyword_args) if params.empty? && callable.is_a?(Proc) && !callable.lambda?
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaUtils # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Parses fields listed in one String, with nested fields in parentheses
|
|
7
|
+
#
|
|
8
|
+
class ParseStringSelection # :nodoc:
|
|
9
|
+
SEPARATOR = /[,()]/
|
|
10
|
+
COMMA = ",".ord
|
|
11
|
+
LPAREN = "(".ord
|
|
12
|
+
private_constant :SEPARATOR, :COMMA, :LPAREN
|
|
13
|
+
|
|
14
|
+
class << self
|
|
15
|
+
# Parses a field selection String
|
|
16
|
+
#
|
|
17
|
+
# parse("user") => { user: {} }
|
|
18
|
+
# parse("user(id)") => { user: { id: {} } }
|
|
19
|
+
# parse("user(id,name)") => { user: { id: {}, name: {} } }
|
|
20
|
+
# parse("user,comments") => { user: {}, comments: {} }
|
|
21
|
+
# parse("user(comments(text))") => { user: { comments: { text: {} } } }
|
|
22
|
+
def parse(fields)
|
|
23
|
+
result = {}
|
|
24
|
+
storage = result
|
|
25
|
+
parent_storages = nil
|
|
26
|
+
start = 0
|
|
27
|
+
|
|
28
|
+
while (index = fields.byteindex(SEPARATOR, start))
|
|
29
|
+
name = extract_name(fields, start, index)
|
|
30
|
+
separator = fields.getbyte(index)
|
|
31
|
+
|
|
32
|
+
if separator == COMMA
|
|
33
|
+
storage[name] = FROZEN_EMPTY_HASH if name
|
|
34
|
+
elsif separator == LPAREN
|
|
35
|
+
if name
|
|
36
|
+
(parent_storages ||= []) << storage
|
|
37
|
+
storage = storage[name] = {}
|
|
38
|
+
end
|
|
39
|
+
else
|
|
40
|
+
storage[name] = FROZEN_EMPTY_HASH if name
|
|
41
|
+
storage = parent_storages&.pop || result
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
start = index + 1
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
name = extract_name(fields, start, fields.bytesize)
|
|
48
|
+
storage[name] = FROZEN_EMPTY_HASH if name
|
|
49
|
+
|
|
50
|
+
result
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
private
|
|
54
|
+
|
|
55
|
+
def extract_name(fields, start, finish)
|
|
56
|
+
name = fields.byteslice(start, finish - start)
|
|
57
|
+
name.strip!
|
|
58
|
+
name.empty? ? nil : name.to_sym
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
#
|
|
5
|
+
# Utilities
|
|
6
|
+
#
|
|
7
|
+
module SeregaUtils # :nodoc:
|
|
8
|
+
#
|
|
9
|
+
# Values stored in the current Ractor by key
|
|
10
|
+
#
|
|
11
|
+
class RactorLocal # :nodoc:
|
|
12
|
+
class << self
|
|
13
|
+
# Returns the key of the owner value with the name
|
|
14
|
+
def key(owner, name)
|
|
15
|
+
:"serega_#{name}_#{owner.object_id}"
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# :nocov:
|
|
19
|
+
if defined?(Ractor.[])
|
|
20
|
+
# Returns the value of the key in the current Ractor, stores the
|
|
21
|
+
# block result when there is none
|
|
22
|
+
def fetch(key)
|
|
23
|
+
Ractor[key] ||= yield
|
|
24
|
+
end
|
|
25
|
+
else
|
|
26
|
+
# Returns the value of the key in the current Ractor, stores the
|
|
27
|
+
# block result when there is none
|
|
28
|
+
def fetch(key)
|
|
29
|
+
Ractor.current[key] ||= yield
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
# :nocov:
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|