panko_serializer 0.4.4 → 0.5.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/ext/panko_serializer/panko_serializer.c +10 -2
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.c +2 -47
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +0 -6
- data/lib/panko/array_serializer.rb +5 -2
- data/lib/panko/serialization_descriptor.rb +16 -8
- data/lib/panko/serializer.rb +28 -12
- data/lib/panko/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5d2e6523f47bf65ff0dca7931267fc0d0402ac68f4490ff643eeafa589b8249d
|
4
|
+
data.tar.gz: 9fd3025b410b6ed6a29206ae02d868300b7b4fcc458987aaaeadb2117466bad3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64cb44e5e8b0dd3406eb720b34aff1eea7bd29f51e7f62a9b98d79690f76d4d4d1f645754f75b5efc880bca313a646cb52aeda667968f6917216bdb311ad8efd
|
7
|
+
data.tar.gz: 40ffe87725181f27edbeaf5e023c09b70ef836c20fdd8ab018c70ee59d0df42a5ae9c370d9eeb67e215be6404cb3c4c0f826f33ea6b6c6db0a162cac66c12c45
|
@@ -9,6 +9,9 @@ static ID pop_id;
|
|
9
9
|
|
10
10
|
static ID to_a_id;
|
11
11
|
|
12
|
+
static ID object_id;
|
13
|
+
static ID serialization_context_id;
|
14
|
+
|
12
15
|
void write_value(VALUE str_writer, VALUE key, VALUE value) {
|
13
16
|
rb_funcall(str_writer, push_value_id, 2, value, key);
|
14
17
|
}
|
@@ -24,8 +27,8 @@ void serialize_method_fields(VALUE subject,
|
|
24
27
|
return;
|
25
28
|
}
|
26
29
|
|
27
|
-
serializer =
|
28
|
-
|
30
|
+
serializer = descriptor->serializer;
|
31
|
+
rb_ivar_set(serializer, object_id, subject);
|
29
32
|
|
30
33
|
for (i = 0; i < RARRAY_LEN(method_fields); i++) {
|
31
34
|
volatile VALUE attribute_name = RARRAY_AREF(method_fields, i);
|
@@ -34,6 +37,8 @@ void serialize_method_fields(VALUE subject,
|
|
34
37
|
|
35
38
|
write_value(str_writer, rb_sym2str(attribute_name), result);
|
36
39
|
}
|
40
|
+
|
41
|
+
rb_ivar_set(serializer, object_id, Qnil);
|
37
42
|
}
|
38
43
|
|
39
44
|
void serialize_fields(VALUE subject,
|
@@ -155,6 +160,9 @@ void Init_panko_serializer() {
|
|
155
160
|
push_object_id = rb_intern("push_object");
|
156
161
|
pop_id = rb_intern("pop");
|
157
162
|
to_a_id = rb_intern("to_a");
|
163
|
+
object_id = rb_intern("@object");
|
164
|
+
serialization_context_id = rb_intern("@serialization_context");
|
165
|
+
|
158
166
|
|
159
167
|
VALUE mPanko = rb_define_module("Panko");
|
160
168
|
|
@@ -3,8 +3,8 @@
|
|
3
3
|
VALUE cSerializationDescriptor;
|
4
4
|
|
5
5
|
static ID context_id;
|
6
|
-
static ID scope_id;
|
7
6
|
static ID object_id;
|
7
|
+
static ID sc_id;
|
8
8
|
|
9
9
|
static void sd_free(SerializationDescriptor sd) {
|
10
10
|
if (!sd) {
|
@@ -18,9 +18,6 @@ static void sd_free(SerializationDescriptor sd) {
|
|
18
18
|
sd->has_one_associations = Qnil;
|
19
19
|
sd->has_many_associations = Qnil;
|
20
20
|
sd->aliases = Qnil;
|
21
|
-
sd->context = Qnil;
|
22
|
-
sd->scope = Qnil;
|
23
|
-
|
24
21
|
xfree(sd);
|
25
22
|
}
|
26
23
|
|
@@ -32,8 +29,6 @@ void sd_mark(SerializationDescriptor data) {
|
|
32
29
|
rb_gc_mark(data->has_one_associations);
|
33
30
|
rb_gc_mark(data->has_many_associations);
|
34
31
|
rb_gc_mark(data->aliases);
|
35
|
-
rb_gc_mark(data->context);
|
36
|
-
rb_gc_mark(data->scope);
|
37
32
|
}
|
38
33
|
|
39
34
|
static VALUE sd_new(int argc, VALUE* argv, VALUE self) {
|
@@ -46,8 +41,6 @@ static VALUE sd_new(int argc, VALUE* argv, VALUE self) {
|
|
46
41
|
sd->has_one_associations = Qnil;
|
47
42
|
sd->has_many_associations = Qnil;
|
48
43
|
sd->aliases = Qnil;
|
49
|
-
sd->context = Qnil;
|
50
|
-
sd->scope = Qnil;
|
51
44
|
|
52
45
|
sd->attributes_writer = create_empty_attributes_writer();
|
53
46
|
|
@@ -66,14 +59,6 @@ void sd_set_writer(SerializationDescriptor sd, VALUE subject) {
|
|
66
59
|
sd->attributes_writer = create_attributes_writer(subject);
|
67
60
|
}
|
68
61
|
|
69
|
-
VALUE sd_build_serializer(SerializationDescriptor sd) {
|
70
|
-
if (sd->serializer == Qnil) {
|
71
|
-
VALUE args[0];
|
72
|
-
sd->serializer = rb_class_new_instance(0, args, sd->serializer_type);
|
73
|
-
}
|
74
|
-
|
75
|
-
return sd->serializer;
|
76
|
-
}
|
77
62
|
|
78
63
|
VALUE sd_serializer_set(VALUE self, VALUE serializer) {
|
79
64
|
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
@@ -88,18 +73,6 @@ VALUE sd_serializer_ref(VALUE self) {
|
|
88
73
|
return sd->serializer;
|
89
74
|
}
|
90
75
|
|
91
|
-
void sd_apply_serializer_config(SerializationDescriptor descriptor,
|
92
|
-
VALUE serializer,
|
93
|
-
VALUE object) {
|
94
|
-
rb_ivar_set(serializer, object_id, object);
|
95
|
-
if (descriptor->context != Qnil && descriptor->context != Qundef) {
|
96
|
-
rb_ivar_set(serializer, context_id, descriptor->context);
|
97
|
-
}
|
98
|
-
|
99
|
-
if (descriptor->scope != Qnil && descriptor->scope != Qundef) {
|
100
|
-
rb_ivar_set(serializer, scope_id, descriptor->scope);
|
101
|
-
}
|
102
|
-
}
|
103
76
|
VALUE sd_attributes_set(VALUE self, VALUE attributes) {
|
104
77
|
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
105
78
|
|
@@ -173,22 +146,9 @@ VALUE public_sd_build_serializer(VALUE self) {
|
|
173
146
|
return sd_build_serializer(sd);
|
174
147
|
}
|
175
148
|
|
176
|
-
VALUE sd_context_set(VALUE self, VALUE context) {
|
177
|
-
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
178
|
-
sd->context = context;
|
179
|
-
return Qnil;
|
180
|
-
}
|
181
|
-
|
182
|
-
VALUE sd_scope_set(VALUE self, VALUE scope) {
|
183
|
-
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
184
|
-
sd->scope = scope;
|
185
|
-
return Qnil;
|
186
|
-
}
|
187
|
-
|
188
149
|
void panko_init_serialization_descriptor(VALUE mPanko) {
|
189
150
|
object_id = rb_intern("@object");
|
190
|
-
|
191
|
-
scope_id = rb_intern("@scope");
|
151
|
+
sc_id = rb_intern("@sc");
|
192
152
|
|
193
153
|
cSerializationDescriptor =
|
194
154
|
rb_define_class_under(mPanko, "SerializationDescriptor", rb_cObject);
|
@@ -225,9 +185,4 @@ void panko_init_serialization_descriptor(VALUE mPanko) {
|
|
225
185
|
|
226
186
|
rb_define_method(cSerializationDescriptor, "aliases=", sd_aliases_set, 1);
|
227
187
|
rb_define_method(cSerializationDescriptor, "aliases", sd_aliases_aref, 0);
|
228
|
-
|
229
|
-
rb_define_method(cSerializationDescriptor, "scope=", sd_scope_set, 1);
|
230
|
-
rb_define_method(cSerializationDescriptor, "context=", sd_context_set, 1);
|
231
|
-
rb_define_method(cSerializationDescriptor, "build_serializer",
|
232
|
-
public_sd_build_serializer, 0);
|
233
188
|
}
|
@@ -18,8 +18,6 @@ typedef struct _SerializationDescriptor {
|
|
18
18
|
VALUE has_one_associations;
|
19
19
|
VALUE has_many_associations;
|
20
20
|
|
21
|
-
VALUE context;
|
22
|
-
VALUE scope;
|
23
21
|
AttributesWriter attributes_writer;
|
24
22
|
} * SerializationDescriptor;
|
25
23
|
|
@@ -27,10 +25,6 @@ SerializationDescriptor sd_read(VALUE descriptor);
|
|
27
25
|
|
28
26
|
void sd_mark(SerializationDescriptor data);
|
29
27
|
|
30
|
-
VALUE sd_build_serializer(SerializationDescriptor descriptor);
|
31
|
-
void sd_apply_serializer_config(SerializationDescriptor descriptor,
|
32
|
-
VALUE serializer,
|
33
|
-
VALUE object);
|
34
28
|
void sd_set_writer(SerializationDescriptor sd, VALUE subject);
|
35
29
|
|
36
30
|
void panko_init_serialization_descriptor(VALUE mPanko);
|
@@ -22,8 +22,10 @@ Please pass valid each_serializer to ArraySerializer, for example:
|
|
22
22
|
scope: options[:scope]
|
23
23
|
}
|
24
24
|
|
25
|
-
@
|
26
|
-
|
25
|
+
@serialization_context = if options.key?(:context) || options.key?(:scope)
|
26
|
+
SerializationContext.new(options[:context], options[:scope])
|
27
|
+
end
|
28
|
+
@descriptor = Panko::SerializationDescriptor.build(@each_serializer, serializer_options, @serialization_context)
|
27
29
|
end
|
28
30
|
|
29
31
|
def to_json
|
@@ -41,6 +43,7 @@ Please pass valid each_serializer to ArraySerializer, for example:
|
|
41
43
|
def serialize_to_json(subjects)
|
42
44
|
writer = Oj::StringWriter.new(mode: :rails)
|
43
45
|
Panko.serialize_subjects(subjects.to_a, writer, @descriptor)
|
46
|
+
@descriptor.set_serialization_context(nil) if @serialization_context.present?
|
44
47
|
writer.to_s
|
45
48
|
end
|
46
49
|
end
|
@@ -6,15 +6,15 @@ module Panko
|
|
6
6
|
# Creates new description and apply the options
|
7
7
|
# on the new descriptor
|
8
8
|
#
|
9
|
-
def self.build(serializer, options = {})
|
9
|
+
def self.build(serializer, options = {}, serialization_context = nil)
|
10
10
|
backend = Panko::SerializationDescriptor.duplicate(serializer._descriptor)
|
11
11
|
|
12
12
|
options.merge! serializer.filters_for(options[:context], options[:scope]) if serializer.respond_to? :filters_for
|
13
13
|
|
14
|
-
backend.context = options[:context]
|
15
|
-
backend.scope = options[:scope]
|
16
14
|
backend.apply_filters(options)
|
17
15
|
|
16
|
+
backend.set_serialization_context(serialization_context)
|
17
|
+
|
18
18
|
backend
|
19
19
|
end
|
20
20
|
|
@@ -30,18 +30,26 @@ module Panko
|
|
30
30
|
backend.attributes = descriptor.attributes.dup
|
31
31
|
|
32
32
|
backend.method_fields = descriptor.method_fields.dup
|
33
|
-
|
34
|
-
backend.serializer = descriptor.serializer.reset unless descriptor.serializer.nil?
|
33
|
+
backend.serializer = descriptor.type.new(_skip_init: true)
|
35
34
|
|
36
35
|
backend.has_many_associations = descriptor.has_many_associations.dup
|
37
36
|
backend.has_one_associations = descriptor.has_one_associations.dup
|
38
37
|
|
39
|
-
backend.context = nil
|
40
|
-
backend.scope = nil
|
41
|
-
|
42
38
|
backend
|
43
39
|
end
|
44
40
|
|
41
|
+
def set_serialization_context(context)
|
42
|
+
serializer.instance_variable_set :@serialization_context, context if !method_fields.empty? && serializer.present?
|
43
|
+
|
44
|
+
has_many_associations.each do |assoc|
|
45
|
+
assoc.descriptor.set_serialization_context context
|
46
|
+
end
|
47
|
+
|
48
|
+
has_one_associations.each do |assoc|
|
49
|
+
assoc.descriptor.set_serialization_context context
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
45
53
|
#
|
46
54
|
# Applies attributes and association filters
|
47
55
|
#
|
data/lib/panko/serializer.rb
CHANGED
@@ -3,6 +3,15 @@
|
|
3
3
|
require_relative "serialization_descriptor"
|
4
4
|
require "oj"
|
5
5
|
|
6
|
+
class SerializationContext
|
7
|
+
attr_accessor :context, :scope
|
8
|
+
|
9
|
+
def initialize(context, scope)
|
10
|
+
@context = context
|
11
|
+
@scope = scope
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
6
15
|
module Panko
|
7
16
|
class Serializer
|
8
17
|
class << self
|
@@ -69,12 +78,25 @@ module Panko
|
|
69
78
|
end
|
70
79
|
|
71
80
|
def initialize(options = {})
|
72
|
-
|
73
|
-
|
74
|
-
|
81
|
+
# this "_skip_init" trick is so I can create serializers from serialization descriptor
|
82
|
+
return if options[:_skip_init]
|
83
|
+
|
84
|
+
@serialization_context = if options.key?(:context) || options.key?(:scope)
|
85
|
+
SerializationContext.new(options[:context], options[:scope])
|
86
|
+
end
|
87
|
+
|
88
|
+
@descriptor = Panko::SerializationDescriptor.build(self.class, options, @serialization_context)
|
89
|
+
end
|
90
|
+
|
91
|
+
def context
|
92
|
+
@serialization_context.context
|
93
|
+
end
|
94
|
+
|
95
|
+
def scope
|
96
|
+
@serialization_context.scope
|
75
97
|
end
|
76
98
|
|
77
|
-
attr_reader :object
|
99
|
+
attr_reader :object
|
78
100
|
|
79
101
|
def serialize(object)
|
80
102
|
Oj.load(serialize_to_json(object))
|
@@ -83,15 +105,9 @@ module Panko
|
|
83
105
|
def serialize_to_json(object)
|
84
106
|
writer = Oj::StringWriter.new(mode: :rails)
|
85
107
|
Panko.serialize_subject(object, writer, @descriptor)
|
86
|
-
writer.to_s
|
87
|
-
end
|
88
|
-
|
89
|
-
def reset
|
90
|
-
@object = nil
|
91
|
-
@context = nil
|
92
|
-
@scope = nil
|
93
108
|
|
94
|
-
|
109
|
+
@descriptor.set_serialization_context(nil) if @serialization_context.present?
|
110
|
+
writer.to_s
|
95
111
|
end
|
96
112
|
end
|
97
113
|
end
|
data/lib/panko/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: panko_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yosi Attias
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|