json-schema-serializer 1.6.0 → 1.7.1
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/.github/workflows/ruby.yml +0 -0
- data/.gitignore +0 -0
- data/.prettierrc +0 -0
- data/.rspec +0 -0
- data/.rubocop.yml +0 -0
- data/.rubocop_airbnb.yml +0 -0
- data/CHANGELOG.md +9 -0
- data/Gemfile +0 -0
- data/LICENSE +0 -0
- data/README.md +138 -1
- data/Rakefile +0 -0
- data/json-schema-serializer.gemspec +0 -0
- data/lib/json/schema/serializer.rb +32 -2
- data/lib/json/schema/serializer/version.rb +1 -1
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8520da63406ec92ce27f94214e6377eb0235fa22745c1dd9473fb597bfc2acdd
|
4
|
+
data.tar.gz: 36de8e40ad358955d937e2b46141c0da13e9b7cff328a26194e6e52991c1b2cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6a9d1927d21adc4c7a499313b195e63465302018e4575a83c9095236c31df86eb1037e5d06618ad07fa0674c620c5cd2b58aad0aae2ae04884f3c0f9b6592c8
|
7
|
+
data.tar.gz: f3d64406270a9b00f816106611cf1327ed5eab8bd9ec5472bb577fbfda8f0ef415404563fda4fd50008441388d03f655d82c0085f9dd8b5aba95438c1d50d9f7
|
data/.github/workflows/ruby.yml
CHANGED
File without changes
|
data/.gitignore
CHANGED
File without changes
|
data/.prettierrc
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/.rubocop.yml
CHANGED
File without changes
|
data/.rubocop_airbnb.yml
CHANGED
File without changes
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
File without changes
|
data/LICENSE
CHANGED
File without changes
|
data/README.md
CHANGED
@@ -157,6 +157,133 @@ serializer_injected_with_context = JSON::Schema::Serializer.new(
|
|
157
157
|
|
158
158
|
serializer_injected_with_context.serialize({ id: 1 })
|
159
159
|
# => { "id" => 1, "score" => 100 }
|
160
|
+
|
161
|
+
#
|
162
|
+
# inject in serializer
|
163
|
+
#
|
164
|
+
|
165
|
+
class ParentSerializer
|
166
|
+
include JSON::Schema::Serializer::WithContext
|
167
|
+
|
168
|
+
def initialize(model, context = nil)
|
169
|
+
@model = model
|
170
|
+
@context = context
|
171
|
+
end
|
172
|
+
|
173
|
+
def id
|
174
|
+
@model[:id]
|
175
|
+
end
|
176
|
+
|
177
|
+
def score
|
178
|
+
@context[:parent_scores][@model[:id]]
|
179
|
+
end
|
180
|
+
|
181
|
+
def child
|
182
|
+
# it can be
|
183
|
+
# with_context(context) { data }
|
184
|
+
# with_context(data, context)
|
185
|
+
# with_context(data: data, context: context)
|
186
|
+
with_context(@context.merge(child_scores: { 1 => 100, 2 => 200 })) do
|
187
|
+
@model[:child]
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
class ChildSerializer
|
193
|
+
def initialize(model, context = nil)
|
194
|
+
@model = model
|
195
|
+
@context = context
|
196
|
+
end
|
197
|
+
|
198
|
+
def id
|
199
|
+
@model[:id]
|
200
|
+
end
|
201
|
+
|
202
|
+
def score
|
203
|
+
@context[:child_scores][@model[:id]]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
serializer_injected_with_context_in_serializer = JSON::Schema::Serializer.new(
|
208
|
+
{
|
209
|
+
type: :object,
|
210
|
+
inject: :Parent,
|
211
|
+
properties: {
|
212
|
+
id: { type: :integer },
|
213
|
+
score: { type: :integer },
|
214
|
+
child: {
|
215
|
+
type: :object,
|
216
|
+
inject: :Child,
|
217
|
+
properties: {
|
218
|
+
id: { type: :integer },
|
219
|
+
score: { type: :integer },
|
220
|
+
},
|
221
|
+
},
|
222
|
+
},
|
223
|
+
},
|
224
|
+
{
|
225
|
+
inject_key: :inject,
|
226
|
+
injectors: {
|
227
|
+
Parent: ParentSerializer,
|
228
|
+
Child: ChildSerializer,
|
229
|
+
},
|
230
|
+
inject_context: { 1 => 10, 2 => 20 },
|
231
|
+
},
|
232
|
+
)
|
233
|
+
|
234
|
+
serializer_injected_with_context_in_serializer.serialize({ id: 1, child: { id: 2 } })
|
235
|
+
# => { "id" => 1, "score" => 10, "child" => { "id" => 2, "score" => 200 } }
|
236
|
+
|
237
|
+
#
|
238
|
+
# also you can inject context with arraylike data
|
239
|
+
#
|
240
|
+
|
241
|
+
class ItemsSerializer
|
242
|
+
include JSON::Schema::Serializer::WithContext
|
243
|
+
|
244
|
+
def initialize(models, context = nil)
|
245
|
+
@models = models
|
246
|
+
@context = context
|
247
|
+
end
|
248
|
+
|
249
|
+
def map(&block)
|
250
|
+
context = (@context || {}).merge(scores: {...})
|
251
|
+
@models.map { |model| block.call(with_context(model, context)) }
|
252
|
+
# CAUTION!
|
253
|
+
# not like below!
|
254
|
+
# with_context(@models.map(&block), context)
|
255
|
+
# with_context(context) { @models.map(&block) }
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
#
|
260
|
+
# inject model can initialize by keywords
|
261
|
+
#
|
262
|
+
|
263
|
+
class KeywordSerializer
|
264
|
+
def initialize(data:, context: nil)
|
265
|
+
@data = data
|
266
|
+
@context = context
|
267
|
+
end
|
268
|
+
|
269
|
+
...
|
270
|
+
end
|
271
|
+
|
272
|
+
serializer_with_keyword_init_inject = JSON::Schema::Serializer.new(
|
273
|
+
{
|
274
|
+
type: :object,
|
275
|
+
inject: :Keyword,
|
276
|
+
properties: { ... },
|
277
|
+
},
|
278
|
+
{
|
279
|
+
inject_key: :inject,
|
280
|
+
injectors: {
|
281
|
+
Keyword: KeywordSerializer,
|
282
|
+
Child: ChildSerializer,
|
283
|
+
},
|
284
|
+
inject_by_keyword: true, # <- keyword_init!
|
285
|
+
},
|
286
|
+
)
|
160
287
|
```
|
161
288
|
|
162
289
|
### "additionalProperties"
|
@@ -256,12 +383,14 @@ new({
|
|
256
383
|
}, { schema_key_transform_for_output: ->(name) { name.underscore } }).serialize({ userCount: 1 }) == { "user_count" => 1 }
|
257
384
|
```
|
258
385
|
|
259
|
-
#### options[:injectors] [Hashlike<String, Class>, Class], options[:inject_key] [String, Symbol], options[:inject_context] [any]
|
386
|
+
#### options[:injectors] [Hashlike<String, Class>, Class], options[:inject_key] [String, Symbol], options[:inject_context] [any], options[:inject_by_keyword] [Boolean]
|
260
387
|
|
261
388
|
If schema has inject key, the serializer treats data by `injectors[inject_key].new(data)` (or `injectors.send(inject_key).new(data)`).
|
262
389
|
|
263
390
|
And if `inject_context` is present, `injectors[inject_key].new(data, inject_context)` (or `injectors.send(inject_key).new(data, inject_context)`).
|
264
391
|
|
392
|
+
And if `inject_by_keyword` is true, `new(data, inject_context)` will be `new(data: data, context: inject_context)`.
|
393
|
+
|
265
394
|
See examples in [Usage](#usage).
|
266
395
|
|
267
396
|
CAUTION: In many case you should define the `nil?` method in the injector class because Injector always initialized by `Injector.new(obj)` even if obj == nil.
|
@@ -324,6 +453,14 @@ Serialize the object data by the schema.
|
|
324
453
|
|
325
454
|
Serialize target object. The serializer tries data["foo"], data[:foo] and data.foo!
|
326
455
|
|
456
|
+
## JSON::Schema::Serializer::WithContext API
|
457
|
+
|
458
|
+
### #with_context!(data, context), #with_context!(data: data, context: context), #with_context!(context) { data }
|
459
|
+
|
460
|
+
If you use `with_context!(data, context)` as the return value of the serializer, then "child" serializer can use that context.
|
461
|
+
|
462
|
+
See examples in [Usage](#usage).
|
463
|
+
|
327
464
|
## License
|
328
465
|
|
329
466
|
Zlib License
|
data/Rakefile
CHANGED
File without changes
|
File without changes
|
@@ -14,6 +14,22 @@ module JSON
|
|
14
14
|
Walker.walk(@schema, data, true, @options)
|
15
15
|
end
|
16
16
|
|
17
|
+
DataWithContext = Struct.new(:data, :context, keyword_init: true)
|
18
|
+
|
19
|
+
module WithContext
|
20
|
+
ARG2_NOT_GIVEN = :__json_schema_serializer_arg2_not_given__
|
21
|
+
|
22
|
+
def with_context!(arg1, arg2 = ARG2_NOT_GIVEN) # rubocop:disable Airbnb/OptArgParameters
|
23
|
+
if block_given?
|
24
|
+
DataWithContext.new(data: yield, context: arg1)
|
25
|
+
elsif arg2 == ARG2_NOT_GIVEN
|
26
|
+
DataWithContext.new(arg1)
|
27
|
+
else
|
28
|
+
DataWithContext.new(data: arg1, context: arg2)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
17
33
|
class Walker
|
18
34
|
class << self
|
19
35
|
TimeWithZone = defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil
|
@@ -27,11 +43,25 @@ module JSON
|
|
27
43
|
if options[:inject_key]
|
28
44
|
inject_key = try_hash(schema, options[:inject_key])
|
29
45
|
injector = try_hash(options[:injectors], inject_key) if inject_key
|
46
|
+
if obj.instance_of?(JSON::Schema::Serializer::DataWithContext)
|
47
|
+
options = options.merge(inject_context: obj.context)
|
48
|
+
obj = obj.data
|
49
|
+
end
|
30
50
|
if injector
|
31
51
|
if options[:inject_context]
|
32
|
-
obj =
|
52
|
+
obj =
|
53
|
+
if options[:inject_by_keyword]
|
54
|
+
injector.new(data: obj, context: options[:inject_context])
|
55
|
+
else
|
56
|
+
injector.new(obj, options[:inject_context])
|
57
|
+
end
|
33
58
|
else
|
34
|
-
obj =
|
59
|
+
obj =
|
60
|
+
if options[:inject_by_keyword]
|
61
|
+
injector.new(data: obj)
|
62
|
+
else
|
63
|
+
injector.new(obj)
|
64
|
+
end
|
35
65
|
end
|
36
66
|
end
|
37
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema-serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Narazaka
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -195,7 +195,7 @@ metadata:
|
|
195
195
|
homepage_uri: https://github.com/Narazaka/json-schema-serializer
|
196
196
|
source_code_uri: https://github.com/Narazaka/json-schema-serializer.git
|
197
197
|
changelog_uri: https://github.com/Narazaka/json-schema-serializer/blob/master/CHANGELOG.md
|
198
|
-
documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/1.
|
198
|
+
documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/1.7.1
|
199
199
|
post_install_message:
|
200
200
|
rdoc_options: []
|
201
201
|
require_paths:
|
@@ -211,8 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
211
211
|
- !ruby/object:Gem::Version
|
212
212
|
version: '0'
|
213
213
|
requirements: []
|
214
|
-
|
215
|
-
rubygems_version: 2.7.6
|
214
|
+
rubygems_version: 3.0.3
|
216
215
|
signing_key:
|
217
216
|
specification_version: 4
|
218
217
|
summary: JSON Schema based serializer
|