serega 0.50.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/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 +80 -102
- data/lib/serega/attribute_normalizer.rb +43 -17
- data/lib/serega/attribute_normalizers/preloads.rb +5 -4
- data/lib/serega/attribute_value_resolvers/base.rb +35 -0
- data/lib/serega/attribute_value_resolvers/batch.rb +14 -1
- data/lib/serega/attribute_value_resolvers/callable.rb +52 -0
- data/lib/serega/attribute_value_resolvers/const.rb +11 -1
- data/lib/serega/attribute_value_resolvers/delegate.rb +12 -2
- data/lib/serega/attribute_value_resolvers/hash_access.rb +19 -4
- data/lib/serega/attribute_value_resolvers/keyword.rb +10 -3
- data/lib/serega/attribute_values.rb +4 -18
- data/lib/serega/conditions.rb +26 -1
- data/lib/serega/config.rb +45 -6
- data/lib/serega/engine/pulls.rb +3 -16
- data/lib/serega/engine/run.rb +4 -9
- data/lib/serega/errors.rb +26 -2
- 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 +83 -60
- data/lib/serega/plan/depth_limit.rb +36 -0
- data/lib/serega/plan/per_ractor_cache.rb +11 -6
- 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 +41 -55
- data/lib/serega/plugins/metadata/meta_attribute.rb +17 -1
- data/lib/serega/plugins/metadata/metadata.rb +1 -10
- data/lib/serega/plugins/root/root.rb +11 -8
- data/lib/serega/presenter.rb +20 -0
- data/lib/serega/result_builder.rb +1 -1
- 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 +41 -85
- data/lib/serega/source_group.rb +36 -67
- data/lib/serega/utils/camel_case.rb +22 -0
- data/lib/serega/utils/collection_detector.rb +3 -2
- data/lib/serega/utils/{parse_string_modifiers.rb → parse_string_selection.rb} +2 -2
- data/lib/serega/utils/ruby_literal.rb +28 -0
- 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/check_attribute_params.rb +4 -4
- data/lib/serega/validations/check_definition_selection.rb +21 -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.rb +94 -30
- data/sig/serega.rbs +23 -7
- metadata +25 -5
- data/lib/serega/formatter.rb +0 -98
- data/lib/serega/plan_point.rb +0 -58
- data/lib/serega/validations/initiate/check_modifiers.rb +0 -87
data/lib/serega/result_code.rb
CHANGED
|
@@ -42,9 +42,14 @@ class Serega
|
|
|
42
42
|
# Serialization mode
|
|
43
43
|
attr_reader :mode
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
# `compact` leaves out the keys with `nil` values from serialized Hashes
|
|
46
|
+
def initialize(mode, points, compact: false)
|
|
46
47
|
@mode = mode
|
|
47
|
-
@
|
|
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)
|
|
48
53
|
end
|
|
49
54
|
|
|
50
55
|
# Returns the code of the `#call` method, and of the `#call_one`
|
|
@@ -58,14 +63,13 @@ class Serega
|
|
|
58
63
|
def to_s
|
|
59
64
|
variables = variables_code.join("\n ")
|
|
60
65
|
values = values_code
|
|
61
|
-
|
|
62
|
-
return code unless @points.none? { |point| point.child_plan || !point.batch_loaders.empty? }
|
|
63
|
-
|
|
64
|
-
"#{code}\n#{call_one_code(variables, values)}"
|
|
66
|
+
[call_code(variables, values), collect_code, call_one_code(variables, values)].compact.join("\n")
|
|
65
67
|
end
|
|
66
68
|
|
|
67
69
|
private
|
|
68
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.
|
|
69
73
|
def call_code(variables, values)
|
|
70
74
|
<<~RUBY
|
|
71
75
|
def call(sources, context, batches, relations)
|
|
@@ -83,7 +87,27 @@ class Serega
|
|
|
83
87
|
RUBY
|
|
84
88
|
end
|
|
85
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
|
|
86
108
|
def call_one_code(variables, values)
|
|
109
|
+
return unless @point_codes.all?(&:call_one?)
|
|
110
|
+
|
|
87
111
|
<<~RUBY
|
|
88
112
|
def call_one(source, context)
|
|
89
113
|
#{variables}
|
|
@@ -95,71 +119,15 @@ class Serega
|
|
|
95
119
|
# Code that sets the local variables of a method: only the variables
|
|
96
120
|
# that the code of the points reads
|
|
97
121
|
def variables_code
|
|
98
|
-
point_variables = @
|
|
122
|
+
point_variables = @point_codes.flat_map(&:variables)
|
|
99
123
|
return point_variables if mode == :hash
|
|
100
124
|
|
|
101
125
|
["result_class = @result_class", *point_variables]
|
|
102
126
|
end
|
|
103
127
|
|
|
104
|
-
def point_variables_code(point, index)
|
|
105
|
-
return ["relation_values_#{index} = relations[@points[#{index}]]"] if point.child_plan
|
|
106
|
-
|
|
107
|
-
variables = []
|
|
108
|
-
variables << "attribute_#{index} = @points[#{index}].attribute" unless point.attribute.value_method
|
|
109
|
-
variables << "batches_#{index} = batches[#{index}]" unless point.batch_loaders.empty?
|
|
110
|
-
variables << "conditions_#{index} = @points[#{index}].attribute.conditions" if point.conditional?
|
|
111
|
-
variables
|
|
112
|
-
end
|
|
113
|
-
|
|
114
128
|
# Code that reads all values of the source, one entry per point
|
|
115
129
|
def values_code
|
|
116
|
-
@
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
# Code that assigns the value of the point to `value_N`. A conditional
|
|
120
|
-
# attribute gets SKIP when it fails its conditions. A conditional
|
|
121
|
-
# relation value is SKIP already.
|
|
122
|
-
def assign_value_code(point, index)
|
|
123
|
-
return "value_#{index} = #{read_value_code(point, index)}" if point.child_plan
|
|
124
|
-
|
|
125
|
-
read_code = rescued_read_code(point, index)
|
|
126
|
-
return "value_#{index} =\n#{read_code.gsub(/^/, " ")}" unless point.conditional?
|
|
127
|
-
|
|
128
|
-
<<~RUBY.chomp
|
|
129
|
-
value_#{index} =
|
|
130
|
-
if conditions_#{index}.satisfy?(source, context)
|
|
131
|
-
value_#{index} =
|
|
132
|
-
#{read_code.gsub(/^/, " ")}
|
|
133
|
-
conditions_#{index}.satisfy_value?(value_#{index}, context) ? value_#{index} : Serega::SeregaEngine::SKIP
|
|
134
|
-
else
|
|
135
|
-
Serega::SeregaEngine::SKIP
|
|
136
|
-
end
|
|
137
|
-
RUBY
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
# Code that reads the value of the point. An error raised there gets
|
|
141
|
-
# the attribute name. Only the rescue reads the point.
|
|
142
|
-
def rescued_read_code(point, index)
|
|
143
|
-
<<~RUBY.chomp
|
|
144
|
-
begin
|
|
145
|
-
#{read_value_code(point, index)}
|
|
146
|
-
rescue => error
|
|
147
|
-
Serega::SeregaUtils::SerializedAttributeError.call(error, @points[#{index}])
|
|
148
|
-
end
|
|
149
|
-
RUBY
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
# Code that reads the value of the point. A plain attribute reads its
|
|
153
|
-
# `:const`, `:default` and formatter values from the constants of
|
|
154
|
-
# SeregaAttributeValues.
|
|
155
|
-
def read_value_code(point, index)
|
|
156
|
-
return "relation_values_#{index}[source_index]" if point.child_plan
|
|
157
|
-
|
|
158
|
-
attribute = point.attribute
|
|
159
|
-
return attribute.value_code("source", "SeregaAttributeValues::") if attribute.value_method
|
|
160
|
-
return "attribute_#{index}.value(source, context)" if point.batch_loaders.empty?
|
|
161
|
-
|
|
162
|
-
"attribute_#{index}.value(source, context, batches: batches_#{index})"
|
|
130
|
+
@point_codes.map(&:assign_value)
|
|
163
131
|
end
|
|
164
132
|
|
|
165
133
|
# Code that builds the serialized object of the source. `assign` is the
|
|
@@ -167,37 +135,25 @@ class Serega
|
|
|
167
135
|
def build_serialized_code(assign)
|
|
168
136
|
return build_serialized_hash_code(assign) if mode == :hash
|
|
169
137
|
|
|
170
|
-
arguments = @
|
|
138
|
+
arguments = @point_codes.map(&:argument)
|
|
171
139
|
["#{assign}result_class.new(#{arguments.join(", ")})"]
|
|
172
140
|
end
|
|
173
141
|
|
|
174
|
-
# Code of the Struct or Data argument with the value of the point, nil
|
|
175
|
-
# for a skipped value
|
|
176
|
-
def argument_code(index)
|
|
177
|
-
return "value_#{index}" unless @points[index].conditional?
|
|
178
|
-
|
|
179
|
-
"(Serega::SeregaEngine::SKIP.equal?(value_#{index}) ? nil : value_#{index})"
|
|
180
|
-
end
|
|
181
|
-
|
|
182
142
|
# Code that builds the serialized Hash of the source, after `assign`.
|
|
183
|
-
# A Hash literal holds the values up to the first
|
|
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
|
|
184
145
|
# the code adds the other values one by one, thus the Hash keys keep the
|
|
185
146
|
# order of the points.
|
|
186
147
|
def build_serialized_hash_code(assign)
|
|
187
|
-
|
|
188
|
-
|
|
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)
|
|
189
152
|
literal = "{#{pairs.join(", ")}}"
|
|
190
|
-
return ["#{assign}#{literal}"] if literal_size ==
|
|
153
|
+
return ["#{assign}#{literal}"] if literal_size == size
|
|
191
154
|
|
|
192
|
-
assigns = @
|
|
155
|
+
assigns = @point_codes.drop(literal_size).map { |point_code| point_code.hash_assign(compact) }
|
|
193
156
|
["serialized_hash = #{literal}", *assigns, "#{assign}serialized_hash"]
|
|
194
157
|
end
|
|
195
|
-
|
|
196
|
-
# Code that adds the value of the point to the serialized Hash. It adds
|
|
197
|
-
# no key for a skipped value.
|
|
198
|
-
def hash_assign_code(point, index)
|
|
199
|
-
code = "serialized_hash[#{point.name.inspect}] = value_#{index}"
|
|
200
|
-
point.conditional? ? "#{code} unless Serega::SeregaEngine::SKIP.equal?(value_#{index})" : code
|
|
201
|
-
end
|
|
202
158
|
end
|
|
203
159
|
end
|
data/lib/serega/source_group.rb
CHANGED
|
@@ -7,8 +7,9 @@ class Serega
|
|
|
7
7
|
# output: a Hash, Struct or Data object.
|
|
8
8
|
#
|
|
9
9
|
# The group serializes its sources in two steps:
|
|
10
|
-
# - `#discover` runs the preloads, reads the relation sources
|
|
11
|
-
# child group per
|
|
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.
|
|
12
13
|
# - `#build` builds the serialized objects. It runs after the child groups
|
|
13
14
|
# are built, thus the relation values are ready.
|
|
14
15
|
#
|
|
@@ -29,12 +30,12 @@ class Serega
|
|
|
29
30
|
# source, after #build
|
|
30
31
|
attr_reader :serialized
|
|
31
32
|
|
|
32
|
-
# Pulls from
|
|
33
|
-
#
|
|
33
|
+
# Pulls from the generated `#collect` of the parent group, one per
|
|
34
|
+
# source of the parent group
|
|
34
35
|
attr_reader :pulls
|
|
35
36
|
|
|
36
|
-
# Each source is wrapped in the
|
|
37
|
-
# and batch loaders alike see presenters.
|
|
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.
|
|
38
39
|
def initialize(run, plan, sources, pulls)
|
|
39
40
|
@run = run
|
|
40
41
|
@plan = plan
|
|
@@ -45,18 +46,20 @@ class Serega
|
|
|
45
46
|
@child_groups = nil
|
|
46
47
|
@serialized = nil
|
|
47
48
|
@loaded_batches = nil
|
|
48
|
-
@attribute_values = nil
|
|
49
49
|
end
|
|
50
50
|
|
|
51
51
|
# Runs the preloads, and makes one child group per relation point, in the
|
|
52
|
-
# order of the relation points.
|
|
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.
|
|
53
55
|
def discover
|
|
54
56
|
run_preloads
|
|
55
57
|
|
|
56
58
|
relation_points = plan.relation_points
|
|
57
59
|
return FROZEN_EMPTY_ARRAY if relation_points.empty?
|
|
58
60
|
|
|
59
|
-
|
|
61
|
+
collected = plan.result_builder(run.mode).collect(@sources, @context, relation_batches)
|
|
62
|
+
@child_groups = child_groups(relation_points, collected)
|
|
60
63
|
end
|
|
61
64
|
|
|
62
65
|
# Builds the serialized objects.
|
|
@@ -65,6 +68,13 @@ class Serega
|
|
|
65
68
|
@serialized = plan.result_builder(run.mode).call(@sources, @context, batches, relations)
|
|
66
69
|
end
|
|
67
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
|
+
|
|
68
78
|
private
|
|
69
79
|
|
|
70
80
|
# Relation values of each relation point, taken from its built child
|
|
@@ -86,21 +96,6 @@ class Serega
|
|
|
86
96
|
relations
|
|
87
97
|
end
|
|
88
98
|
|
|
89
|
-
# Reads the relation sources of the point, and makes their child group
|
|
90
|
-
def child_group(point)
|
|
91
|
-
batches = batches_for(point)
|
|
92
|
-
child_sources = []
|
|
93
|
-
pulls =
|
|
94
|
-
if point.conditional?
|
|
95
|
-
relation_sources = read_conditional_relation_sources(point, batches)
|
|
96
|
-
SeregaEngine::Pulls.collect_conditional(child_sources, relation_sources, point.many, point.collection_classes)
|
|
97
|
-
else
|
|
98
|
-
relation_sources = read_relation_sources(point, batches)
|
|
99
|
-
SeregaEngine::Pulls.collect(child_sources, relation_sources, point.many, point.collection_classes)
|
|
100
|
-
end
|
|
101
|
-
run.new_source_group(point.child_plan, child_sources, pulls)
|
|
102
|
-
end
|
|
103
|
-
|
|
104
99
|
# Runs the preloads of all points with the serializer's preload handler.
|
|
105
100
|
# Preload handlers get the sources without presenters.
|
|
106
101
|
def run_preloads
|
|
@@ -122,6 +117,23 @@ class Serega
|
|
|
122
117
|
end
|
|
123
118
|
end
|
|
124
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
|
+
|
|
125
137
|
# Loads the batch loaders of the point, each once for all sources of
|
|
126
138
|
# the group. An attribute with an inline batch loader uses it, other
|
|
127
139
|
# attributes use the named batch loaders of the serializer. Returns nil
|
|
@@ -140,48 +152,5 @@ class Serega
|
|
|
140
152
|
rescue => error
|
|
141
153
|
SeregaUtils::SerializedAttributeError.call(error, point)
|
|
142
154
|
end
|
|
143
|
-
|
|
144
|
-
# SeregaAttributeValues of the serializer: the methods that read
|
|
145
|
-
# attribute values with plain Ruby code
|
|
146
|
-
def attribute_values
|
|
147
|
-
@attribute_values ||= plan.serializer_class::SeregaAttributeValues.new
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Reads the relation source of every source: what the relation
|
|
151
|
-
# attribute returns, for example `user.posts`.
|
|
152
|
-
def read_relation_sources(point, batches)
|
|
153
|
-
attribute = point.attribute
|
|
154
|
-
value_method = attribute.value_method
|
|
155
|
-
context = @context
|
|
156
|
-
|
|
157
|
-
if value_method
|
|
158
|
-
values = attribute_values
|
|
159
|
-
@sources.map { |source| values.__send__(value_method, source) }
|
|
160
|
-
else
|
|
161
|
-
@sources.map { |source| attribute.value(source, context, batches: batches) }
|
|
162
|
-
end
|
|
163
|
-
rescue => error
|
|
164
|
-
SeregaUtils::SerializedAttributeError.call(error, point)
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Reads the relation source of every source of a conditional relation.
|
|
168
|
-
# A source failing the :if or :unless condition gets SKIP.
|
|
169
|
-
def read_conditional_relation_sources(point, batches)
|
|
170
|
-
attribute = point.attribute
|
|
171
|
-
value_method = attribute.value_method
|
|
172
|
-
values = attribute_values if value_method
|
|
173
|
-
conditions = attribute.conditions
|
|
174
|
-
context = @context
|
|
175
|
-
|
|
176
|
-
@sources.map do |source|
|
|
177
|
-
next SeregaEngine::SKIP unless conditions.satisfy?(source, context)
|
|
178
|
-
|
|
179
|
-
begin
|
|
180
|
-
value_method ? values.__send__(value_method, source) : attribute.value(source, context, batches: batches)
|
|
181
|
-
rescue => error
|
|
182
|
-
SeregaUtils::SerializedAttributeError.call(error, point)
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
155
|
end
|
|
187
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
|
|
@@ -8,6 +8,9 @@ class Serega
|
|
|
8
8
|
# Hashes and Structs are Enumerable, but enumerate their own member
|
|
9
9
|
# values, so they are treated as single objects.
|
|
10
10
|
#
|
|
11
|
+
# Callers check `instance_of?(Array)` before they call the detector, thus
|
|
12
|
+
# an Array needs no lookup.
|
|
13
|
+
#
|
|
11
14
|
class CollectionDetector # :nodoc:
|
|
12
15
|
# Number of classes a Hash of known classes keeps
|
|
13
16
|
MAX_KNOWN_CLASSES = 8
|
|
@@ -18,8 +21,6 @@ class Serega
|
|
|
18
21
|
# MAX_KNOWN_CLASSES classes: `is_a?(Enumerable)` checks all
|
|
19
22
|
# ancestors of a class that does not include Enumerable.
|
|
20
23
|
def call(object, known_classes)
|
|
21
|
-
return true if object.instance_of?(Array)
|
|
22
|
-
|
|
23
24
|
object_class = object.class
|
|
24
25
|
collection = known_classes[object_class]
|
|
25
26
|
return collection unless collection.nil?
|
|
@@ -5,14 +5,14 @@ class Serega
|
|
|
5
5
|
#
|
|
6
6
|
# Parses fields listed in one String, with nested fields in parentheses
|
|
7
7
|
#
|
|
8
|
-
class
|
|
8
|
+
class ParseStringSelection # :nodoc:
|
|
9
9
|
SEPARATOR = /[,()]/
|
|
10
10
|
COMMA = ",".ord
|
|
11
11
|
LPAREN = "(".ord
|
|
12
12
|
private_constant :SEPARATOR, :COMMA, :LPAREN
|
|
13
13
|
|
|
14
14
|
class << self
|
|
15
|
-
# Parses
|
|
15
|
+
# Parses a field selection String
|
|
16
16
|
#
|
|
17
17
|
# parse("user") => { user: {} }
|
|
18
18
|
# parse("user(id)") => { user: { id: {} } }
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaUtils # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Writes values as Ruby literals in generated code
|
|
7
|
+
#
|
|
8
|
+
class RubyLiteral # :nodoc:
|
|
9
|
+
class << self
|
|
10
|
+
# Whether the value can be written as a literal: an Integer, a
|
|
11
|
+
# Symbol, a String, true, false, nil or a finite Float
|
|
12
|
+
def literal?(value)
|
|
13
|
+
case value
|
|
14
|
+
when Integer, Symbol, String, true, false, nil then true
|
|
15
|
+
when Float then value.finite?
|
|
16
|
+
else false
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Ruby code of the literal value. A String literal is frozen, thus
|
|
21
|
+
# the code makes no new String each time it runs.
|
|
22
|
+
def code(value)
|
|
23
|
+
value.is_a?(String) ? "#{value.inspect}.freeze" : value.inspect
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
6
|
+
#
|
|
7
|
+
# Attribute `:preset` option validator
|
|
8
|
+
#
|
|
9
|
+
class CheckOptPreset # :nodoc:
|
|
10
|
+
class << self
|
|
11
|
+
# Checks that the :preset option names presets of the relation
|
|
12
|
+
# serializer. A serializer given as a String or a Proc, or defined
|
|
13
|
+
# with a block, is checked when a plan is built.
|
|
14
|
+
def call(opts, block = nil)
|
|
15
|
+
return unless opts.key?(:preset)
|
|
16
|
+
|
|
17
|
+
serializer = opts[:serializer]
|
|
18
|
+
raise SeregaError, "Option :preset can be provided only together with :serializer option or a block" unless serializer || block
|
|
19
|
+
|
|
20
|
+
check_defined(serializer, SeregaPlan::Presets.names(opts[:preset]))
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def check_defined(serializer, names)
|
|
26
|
+
return unless serializer.is_a?(Class)
|
|
27
|
+
|
|
28
|
+
names.each { |name| SeregaPlan::Presets.fetch_definition(serializer, name) }
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
module Attribute # :nodoc:
|
|
6
|
+
#
|
|
7
|
+
# Attribute `:only`, `:with` and `:except` options validator
|
|
8
|
+
#
|
|
9
|
+
class CheckOptSelection # :nodoc:
|
|
10
|
+
# Field selection options of a relation
|
|
11
|
+
KEYS = %i[only with except].freeze
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Checks that the options are given with a relation, that they name
|
|
15
|
+
# attributes, and that the attributes exist in a serializer class.
|
|
16
|
+
# Another serializer is checked when a plan is built.
|
|
17
|
+
def call(name, opts, block = nil)
|
|
18
|
+
key = KEYS.find { |option| opts.key?(option) }
|
|
19
|
+
return unless key
|
|
20
|
+
|
|
21
|
+
serializer = opts[:serializer]
|
|
22
|
+
raise SeregaError, "Option #{key.inspect} can be provided only together with :serializer option or a block" unless serializer || block
|
|
23
|
+
|
|
24
|
+
selection = KEYS.to_h { |option| [option, parse(option, opts[option])] }
|
|
25
|
+
return unless serializer.is_a?(Class)
|
|
26
|
+
|
|
27
|
+
CheckRelationSelection.check(name, serializer, selection, CheckDefinitionSelection)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def parse(key, value)
|
|
33
|
+
SeregaPlan::SelectionPlanner.parse_option(value)
|
|
34
|
+
rescue SeregaError
|
|
35
|
+
raise SeregaError, "Invalid option #{key.inspect} => #{value.inspect}. Must be a Symbol, a String, an Array or a Hash of attribute names"
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -12,7 +12,7 @@ class Serega
|
|
|
12
12
|
# Options of `Serega.attribute`
|
|
13
13
|
ATTRIBUTE_KEYS = %i[
|
|
14
14
|
method value serializer many hide const delegate default format preload batch
|
|
15
|
-
base_serializer hash_access if unless if_value unless_value
|
|
15
|
+
base_serializer hash_access preset only with except if unless if_value unless_value
|
|
16
16
|
].freeze
|
|
17
17
|
|
|
18
18
|
# Options that can't be given together, `block` for the attribute
|
|
@@ -27,9 +27,7 @@ class Serega
|
|
|
27
27
|
batch: %i[method const delegate hash_access],
|
|
28
28
|
hash_access: %i[value const batch],
|
|
29
29
|
preload: %i[const],
|
|
30
|
-
serializer: %i[block
|
|
31
|
-
if_value: %i[serializer block],
|
|
32
|
-
unless_value: %i[serializer block]
|
|
30
|
+
serializer: %i[block]
|
|
33
31
|
}.freeze
|
|
34
32
|
|
|
35
33
|
# Validated attribute name
|
|
@@ -80,6 +78,8 @@ class Serega
|
|
|
80
78
|
Attribute::CheckOptMethod.call(opts)
|
|
81
79
|
Attribute::CheckOptMany.call(opts, block, require_many)
|
|
82
80
|
Attribute::CheckOptPreload.call(opts)
|
|
81
|
+
Attribute::CheckOptPreset.call(opts, block)
|
|
82
|
+
Attribute::CheckOptSelection.call(name, opts, block)
|
|
83
83
|
Attribute::CheckOptSerializer.call(opts)
|
|
84
84
|
Attribute::CheckOptValue.call(opts)
|
|
85
85
|
Attribute::CheckOptBatch.call(@serializer_class, name, opts)
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Field selection validation of a preset or a relation when it is
|
|
7
|
+
# defined. It skips the nested fields of relations with a serializer name
|
|
8
|
+
# or a Proc, as that serializer may be defined later.
|
|
9
|
+
#
|
|
10
|
+
class CheckDefinitionSelection < CheckSelection # :nodoc:
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def validate_nested(name, attribute, nested_fields)
|
|
14
|
+
serializer = attribute.initials[:opts][:serializer]
|
|
15
|
+
return if serializer.is_a?(String) || serializer.is_a?(Proc)
|
|
16
|
+
|
|
17
|
+
super
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Validations of the parameters of `Serega.preset`. Nested fields of a
|
|
7
|
+
# relation with a serializer name or a Proc are not checked: that
|
|
8
|
+
# serializer may be defined later.
|
|
9
|
+
#
|
|
10
|
+
class CheckPresetParams # :nodoc:
|
|
11
|
+
# Initializes the preset params validator
|
|
12
|
+
def initialize(serializer_class, name, preset, only, with, except)
|
|
13
|
+
@serializer_class = serializer_class
|
|
14
|
+
@name = name
|
|
15
|
+
@preset = preset
|
|
16
|
+
@only = only
|
|
17
|
+
@with = with
|
|
18
|
+
@except = except
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Raises an error for an invalid name, an invalid `:preset` option,
|
|
22
|
+
# presets that include each other, invalid field selection options and
|
|
23
|
+
# a preset without a field selection, PresetNotExist for an undefined
|
|
24
|
+
# included preset, and AttributeNotExist for unknown attributes
|
|
25
|
+
def validate
|
|
26
|
+
check_name
|
|
27
|
+
names = included_names
|
|
28
|
+
check_included_presets(names)
|
|
29
|
+
check_selection(names)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def check_name
|
|
35
|
+
return if @name.is_a?(Symbol) || @name.is_a?(String)
|
|
36
|
+
|
|
37
|
+
raise SeregaError, "Preset name must be a Symbol or a String, #{@name.inspect} provided"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def included_names
|
|
41
|
+
return FROZEN_EMPTY_ARRAY if @preset.nil?
|
|
42
|
+
|
|
43
|
+
names = @preset.is_a?(Array) ? @preset : [@preset]
|
|
44
|
+
check_preset_option(names)
|
|
45
|
+
names.map(&:to_sym)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def check_preset_option(names)
|
|
49
|
+
return if names.all? { |name| name.is_a?(Symbol) || name.is_a?(String) }
|
|
50
|
+
|
|
51
|
+
raise SeregaError, "Invalid option :preset => #{@preset.inspect}. Must be a Symbol, a String, or an Array of them"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def check_included_presets(names)
|
|
55
|
+
check_defined(names)
|
|
56
|
+
check_cycle(names, [@name.to_sym])
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def check_defined(names)
|
|
60
|
+
names.each { |included_name| SeregaPlan::Presets.fetch_definition(@serializer_class, included_name) }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Raises an error when the included presets, or the presets they
|
|
64
|
+
# include, include the new preset. `path` lists the presets from the
|
|
65
|
+
# new one, its first item, to the included ones. `visited` keeps the
|
|
66
|
+
# presets already walked, so each preset is walked once.
|
|
67
|
+
def check_cycle(names, path, visited = Set.new)
|
|
68
|
+
names.each do |included_name|
|
|
69
|
+
included_path = [*path, included_name]
|
|
70
|
+
raise SeregaError, "Presets of #{@serializer_class} include each other: #{included_path.join(" -> ")}" if included_name == path.first
|
|
71
|
+
next unless visited.add?(included_name)
|
|
72
|
+
|
|
73
|
+
check_cycle(@serializer_class.presets[included_name][:presets], included_path, visited)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def parse_option(key, value)
|
|
78
|
+
SeregaPlan::SelectionPlanner.parse_option(value)
|
|
79
|
+
rescue SeregaError
|
|
80
|
+
raise SeregaError, "Invalid :#{key} => #{value.inspect} in preset :#{@name}. Must be a Symbol, a String, an Array or a Hash of attribute names"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def check_selection(names)
|
|
84
|
+
selection = {only: parse_option(:only, @only), with: parse_option(:with, @with), except: parse_option(:except, @except)}
|
|
85
|
+
check_not_empty(names, selection)
|
|
86
|
+
check_attributes(selection)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def check_attributes(selection)
|
|
90
|
+
CheckDefinitionSelection.new.call(@serializer_class, selection[:only], selection[:with], selection[:except])
|
|
91
|
+
rescue AttributeNotExist => error
|
|
92
|
+
attributes = error.attributes
|
|
93
|
+
raise AttributeNotExist.new("Preset :#{@name} names not existing attributes: #{attributes.join(", ")}", error.serializer, attributes)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def check_not_empty(names, selection)
|
|
97
|
+
return unless names.empty? && SeregaPlan::SelectionPlanner.empty?(selection)
|
|
98
|
+
|
|
99
|
+
raise SeregaError, "Preset :#{@name} must select fields with :only, :with, :except or :preset"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|