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
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Checks the attributes that the `only`, `with` and `except` options of
|
|
7
|
+
# a relation name
|
|
8
|
+
#
|
|
9
|
+
class CheckRelationSelection # :nodoc:
|
|
10
|
+
class << self
|
|
11
|
+
# Checks the options of a relation when its plan is built, for a
|
|
12
|
+
# serializer given as a String or a Proc, or defined with a block. A
|
|
13
|
+
# serializer class is checked when the attribute is defined.
|
|
14
|
+
def call(attribute, serializer)
|
|
15
|
+
return if attribute.initials[:opts][:serializer].is_a?(Class)
|
|
16
|
+
|
|
17
|
+
check(attribute.name, serializer, attribute.selection, CheckSelection)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Raises AttributeNotExist that names the relation for attributes
|
|
21
|
+
# the selection names and the serializer does not have
|
|
22
|
+
def check(name, serializer, selection, validator)
|
|
23
|
+
validator.new.call(serializer, selection[:only], selection[:with], selection[:except])
|
|
24
|
+
rescue AttributeNotExist => error
|
|
25
|
+
attributes = error.attributes
|
|
26
|
+
raise AttributeNotExist.new("Relation :#{name} names not existing attributes: #{attributes.join(", ")}", error.serializer, attributes)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Serega
|
|
4
|
+
module SeregaValidations # :nodoc:
|
|
5
|
+
#
|
|
6
|
+
# Raises AttributeNotExist for the attributes that the field selection
|
|
7
|
+
# options name and the serializer does not have
|
|
8
|
+
#
|
|
9
|
+
class CheckSelection # :nodoc:
|
|
10
|
+
#
|
|
11
|
+
# Instantiates the field selection validator
|
|
12
|
+
#
|
|
13
|
+
def initialize
|
|
14
|
+
@parents_names = nil
|
|
15
|
+
@error_attributes = nil
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Validates provided fields names are existing attributes
|
|
19
|
+
def call(serializer_class, only, with, except)
|
|
20
|
+
validate(serializer_class, only)
|
|
21
|
+
validate(serializer_class, with)
|
|
22
|
+
validate(serializer_class, except)
|
|
23
|
+
|
|
24
|
+
raise_errors(serializer_class) if any_error?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def validate(serializer_class, fields)
|
|
30
|
+
fields.each do |name, nested_fields|
|
|
31
|
+
attribute = serializer_class && serializer_class.attributes[name]
|
|
32
|
+
|
|
33
|
+
# Save error when no attribute with checked name exists
|
|
34
|
+
unless attribute
|
|
35
|
+
save_error(name)
|
|
36
|
+
next
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Return when attribute has no nested fields
|
|
40
|
+
next if nested_fields.empty?
|
|
41
|
+
|
|
42
|
+
validate_nested(name, attribute, nested_fields)
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Validates the nested fields of an attribute with the attributes of
|
|
47
|
+
# its serializer
|
|
48
|
+
def validate_nested(name, attribute, nested_fields)
|
|
49
|
+
with_parent_name(name) do
|
|
50
|
+
validate(attribute.serializer, nested_fields)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def parents_names
|
|
55
|
+
@parents_names ||= []
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def with_parent_name(name)
|
|
59
|
+
parents_names << name
|
|
60
|
+
yield
|
|
61
|
+
parents_names.pop
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def error_attributes
|
|
65
|
+
@error_attributes ||= []
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def save_error(name)
|
|
69
|
+
error_attributes << build_full_attribute_name(*parents_names, name)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def build_full_attribute_name(*names)
|
|
73
|
+
head, *nested = *names
|
|
74
|
+
result = +head.to_s # names are symbols, we need not frozen string
|
|
75
|
+
nested.each { |nested_name| result << "(" << nested_name.to_s }
|
|
76
|
+
nested.each { result << ")" }
|
|
77
|
+
result
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def raise_errors(serializer_class)
|
|
81
|
+
raise Serega::AttributeNotExist.new("Not existing attributes: #{error_attributes.join(", ")}", serializer_class, error_attributes)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def any_error?
|
|
85
|
+
!@error_attributes.nil?
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
data/lib/serega.rb
CHANGED
|
@@ -18,10 +18,10 @@ require_relative "serega/version"
|
|
|
18
18
|
#
|
|
19
19
|
# See the [guides](https://github.com/andreyruby/serega/blob/master/docs/README.md).
|
|
20
20
|
class Serega
|
|
21
|
-
# Frozen
|
|
21
|
+
# Frozen empty Hash, a shared default in place of a new `{}` per call
|
|
22
22
|
FROZEN_EMPTY_HASH = {}.freeze # :nodoc:
|
|
23
23
|
|
|
24
|
-
# Frozen
|
|
24
|
+
# Frozen empty Array, a shared default in place of a new `[]` per call
|
|
25
25
|
FROZEN_EMPTY_ARRAY = [].freeze # :nodoc:
|
|
26
26
|
|
|
27
27
|
# Locks serializers one at a time
|
|
@@ -38,24 +38,32 @@ class Serega
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
require_relative "serega/errors"
|
|
41
|
+
require_relative "serega/utils/camel_case"
|
|
41
42
|
require_relative "serega/utils/collection_detector"
|
|
42
43
|
require_relative "serega/utils/deep_merge"
|
|
43
44
|
require_relative "serega/utils/enum_deep_dup"
|
|
44
45
|
require_relative "serega/utils/enum_deep_freeze"
|
|
45
46
|
require_relative "serega/utils/method_signature"
|
|
46
|
-
require_relative "serega/utils/
|
|
47
|
+
require_relative "serega/utils/parse_string_selection"
|
|
47
48
|
require_relative "serega/utils/ractor_local"
|
|
49
|
+
require_relative "serega/utils/ruby_literal"
|
|
48
50
|
require_relative "serega/utils/serialized_attribute_error"
|
|
49
51
|
require_relative "serega/utils/to_hash"
|
|
52
|
+
require_relative "serega/attribute_value_resolvers/base"
|
|
50
53
|
require_relative "serega/attribute_value_resolvers/batch"
|
|
54
|
+
require_relative "serega/attribute_value_resolvers/callable"
|
|
51
55
|
require_relative "serega/attribute_value_resolvers/const"
|
|
52
56
|
require_relative "serega/attribute_value_resolvers/delegate"
|
|
53
57
|
require_relative "serega/attribute_value_resolvers/hash_access"
|
|
54
58
|
require_relative "serega/attribute_value_resolvers/keyword"
|
|
55
59
|
require_relative "serega/attribute_values"
|
|
56
60
|
require_relative "serega/conditions"
|
|
57
|
-
require_relative "serega/
|
|
61
|
+
require_relative "serega/formatters"
|
|
62
|
+
require_relative "serega/formatters/callable"
|
|
63
|
+
require_relative "serega/formatters/method"
|
|
58
64
|
require_relative "serega/attribute"
|
|
65
|
+
require_relative "serega/attribute/serializer_reference"
|
|
66
|
+
require_relative "serega/attribute/value_code"
|
|
59
67
|
require_relative "serega/attribute_normalizers/batch"
|
|
60
68
|
require_relative "serega/attribute_normalizers/block"
|
|
61
69
|
require_relative "serega/attribute_normalizers/delegate"
|
|
@@ -81,11 +89,16 @@ require_relative "serega/validations/attribute/check_opt_hash_access"
|
|
|
81
89
|
require_relative "serega/validations/attribute/check_opt_many"
|
|
82
90
|
require_relative "serega/validations/attribute/check_opt_method"
|
|
83
91
|
require_relative "serega/validations/attribute/check_opt_preload"
|
|
92
|
+
require_relative "serega/validations/attribute/check_opt_preset"
|
|
93
|
+
require_relative "serega/validations/attribute/check_opt_selection"
|
|
84
94
|
require_relative "serega/validations/attribute/check_opt_serializer"
|
|
85
95
|
require_relative "serega/validations/attribute/check_opt_value"
|
|
86
|
-
require_relative "serega/validations/
|
|
96
|
+
require_relative "serega/validations/check_selection"
|
|
97
|
+
require_relative "serega/validations/check_definition_selection"
|
|
98
|
+
require_relative "serega/validations/check_relation_selection"
|
|
87
99
|
require_relative "serega/validations/check_attribute_params"
|
|
88
100
|
require_relative "serega/validations/check_batch_loader_params"
|
|
101
|
+
require_relative "serega/validations/check_preset_params"
|
|
89
102
|
require_relative "serega/validations/check_formatter"
|
|
90
103
|
require_relative "serega/validations/check_serialize_keywords"
|
|
91
104
|
|
|
@@ -93,10 +106,17 @@ require_relative "serega/batch_loader"
|
|
|
93
106
|
require_relative "serega/config"
|
|
94
107
|
require_relative "serega/presenter"
|
|
95
108
|
require_relative "serega/source_group"
|
|
96
|
-
require_relative "serega/plan_point"
|
|
97
109
|
require_relative "serega/plan"
|
|
110
|
+
require_relative "serega/plan/depth_limit"
|
|
111
|
+
require_relative "serega/plan/point"
|
|
112
|
+
require_relative "serega/plan/selection"
|
|
98
113
|
require_relative "serega/result_code"
|
|
114
|
+
require_relative "serega/result_code/point_code"
|
|
115
|
+
require_relative "serega/result_code/attribute_point_code"
|
|
116
|
+
require_relative "serega/result_code/relation_point_code"
|
|
99
117
|
require_relative "serega/result_builder"
|
|
118
|
+
require_relative "serega/plan/selection_planner"
|
|
119
|
+
require_relative "serega/plan/presets"
|
|
100
120
|
require_relative "serega/plan/cache"
|
|
101
121
|
require_relative "serega/plugins"
|
|
102
122
|
|
|
@@ -151,6 +171,18 @@ class Serega
|
|
|
151
171
|
@attributes
|
|
152
172
|
end
|
|
153
173
|
|
|
174
|
+
#
|
|
175
|
+
# Presets by name: the names of the presets each one includes and its
|
|
176
|
+
# own field selection
|
|
177
|
+
#
|
|
178
|
+
# ```ruby
|
|
179
|
+
# UserSerializer.presets # => {card: {presets: [], only: {posts: {title: {}}}, with: {}, except: {}}}
|
|
180
|
+
# ```
|
|
181
|
+
#
|
|
182
|
+
def presets
|
|
183
|
+
@presets
|
|
184
|
+
end
|
|
185
|
+
|
|
154
186
|
# Lists defined batch loaders
|
|
155
187
|
def batch_loaders # :nodoc:
|
|
156
188
|
@batch_loaders
|
|
@@ -213,6 +245,8 @@ class Serega
|
|
|
213
245
|
# attribute :name, method: :full_name
|
|
214
246
|
# attribute :email, hide: true
|
|
215
247
|
# attribute :posts, serializer: PostSerializer
|
|
248
|
+
# attribute :posts, serializer: PostSerializer, preset: :summary # fields of a preset
|
|
249
|
+
# attribute :posts, serializer: PostSerializer, only: [:id, :title]
|
|
216
250
|
# ```
|
|
217
251
|
#
|
|
218
252
|
# A block defines the attributes of a nested serializer. It inherits from
|
|
@@ -235,6 +269,31 @@ class Serega
|
|
|
235
269
|
attributes[attribute.name] = attribute
|
|
236
270
|
end
|
|
237
271
|
|
|
272
|
+
#
|
|
273
|
+
# Saves the `only`, `with` and `except` options under a name. The
|
|
274
|
+
# `preset:` serialization option selects the fields of the presets,
|
|
275
|
+
# merged with the options given in the call. A preset replaces the preset with the same
|
|
276
|
+
# name.
|
|
277
|
+
#
|
|
278
|
+
# - `preset:` names presets defined earlier. They are merged when the
|
|
279
|
+
# preset is used, thus a redefined preset changes the presets that
|
|
280
|
+
# include it.
|
|
281
|
+
#
|
|
282
|
+
# ```ruby
|
|
283
|
+
# preset :card, only: [:first_name, {avatar: :url}]
|
|
284
|
+
# preset :profile, preset: :card, with: {posts: :comments}
|
|
285
|
+
#
|
|
286
|
+
# UserSerializer.to_h(user, preset: :profile, except: :avatar)
|
|
287
|
+
# ```
|
|
288
|
+
#
|
|
289
|
+
# See [Presets](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md#presets).
|
|
290
|
+
#
|
|
291
|
+
def preset(name, preset: nil, only: nil, with: nil, except: nil)
|
|
292
|
+
check_unlocked
|
|
293
|
+
definition = SeregaPlan::Presets.define(self, name, preset, only, with, except)
|
|
294
|
+
presets[name.to_sym] = definition
|
|
295
|
+
end
|
|
296
|
+
|
|
238
297
|
#
|
|
239
298
|
# Defines a named batch loader: a callable or a block that receives the
|
|
240
299
|
# objects, and the context as a second parameter or as the `:ctx`
|
|
@@ -282,7 +341,7 @@ class Serega
|
|
|
282
341
|
|
|
283
342
|
value ||= block
|
|
284
343
|
SeregaValidations::CheckFormatter.call(name, value)
|
|
285
|
-
formatters[name] =
|
|
344
|
+
formatters[name] = SeregaFormatters.build({use: value, allow_nil: allow_nil})
|
|
286
345
|
end
|
|
287
346
|
|
|
288
347
|
#
|
|
@@ -375,8 +434,8 @@ class Serega
|
|
|
375
434
|
#
|
|
376
435
|
# - `only`, `except`, `with`: fields to serialize, to skip and to add,
|
|
377
436
|
# see [Selecting fields](https://github.com/andreyruby/serega/blob/master/docs/selecting-fields.md);
|
|
378
|
-
# - `
|
|
379
|
-
# `config.
|
|
437
|
+
# - `check_selection`: `false` skips unknown fields, by default
|
|
438
|
+
# `config.check_selection`;
|
|
380
439
|
# - `context`: data for attribute values and conditions;
|
|
381
440
|
# - `many`: `true` serializes a list, `false` one object. By default an
|
|
382
441
|
# `Enumerable` that is not a `Hash` or `Struct` is a list.
|
|
@@ -389,8 +448,10 @@ class Serega
|
|
|
389
448
|
#
|
|
390
449
|
# See [Serializing](https://github.com/andreyruby/serega/blob/master/docs/serializing.md).
|
|
391
450
|
#
|
|
392
|
-
def call(object, only: nil, with: nil, except: nil,
|
|
393
|
-
|
|
451
|
+
def call(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
|
|
452
|
+
# The serialization methods list each keyword instead of taking
|
|
453
|
+
# `**opts`: a `**opts` Hash allocates on every call, even when empty.
|
|
454
|
+
new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
|
|
394
455
|
.to_h(object, context: context, many: many)
|
|
395
456
|
end
|
|
396
457
|
|
|
@@ -404,8 +465,8 @@ class Serega
|
|
|
404
465
|
# UserSerializer.to_data(user) # => #<data first_name="Bruce">
|
|
405
466
|
# ```
|
|
406
467
|
#
|
|
407
|
-
def to_data(object, only: nil, with: nil, except: nil,
|
|
408
|
-
new(only: only, with: with, except: except,
|
|
468
|
+
def to_data(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
|
|
469
|
+
new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
|
|
409
470
|
.to_data(object, context: context, many: many)
|
|
410
471
|
end
|
|
411
472
|
|
|
@@ -419,8 +480,8 @@ class Serega
|
|
|
419
480
|
# UserSerializer.to_struct(user) # => #<struct first_name="Bruce">
|
|
420
481
|
# ```
|
|
421
482
|
#
|
|
422
|
-
def to_struct(object, only: nil, with: nil, except: nil,
|
|
423
|
-
new(only: only, with: with, except: except,
|
|
483
|
+
def to_struct(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
|
|
484
|
+
new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
|
|
424
485
|
.to_struct(object, context: context, many: many)
|
|
425
486
|
end
|
|
426
487
|
|
|
@@ -435,8 +496,8 @@ class Serega
|
|
|
435
496
|
# UserSerializer.to_json(user) # => "{\"first_name\":\"Bruce\"}"
|
|
436
497
|
# ```
|
|
437
498
|
#
|
|
438
|
-
def to_json(object, only: nil, with: nil, except: nil,
|
|
439
|
-
new(only: only, with: with, except: except,
|
|
499
|
+
def to_json(object, only: nil, with: nil, except: nil, preset: nil, check_selection: nil, context: nil, many: nil)
|
|
500
|
+
new(only: only, with: with, except: except, preset: preset, check_selection: check_selection)
|
|
440
501
|
.to_json(object, context: context, many: many)
|
|
441
502
|
end
|
|
442
503
|
|
|
@@ -451,7 +512,10 @@ class Serega
|
|
|
451
512
|
next if @locked
|
|
452
513
|
|
|
453
514
|
SeregaUtils::EnumDeepFreeze.call(config.opts)
|
|
454
|
-
|
|
515
|
+
if @presenter_class
|
|
516
|
+
define_presenter_delegators
|
|
517
|
+
attributes.each_value { |attribute| attribute.check_presenter(@presenter_class) }
|
|
518
|
+
end
|
|
455
519
|
@locked = true
|
|
456
520
|
end
|
|
457
521
|
end
|
|
@@ -489,7 +553,7 @@ class Serega
|
|
|
489
553
|
@presenter_key = SeregaUtils::RactorLocal.key(self, :presenter)
|
|
490
554
|
end
|
|
491
555
|
@unshareable_reason = SeregaShareable.call(self)
|
|
492
|
-
@plan_cache = SeregaPlan::PerRactorCache.new(self) unless @unshareable_reason
|
|
556
|
+
@plan_cache = SeregaPlan::PerRactorCache.new(self, SeregaPlan::SelectionPlanner) unless @unshareable_reason
|
|
493
557
|
end
|
|
494
558
|
# :nocov:
|
|
495
559
|
self
|
|
@@ -533,6 +597,9 @@ class Serega
|
|
|
533
597
|
subclass.attributes[subclass_attribute.name] = subclass_attribute
|
|
534
598
|
end
|
|
535
599
|
|
|
600
|
+
# Assign same presets
|
|
601
|
+
subclass.presets.merge!(presets)
|
|
602
|
+
|
|
536
603
|
# Assign same preload handler
|
|
537
604
|
subclass.preload_with(preload_with) if preload_with
|
|
538
605
|
|
|
@@ -553,6 +620,7 @@ class Serega
|
|
|
553
620
|
def initialize_definition(config, formatters)
|
|
554
621
|
@config = config
|
|
555
622
|
@attributes = {}
|
|
623
|
+
@presets = {}
|
|
556
624
|
@batch_loaders = {}
|
|
557
625
|
@formatters = formatters
|
|
558
626
|
@presenter_class = nil
|
|
@@ -566,7 +634,7 @@ class Serega
|
|
|
566
634
|
@locked = false
|
|
567
635
|
@shareable = false
|
|
568
636
|
@unshareable_reason = nil
|
|
569
|
-
@plan_cache = SeregaPlan::Cache.new(self)
|
|
637
|
+
@plan_cache = SeregaPlan::Cache.new(self, SeregaPlan::SelectionPlanner)
|
|
570
638
|
end
|
|
571
639
|
|
|
572
640
|
# The handler given to `method_name` as a value or a block
|
|
@@ -604,10 +672,8 @@ class Serega
|
|
|
604
672
|
# serializer.to_h(users) # => [{first_name: "Bruce"}, {first_name: "Alfred"}]
|
|
605
673
|
# ```
|
|
606
674
|
#
|
|
607
|
-
def initialize(only: nil, with: nil, except: nil,
|
|
608
|
-
|
|
609
|
-
check_initiate_params = config.check_initiate_params if modifiers && check_initiate_params.nil?
|
|
610
|
-
@plan = self.class.plan_cache.fetch(only, with, except, check_initiate_params: modifiers && check_initiate_params)
|
|
675
|
+
def initialize(only: nil, with: nil, except: nil, preset: nil, check_selection: nil)
|
|
676
|
+
@plan = self.class.plan_cache.fetch(only, with, except, preset, check_selection: check_selection)
|
|
611
677
|
@direct_hash_builder = @plan.direct_hash_builder
|
|
612
678
|
end
|
|
613
679
|
|
|
@@ -627,9 +693,11 @@ class Serega
|
|
|
627
693
|
# ```
|
|
628
694
|
#
|
|
629
695
|
def call(object, context: nil, many: nil)
|
|
696
|
+
# One Hash source of a flat plan skips #serialize_to and the run: 73 ns
|
|
697
|
+
# and 1 object instead of 136 ns and 3 objects (Ruby 4.0, YJIT).
|
|
630
698
|
builder = @direct_hash_builder
|
|
631
699
|
if builder && object && !many && (context.nil? || context.is_a?(Hash))
|
|
632
|
-
many = SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
|
|
700
|
+
many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
|
|
633
701
|
return builder.call_one(object, context.nil? ? FROZEN_EMPTY_HASH : context) unless many
|
|
634
702
|
end
|
|
635
703
|
|
|
@@ -671,17 +739,13 @@ class Serega
|
|
|
671
739
|
|
|
672
740
|
private
|
|
673
741
|
|
|
674
|
-
def config
|
|
675
|
-
self.class.config
|
|
676
|
-
end
|
|
677
|
-
|
|
678
742
|
# Serializes the object with the serialization options. `opts` holds the
|
|
679
743
|
# options other than :context and :many.
|
|
680
744
|
def serialize_to(mode, object, context, many, opts)
|
|
681
745
|
SeregaValidations::CheckSerializeKeywords.call(context, many)
|
|
682
746
|
context ||= FROZEN_EMPTY_HASH
|
|
683
747
|
object = prepare_objects(object, context) if self.class.prepare_initial_objects_signature
|
|
684
|
-
many = SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
|
|
748
|
+
many = object.instance_of?(Array) || SeregaUtils::CollectionDetector.call(object, @plan.collection_classes) if many.nil?
|
|
685
749
|
serialize(object, opts, context, many, mode)
|
|
686
750
|
end
|
|
687
751
|
|
data/sig/serega.rbs
CHANGED
|
@@ -4,6 +4,12 @@ class Serega
|
|
|
4
4
|
# Fields of the `only:`, `except:` and `with:` options
|
|
5
5
|
type fields = Symbol | String | Array[fields] | Hash[Symbol | String, fields]
|
|
6
6
|
|
|
7
|
+
# Names of the `preset:` option
|
|
8
|
+
type preset_names = Symbol | String | Array[Symbol | String]
|
|
9
|
+
|
|
10
|
+
# Definition of a preset: the presets it includes and its own field selection
|
|
11
|
+
type preset_definition = { presets: Array[Symbol], only: Hash[Symbol, untyped], with: Hash[Symbol, untyped], except: Hash[Symbol, untyped] }
|
|
12
|
+
|
|
7
13
|
# Serialization context
|
|
8
14
|
type context = Hash[untyped, untyped]
|
|
9
15
|
|
|
@@ -19,6 +25,10 @@ class Serega
|
|
|
19
25
|
|
|
20
26
|
def attribute: (Symbol | String name, **untyped opts) ?{ () [self: singleton(Serega)] -> void } -> SeregaAttribute
|
|
21
27
|
|
|
28
|
+
def presets: () -> Hash[Symbol, preset_definition]
|
|
29
|
+
|
|
30
|
+
def preset: (Symbol | String name, ?preset: preset_names?, ?only: fields?, ?with: fields?, ?except: fields?) -> preset_definition
|
|
31
|
+
|
|
22
32
|
def batch: (Symbol | String name, untyped loader) -> void
|
|
23
33
|
| (Symbol | String name) { (untyped objects) -> untyped } -> void
|
|
24
34
|
| (Symbol | String name) { (untyped objects, context context) -> untyped } -> void
|
|
@@ -44,17 +54,17 @@ class Serega
|
|
|
44
54
|
|
|
45
55
|
# Results are `untyped`: plugins change their shape. Options that plugins
|
|
46
56
|
# add at load time, such as `root:`, are not in these signatures.
|
|
47
|
-
def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?
|
|
57
|
+
def call: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
48
58
|
alias to_h call
|
|
49
|
-
def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?
|
|
50
|
-
def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?
|
|
51
|
-
def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?
|
|
59
|
+
def to_data: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
60
|
+
def to_struct: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> untyped
|
|
61
|
+
def to_json: (untyped object, ?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?, ?context: context?, ?many: bool?) -> String
|
|
52
62
|
|
|
53
63
|
def make_shareable!: () -> self
|
|
54
64
|
end
|
|
55
65
|
|
|
56
66
|
module InstanceMethods
|
|
57
|
-
def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?
|
|
67
|
+
def initialize: (?only: fields?, ?with: fields?, ?except: fields?, ?preset: preset_names?, ?check_selection: bool?) -> void
|
|
58
68
|
|
|
59
69
|
def call: (untyped object, ?context: context?, ?many: bool?) -> untyped
|
|
60
70
|
alias to_h call
|
|
@@ -93,8 +103,8 @@ class Serega
|
|
|
93
103
|
include SeregaConfigInstanceMethods
|
|
94
104
|
|
|
95
105
|
module SeregaConfigInstanceMethods
|
|
96
|
-
def
|
|
97
|
-
def
|
|
106
|
+
def check_selection: () -> bool
|
|
107
|
+
def check_selection=: (bool value) -> bool
|
|
98
108
|
|
|
99
109
|
def check_attribute_name: () -> bool
|
|
100
110
|
def check_attribute_name=: (bool value) -> bool
|
|
@@ -105,6 +115,12 @@ class Serega
|
|
|
105
115
|
def delegate_default_allow_nil: () -> bool
|
|
106
116
|
def delegate_default_allow_nil=: (bool value) -> bool
|
|
107
117
|
|
|
118
|
+
def compact: () -> bool
|
|
119
|
+
def compact=: (bool value) -> bool
|
|
120
|
+
|
|
121
|
+
def key_transform: () -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
|
|
122
|
+
def key_transform=: (:camel_case | ^(Symbol) -> (Symbol | String) | nil value) -> (:camel_case | ^(Symbol) -> (Symbol | String) | nil)
|
|
123
|
+
|
|
108
124
|
def hide_by_default: () -> (bool | :auto)
|
|
109
125
|
def hide_by_default=: (bool | :auto value) -> (bool | :auto)
|
|
110
126
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: serega
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.51.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andrey
|
|
@@ -24,13 +24,17 @@ files:
|
|
|
24
24
|
- VERSION
|
|
25
25
|
- lib/serega.rb
|
|
26
26
|
- lib/serega/attribute.rb
|
|
27
|
+
- lib/serega/attribute/serializer_reference.rb
|
|
28
|
+
- lib/serega/attribute/value_code.rb
|
|
27
29
|
- lib/serega/attribute_normalizer.rb
|
|
28
30
|
- lib/serega/attribute_normalizers/batch.rb
|
|
29
31
|
- lib/serega/attribute_normalizers/block.rb
|
|
30
32
|
- lib/serega/attribute_normalizers/delegate.rb
|
|
31
33
|
- lib/serega/attribute_normalizers/hash_access.rb
|
|
32
34
|
- lib/serega/attribute_normalizers/preloads.rb
|
|
35
|
+
- lib/serega/attribute_value_resolvers/base.rb
|
|
33
36
|
- lib/serega/attribute_value_resolvers/batch.rb
|
|
37
|
+
- lib/serega/attribute_value_resolvers/callable.rb
|
|
34
38
|
- lib/serega/attribute_value_resolvers/const.rb
|
|
35
39
|
- lib/serega/attribute_value_resolvers/delegate.rb
|
|
36
40
|
- lib/serega/attribute_value_resolvers/hash_access.rb
|
|
@@ -42,11 +46,17 @@ files:
|
|
|
42
46
|
- lib/serega/engine/pulls.rb
|
|
43
47
|
- lib/serega/engine/run.rb
|
|
44
48
|
- lib/serega/errors.rb
|
|
45
|
-
- lib/serega/
|
|
49
|
+
- lib/serega/formatters.rb
|
|
50
|
+
- lib/serega/formatters/callable.rb
|
|
51
|
+
- lib/serega/formatters/method.rb
|
|
46
52
|
- lib/serega/plan.rb
|
|
47
53
|
- lib/serega/plan/cache.rb
|
|
54
|
+
- lib/serega/plan/depth_limit.rb
|
|
48
55
|
- lib/serega/plan/per_ractor_cache.rb
|
|
49
|
-
- lib/serega/
|
|
56
|
+
- lib/serega/plan/point.rb
|
|
57
|
+
- lib/serega/plan/presets.rb
|
|
58
|
+
- lib/serega/plan/selection.rb
|
|
59
|
+
- lib/serega/plan/selection_planner.rb
|
|
50
60
|
- lib/serega/plugins.rb
|
|
51
61
|
- lib/serega/plugins/activerecord_preloads/activerecord_preloads.rb
|
|
52
62
|
- lib/serega/plugins/activerecord_preloads/lib/preloader.rb
|
|
@@ -58,15 +68,20 @@ files:
|
|
|
58
68
|
- lib/serega/presenter.rb
|
|
59
69
|
- lib/serega/result_builder.rb
|
|
60
70
|
- lib/serega/result_code.rb
|
|
71
|
+
- lib/serega/result_code/attribute_point_code.rb
|
|
72
|
+
- lib/serega/result_code/point_code.rb
|
|
73
|
+
- lib/serega/result_code/relation_point_code.rb
|
|
61
74
|
- lib/serega/shareable.rb
|
|
62
75
|
- lib/serega/source_group.rb
|
|
76
|
+
- lib/serega/utils/camel_case.rb
|
|
63
77
|
- lib/serega/utils/collection_detector.rb
|
|
64
78
|
- lib/serega/utils/deep_merge.rb
|
|
65
79
|
- lib/serega/utils/enum_deep_dup.rb
|
|
66
80
|
- lib/serega/utils/enum_deep_freeze.rb
|
|
67
81
|
- lib/serega/utils/method_signature.rb
|
|
68
|
-
- lib/serega/utils/
|
|
82
|
+
- lib/serega/utils/parse_string_selection.rb
|
|
69
83
|
- lib/serega/utils/ractor_local.rb
|
|
84
|
+
- lib/serega/utils/ruby_literal.rb
|
|
70
85
|
- lib/serega/utils/serialized_attribute_error.rb
|
|
71
86
|
- lib/serega/utils/to_hash.rb
|
|
72
87
|
- lib/serega/validations/attribute/check_block.rb
|
|
@@ -81,13 +96,18 @@ files:
|
|
|
81
96
|
- lib/serega/validations/attribute/check_opt_many.rb
|
|
82
97
|
- lib/serega/validations/attribute/check_opt_method.rb
|
|
83
98
|
- lib/serega/validations/attribute/check_opt_preload.rb
|
|
99
|
+
- lib/serega/validations/attribute/check_opt_preset.rb
|
|
100
|
+
- lib/serega/validations/attribute/check_opt_selection.rb
|
|
84
101
|
- lib/serega/validations/attribute/check_opt_serializer.rb
|
|
85
102
|
- lib/serega/validations/attribute/check_opt_value.rb
|
|
86
103
|
- lib/serega/validations/check_attribute_params.rb
|
|
87
104
|
- lib/serega/validations/check_batch_loader_params.rb
|
|
105
|
+
- lib/serega/validations/check_definition_selection.rb
|
|
88
106
|
- lib/serega/validations/check_formatter.rb
|
|
107
|
+
- lib/serega/validations/check_preset_params.rb
|
|
108
|
+
- lib/serega/validations/check_relation_selection.rb
|
|
109
|
+
- lib/serega/validations/check_selection.rb
|
|
89
110
|
- lib/serega/validations/check_serialize_keywords.rb
|
|
90
|
-
- lib/serega/validations/initiate/check_modifiers.rb
|
|
91
111
|
- lib/serega/validations/utils/check_allowed_keys.rb
|
|
92
112
|
- lib/serega/validations/utils/check_hash_access_value.rb
|
|
93
113
|
- lib/serega/validations/utils/check_opt_is_bool.rb
|
data/lib/serega/formatter.rb
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
class Serega
|
|
4
|
-
#
|
|
5
|
-
# Formats attribute values. A formatter is one of:
|
|
6
|
-
# - a callable that gets the value, and the context when it has a second
|
|
7
|
-
# parameter or the :ctx keyword;
|
|
8
|
-
# - a method name, called on the value;
|
|
9
|
-
# - an Array of a method name and its arguments, for example `[:round, 2]`;
|
|
10
|
-
# - a Hash with one of them as `:use` and the `:allow_nil` option.
|
|
11
|
-
#
|
|
12
|
-
# With `allow_nil: true` a nil value stays nil and is not formatted.
|
|
13
|
-
#
|
|
14
|
-
class SeregaFormatter # :nodoc:
|
|
15
|
-
# Callable that formats a value, nil for a method formatter
|
|
16
|
-
attr_reader :callable
|
|
17
|
-
|
|
18
|
-
# Arguments of the method, nil for a callable formatter
|
|
19
|
-
attr_reader :args
|
|
20
|
-
|
|
21
|
-
def initialize(formatter)
|
|
22
|
-
formatter = {use: formatter} unless formatter.is_a?(Hash)
|
|
23
|
-
use = formatter[:use]
|
|
24
|
-
@allow_nil = formatter.fetch(:allow_nil, false)
|
|
25
|
-
|
|
26
|
-
if use.respond_to?(:call)
|
|
27
|
-
@method_name = nil
|
|
28
|
-
@args = nil
|
|
29
|
-
@callable = use
|
|
30
|
-
@signature = SeregaUtils::MethodSignature.call(use, pos_limit: 2, keyword_args: [:ctx])
|
|
31
|
-
else
|
|
32
|
-
method_name, *args = use
|
|
33
|
-
@method_name = method_name
|
|
34
|
-
@args = args
|
|
35
|
-
@callable = nil
|
|
36
|
-
@signature = nil
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Formats the value
|
|
41
|
-
def call(value, context)
|
|
42
|
-
return if value.nil? && @allow_nil
|
|
43
|
-
return value.public_send(@method_name, *@args) if @method_name
|
|
44
|
-
|
|
45
|
-
case @signature
|
|
46
|
-
when "1" then @callable.call(value)
|
|
47
|
-
when "1_ctx" then @callable.call(value, ctx: context)
|
|
48
|
-
else @callable.call(value, context) # "2"
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
# Ruby code that formats the value of `value_code`. A method is called
|
|
53
|
-
# directly, for example `(source.created_at).iso8601(3)`. Literal
|
|
54
|
-
# arguments are written in the code, other arguments are read from the
|
|
55
|
-
# Array that `args_code` reads. A callable is read with `callable_code`.
|
|
56
|
-
def code(value_code, callable_code, args_code)
|
|
57
|
-
return method_call_code(value_code, args_code) if @method_name
|
|
58
|
-
return unless @signature == "1"
|
|
59
|
-
return "#{callable_code}.call(#{value_code})" unless @allow_nil
|
|
60
|
-
|
|
61
|
-
"(value = #{value_code}).nil? ? nil : #{callable_code}.call(value)"
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
private
|
|
65
|
-
|
|
66
|
-
def method_call_code(value_code, args_code)
|
|
67
|
-
operator = @allow_nil ? "&." : "."
|
|
68
|
-
arguments = @args.each_with_index.map { |arg, index| argument_code(arg, index, args_code) }
|
|
69
|
-
|
|
70
|
-
unless SeregaResultCode::PLAIN_METHOD_NAME.match?(@method_name)
|
|
71
|
-
send_arguments = [@method_name.inspect, *arguments]
|
|
72
|
-
return "(#{value_code})#{operator}public_send(#{send_arguments.join(", ")})"
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
return "(#{value_code})#{operator}#{@method_name}" if arguments.empty?
|
|
76
|
-
|
|
77
|
-
"(#{value_code})#{operator}#{@method_name}(#{arguments.join(", ")})"
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# Literal arguments are written in the code, other arguments are read
|
|
81
|
-
# from the Array of arguments
|
|
82
|
-
def argument_code(arg, index, args_code)
|
|
83
|
-
literal?(arg) ? literal_code(arg) : "#{args_code}[#{index}]"
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
def literal?(arg)
|
|
87
|
-
case arg
|
|
88
|
-
when Integer, Symbol, String, true, false, nil then true
|
|
89
|
-
when Float then arg.finite?
|
|
90
|
-
else false
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def literal_code(arg)
|
|
95
|
-
arg.is_a?(String) ? "#{arg.inspect}.freeze" : arg.inspect
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|