panko_serializer 0.3.9 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +4 -3
- data/Gemfile +0 -5
- data/Rakefile +1 -1
- data/benchmarks/app.rb +0 -3
- data/benchmarks/sanity.rb +2 -2
- data/benchmarks/type_casts/bm_active_record.rb +6 -1
- data/benchmarks/type_casts/bm_panko.rb +6 -1
- data/benchmarks/type_casts/support.rb +6 -0
- data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +7 -1
- data/ext/panko_serializer/panko_serializer.c +18 -26
- data/ext/panko_serializer/panko_serializer.h +2 -4
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.c +30 -3
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +3 -1
- data/lib/panko/array_serializer.rb +11 -2
- data/lib/panko/serialization_descriptor.rb +8 -1
- data/lib/panko/serializer.rb +17 -9
- data/lib/panko/version.rb +1 -1
- data/panko_serializer.gemspec +2 -2
- metadata +7 -8
- data/benchmarks/bm_fastjsonapi.rb +0 -56
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cd2e60ecfb8f4fd8557aa148cbd981d0552e72e40678e36168b494e31a426a9
|
4
|
+
data.tar.gz: 302d629080ee255fc8ae3117ec169a1f9db35c1e9ff4c598b9cad5a8f9830a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 53bdb412d51f9aaf0ab7d0bd7065117257f28cf6b468a51761d7bb8a4582d2d69b9105e02de65ec0964ddefdd158d169c821f50f03ae46f9a6a4243a9b5035b3
|
7
|
+
data.tar.gz: 86eae7fb5ecda7ae0d356d24cabf557d99bf817577b4b3a45a1f4fcbc5120f54bb2eece291e356047d42857af69c24e5870b6fdae45308881c6c4619241a1961
|
data/.travis.yml
CHANGED
@@ -2,7 +2,7 @@ sudo: false
|
|
2
2
|
cache: bundler
|
3
3
|
language: ruby
|
4
4
|
rvm:
|
5
|
-
- 2.5.
|
5
|
+
- 2.5.1
|
6
6
|
|
7
7
|
env:
|
8
8
|
global:
|
@@ -22,5 +22,6 @@ after_success:
|
|
22
22
|
|
23
23
|
env:
|
24
24
|
matrix:
|
25
|
-
- "RAILS_VERSION=4.2"
|
26
|
-
- "RAILS_VERSION=5.1"
|
25
|
+
- "RAILS_VERSION=4.2.0"
|
26
|
+
- "RAILS_VERSION=5.1.0"
|
27
|
+
- "RAILS_VERSION=5.2.0"
|
data/Gemfile
CHANGED
@@ -6,11 +6,8 @@ gemspec
|
|
6
6
|
raw_rails_version = ENV.fetch("RAILS_VERSION", "4.2")
|
7
7
|
rails_version = "~> #{raw_rails_version}"
|
8
8
|
|
9
|
-
gem "rails", rails_version
|
10
|
-
gem "railties", rails_version
|
11
9
|
gem "activesupport", rails_version
|
12
10
|
gem "activemodel", rails_version
|
13
|
-
gem "actionpack", rails_version
|
14
11
|
gem "activerecord", rails_version, group: :test
|
15
12
|
|
16
13
|
group :benchmarks do
|
@@ -23,8 +20,6 @@ group :benchmarks do
|
|
23
20
|
|
24
21
|
gem "benchmark-ips"
|
25
22
|
gem "active_model_serializers"
|
26
|
-
gem "fast_jsonapi" if raw_rails_version.start_with? "5"
|
27
|
-
|
28
23
|
gem "terminal-table"
|
29
24
|
end
|
30
25
|
|
data/Rakefile
CHANGED
@@ -72,7 +72,7 @@ end
|
|
72
72
|
|
73
73
|
desc "Type Casts - Benchmarks"
|
74
74
|
task :bm_type_casts do
|
75
|
-
run_benchmarks Dir[File.join(__dir__, "benchmarks", "type_casts", "bm_*")]
|
75
|
+
run_benchmarks Dir[File.join(__dir__, "benchmarks", "type_casts", "bm_*")], items_count: 0
|
76
76
|
end
|
77
77
|
|
78
78
|
desc "Sanity Benchmarks"
|
data/benchmarks/app.rb
CHANGED
@@ -2,12 +2,9 @@
|
|
2
2
|
require "bundler/setup"
|
3
3
|
|
4
4
|
|
5
|
-
require "rails"
|
6
5
|
require "active_model"
|
7
6
|
require "active_support"
|
8
7
|
require "active_support/json"
|
9
|
-
require "action_controller"
|
10
|
-
require "action_controller/railtie"
|
11
8
|
|
12
9
|
require "active_model_serializers"
|
13
10
|
require "panko_serializer"
|
data/benchmarks/sanity.rb
CHANGED
@@ -89,7 +89,7 @@ end
|
|
89
89
|
|
90
90
|
|
91
91
|
|
92
|
-
benchmark "SimpleWithMethodCall", PostFastWithMethodCallSerializer
|
93
|
-
benchmark "HasOne", PostWithHasOneFastSerializer
|
94
92
|
benchmark "Simple", PostFastSerializer
|
93
|
+
benchmark "HasOne", PostWithHasOneFastSerializer
|
94
|
+
benchmark "SimpleWithMethodCall", PostFastWithMethodCallSerializer
|
95
95
|
benchmark_aliased "Simple (aliased)", PostWithAliasFastSerializer
|
@@ -79,7 +79,12 @@ if ENV["RAILS_VERSION"].start_with? "4.2"
|
|
79
79
|
ar_type_convert ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Float, "1.23", 1.23
|
80
80
|
ar_type_convert ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Float, "Infinity", ::Float::INFINITY
|
81
81
|
end
|
82
|
-
|
82
|
+
if check_if_exists "ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json"
|
83
|
+
ar_type_convert ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json, '{"a":1}', {a:1}
|
84
|
+
end
|
85
|
+
if check_if_exists "ActiveRecord::Type::Json"
|
86
|
+
ar_type_convert ActiveRecord::Type::Json, '{"a":1}', {a:1}
|
87
|
+
end
|
83
88
|
|
84
89
|
db_ar_time
|
85
90
|
utc_ar_time
|
@@ -59,6 +59,11 @@ if ENV["RAILS_VERSION"].start_with? "4.2"
|
|
59
59
|
panko_type_convert ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Float, "Infinity", ::Float::INFINITY
|
60
60
|
end
|
61
61
|
|
62
|
-
|
62
|
+
if check_if_exists "ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json"
|
63
|
+
panko_type_convert ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Json, '{"a":1}', {a:1}
|
64
|
+
end
|
65
|
+
if check_if_exists "ActiveRecord::Type::Json"
|
66
|
+
panko_type_convert ActiveRecord::Type::Json, '{"a":1}', {a:1}
|
67
|
+
end
|
63
68
|
db_panko_time
|
64
69
|
utc_panko_time
|
@@ -14,4 +14,10 @@ def assert(type_name, from, to)
|
|
14
14
|
raise "#{type_name} - #{from.class} is not equals to #{to.class}" unless from.to_json == to.to_json
|
15
15
|
end
|
16
16
|
|
17
|
+
def check_if_exists(module_name)
|
18
|
+
mod = (module_name.constantize rescue nil)
|
19
|
+
return true if mod
|
20
|
+
return false unless mod
|
21
|
+
end
|
22
|
+
|
17
23
|
Time.zone = "UTC"
|
@@ -17,6 +17,7 @@ static VALUE ar_integer_type = Qundef;
|
|
17
17
|
static VALUE ar_boolean_type = Qundef;
|
18
18
|
static VALUE ar_date_time_type = Qundef;
|
19
19
|
static VALUE ar_time_zone_converter = Qundef;
|
20
|
+
static VALUE ar_json_type = Qundef;
|
20
21
|
|
21
22
|
static VALUE ar_pg_integer_type = Qundef;
|
22
23
|
static VALUE ar_pg_float_type = Qundef;
|
@@ -119,6 +120,10 @@ void cache_type_lookup() {
|
|
119
120
|
deserialize_from_db_id = rb_intern("type_cast_from_database");
|
120
121
|
}
|
121
122
|
|
123
|
+
if (rb_const_defined_at(ar_type, rb_intern("Json")) == (int)Qtrue) {
|
124
|
+
ar_json_type = rb_const_get_at(ar_type, rb_intern("Json"));
|
125
|
+
}
|
126
|
+
|
122
127
|
// TODO: if we get error or not, add this to some debug log
|
123
128
|
int isErrored;
|
124
129
|
rb_protect(cache_postgres_type_lookup, ar, &isErrored);
|
@@ -203,7 +208,8 @@ VALUE cast_integer_type(VALUE value) {
|
|
203
208
|
}
|
204
209
|
|
205
210
|
bool is_json_type(VALUE type_klass) {
|
206
|
-
return ar_pg_json_type != Qundef && type_klass == ar_pg_json_type
|
211
|
+
return ((ar_pg_json_type != Qundef && type_klass == ar_pg_json_type) ||
|
212
|
+
(ar_json_type != Qundef && type_klass == ar_json_type));
|
207
213
|
}
|
208
214
|
|
209
215
|
VALUE rescue_func() {
|
@@ -15,8 +15,7 @@ void write_value(VALUE str_writer, VALUE key, VALUE value) {
|
|
15
15
|
|
16
16
|
void serialize_method_fields(VALUE subject,
|
17
17
|
VALUE str_writer,
|
18
|
-
SerializationDescriptor descriptor
|
19
|
-
VALUE context) {
|
18
|
+
SerializationDescriptor descriptor) {
|
20
19
|
VALUE method_fields, serializer;
|
21
20
|
long i;
|
22
21
|
|
@@ -26,7 +25,7 @@ void serialize_method_fields(VALUE subject,
|
|
26
25
|
}
|
27
26
|
|
28
27
|
serializer = sd_build_serializer(descriptor);
|
29
|
-
sd_apply_serializer_config(serializer, subject
|
28
|
+
sd_apply_serializer_config(descriptor, serializer, subject);
|
30
29
|
|
31
30
|
for (i = 0; i < RARRAY_LEN(method_fields); i++) {
|
32
31
|
volatile VALUE attribute_name = RARRAY_AREF(method_fields, i);
|
@@ -39,17 +38,15 @@ void serialize_method_fields(VALUE subject,
|
|
39
38
|
|
40
39
|
void serialize_fields(VALUE subject,
|
41
40
|
VALUE str_writer,
|
42
|
-
SerializationDescriptor descriptor
|
43
|
-
VALUE context) {
|
41
|
+
SerializationDescriptor descriptor) {
|
44
42
|
descriptor->attributes_writer.write_attributes(
|
45
43
|
subject, descriptor->attributes, write_value, str_writer);
|
46
44
|
|
47
|
-
serialize_method_fields(subject, str_writer, descriptor
|
45
|
+
serialize_method_fields(subject, str_writer, descriptor);
|
48
46
|
}
|
49
47
|
|
50
48
|
void serialize_has_one_associations(VALUE subject,
|
51
49
|
VALUE str_writer,
|
52
|
-
VALUE context,
|
53
50
|
SerializationDescriptor descriptor,
|
54
51
|
VALUE associations) {
|
55
52
|
long i;
|
@@ -63,14 +60,13 @@ void serialize_has_one_associations(VALUE subject,
|
|
63
60
|
write_value(str_writer, association->name_str, value);
|
64
61
|
} else {
|
65
62
|
serialize_subject(association->name_str, value, str_writer,
|
66
|
-
association->descriptor
|
63
|
+
association->descriptor);
|
67
64
|
}
|
68
65
|
}
|
69
66
|
}
|
70
67
|
|
71
68
|
void serialize_has_many_associations(VALUE subject,
|
72
69
|
VALUE str_writer,
|
73
|
-
VALUE context,
|
74
70
|
SerializationDescriptor descriptor,
|
75
71
|
VALUE associations) {
|
76
72
|
long i;
|
@@ -84,7 +80,7 @@ void serialize_has_many_associations(VALUE subject,
|
|
84
80
|
write_value(str_writer, association->name_str, value);
|
85
81
|
} else {
|
86
82
|
serialize_subjects(association->name_str, value, str_writer,
|
87
|
-
association->descriptor
|
83
|
+
association->descriptor);
|
88
84
|
}
|
89
85
|
}
|
90
86
|
}
|
@@ -92,21 +88,20 @@ void serialize_has_many_associations(VALUE subject,
|
|
92
88
|
VALUE serialize_subject(VALUE key,
|
93
89
|
VALUE subject,
|
94
90
|
VALUE str_writer,
|
95
|
-
SerializationDescriptor descriptor
|
96
|
-
VALUE context) {
|
91
|
+
SerializationDescriptor descriptor) {
|
97
92
|
sd_set_writer(descriptor, subject);
|
98
93
|
|
99
94
|
rb_funcall(str_writer, push_object_id, 1, key);
|
100
95
|
|
101
|
-
serialize_fields(subject, str_writer, descriptor
|
96
|
+
serialize_fields(subject, str_writer, descriptor);
|
102
97
|
|
103
98
|
if (RARRAY_LEN(descriptor->has_one_associations) >= 0) {
|
104
|
-
serialize_has_one_associations(subject, str_writer,
|
99
|
+
serialize_has_one_associations(subject, str_writer, descriptor,
|
105
100
|
descriptor->has_one_associations);
|
106
101
|
}
|
107
102
|
|
108
103
|
if (RARRAY_LEN(descriptor->has_many_associations) >= 0) {
|
109
|
-
serialize_has_many_associations(subject, str_writer,
|
104
|
+
serialize_has_many_associations(subject, str_writer, descriptor,
|
110
105
|
descriptor->has_many_associations);
|
111
106
|
}
|
112
107
|
|
@@ -118,8 +113,7 @@ VALUE serialize_subject(VALUE key,
|
|
118
113
|
VALUE serialize_subjects(VALUE key,
|
119
114
|
VALUE subjects,
|
120
115
|
VALUE str_writer,
|
121
|
-
SerializationDescriptor descriptor
|
122
|
-
VALUE context) {
|
116
|
+
SerializationDescriptor descriptor) {
|
123
117
|
long i;
|
124
118
|
|
125
119
|
rb_funcall(str_writer, push_array_id, 1, key);
|
@@ -130,7 +124,7 @@ VALUE serialize_subjects(VALUE key,
|
|
130
124
|
|
131
125
|
for (i = 0; i < RARRAY_LEN(subjects); i++) {
|
132
126
|
volatile VALUE subject = RARRAY_AREF(subjects, i);
|
133
|
-
serialize_subject(Qnil, subject, str_writer, descriptor
|
127
|
+
serialize_subject(Qnil, subject, str_writer, descriptor);
|
134
128
|
}
|
135
129
|
|
136
130
|
rb_funcall(str_writer, pop_id, 0);
|
@@ -141,18 +135,16 @@ VALUE serialize_subjects(VALUE key,
|
|
141
135
|
VALUE serialize_subject_api(VALUE klass,
|
142
136
|
VALUE subject,
|
143
137
|
VALUE str_writer,
|
144
|
-
VALUE descriptor
|
145
|
-
VALUE context) {
|
138
|
+
VALUE descriptor) {
|
146
139
|
SerializationDescriptor sd = sd_read(descriptor);
|
147
|
-
return serialize_subject(Qnil, subject, str_writer, sd
|
140
|
+
return serialize_subject(Qnil, subject, str_writer, sd);
|
148
141
|
}
|
149
142
|
|
150
143
|
VALUE serialize_subjects_api(VALUE klass,
|
151
144
|
VALUE subjects,
|
152
145
|
VALUE str_writer,
|
153
|
-
VALUE descriptor
|
154
|
-
|
155
|
-
serialize_subjects(Qnil, subjects, str_writer, sd_read(descriptor), context);
|
146
|
+
VALUE descriptor) {
|
147
|
+
serialize_subjects(Qnil, subjects, str_writer, sd_read(descriptor));
|
156
148
|
|
157
149
|
return Qnil;
|
158
150
|
}
|
@@ -167,10 +159,10 @@ void Init_panko_serializer() {
|
|
167
159
|
VALUE mPanko = rb_define_module("Panko");
|
168
160
|
|
169
161
|
rb_define_singleton_method(mPanko, "serialize_subject", serialize_subject_api,
|
170
|
-
|
162
|
+
3);
|
171
163
|
|
172
164
|
rb_define_singleton_method(mPanko, "serialize_subjects",
|
173
|
-
serialize_subjects_api,
|
165
|
+
serialize_subjects_api, 3);
|
174
166
|
|
175
167
|
panko_init_serialization_descriptor(mPanko);
|
176
168
|
init_attributes_writer(mPanko);
|
@@ -8,11 +8,9 @@
|
|
8
8
|
VALUE serialize_subject(VALUE key,
|
9
9
|
VALUE subject,
|
10
10
|
VALUE str_writer,
|
11
|
-
SerializationDescriptor descriptor
|
12
|
-
VALUE context);
|
11
|
+
SerializationDescriptor descriptor);
|
13
12
|
|
14
13
|
VALUE serialize_subjects(VALUE key,
|
15
14
|
VALUE subjects,
|
16
15
|
VALUE str_writer,
|
17
|
-
SerializationDescriptor descriptor
|
18
|
-
VALUE context);
|
16
|
+
SerializationDescriptor descriptor);
|
@@ -3,6 +3,7 @@
|
|
3
3
|
VALUE cSerializationDescriptor;
|
4
4
|
|
5
5
|
static ID context_id;
|
6
|
+
static ID scope_id;
|
6
7
|
static ID object_id;
|
7
8
|
|
8
9
|
|
@@ -18,6 +19,8 @@ static void sd_free(SerializationDescriptor sd) {
|
|
18
19
|
sd->has_one_associations = Qnil;
|
19
20
|
sd->has_many_associations = Qnil;
|
20
21
|
sd->aliases = Qnil;
|
22
|
+
sd->context = Qnil;
|
23
|
+
sd->scope = Qnil;
|
21
24
|
|
22
25
|
xfree(sd);
|
23
26
|
}
|
@@ -30,6 +33,8 @@ void sd_mark(SerializationDescriptor data) {
|
|
30
33
|
rb_gc_mark(data->has_one_associations);
|
31
34
|
rb_gc_mark(data->has_many_associations);
|
32
35
|
rb_gc_mark(data->aliases);
|
36
|
+
rb_gc_mark(data->context);
|
37
|
+
rb_gc_mark(data->scope);
|
33
38
|
}
|
34
39
|
|
35
40
|
static VALUE sd_new(int argc, VALUE* argv, VALUE self) {
|
@@ -42,6 +47,8 @@ static VALUE sd_new(int argc, VALUE* argv, VALUE self) {
|
|
42
47
|
sd->has_one_associations = Qnil;
|
43
48
|
sd->has_many_associations = Qnil;
|
44
49
|
sd->aliases = Qnil;
|
50
|
+
sd->context = Qnil;
|
51
|
+
sd->scope = Qnil;
|
45
52
|
|
46
53
|
sd->attributes_writer = create_empty_attributes_writer();
|
47
54
|
|
@@ -83,11 +90,16 @@ VALUE sd_serializer_ref(VALUE self) {
|
|
83
90
|
return sd->serializer;
|
84
91
|
}
|
85
92
|
|
86
|
-
void sd_apply_serializer_config(
|
93
|
+
void sd_apply_serializer_config(SerializationDescriptor descriptor, VALUE serializer, VALUE object) {
|
87
94
|
rb_ivar_set(serializer, object_id, object);
|
88
|
-
if (context != Qnil && context != Qundef) {
|
89
|
-
rb_ivar_set(serializer, context_id, context);
|
95
|
+
if (descriptor->context != Qnil && descriptor->context != Qundef) {
|
96
|
+
rb_ivar_set(serializer, context_id, descriptor->context);
|
90
97
|
}
|
98
|
+
|
99
|
+
if (descriptor->scope != Qnil && descriptor->scope != Qundef) {
|
100
|
+
rb_ivar_set(serializer, scope_id, descriptor->scope);
|
101
|
+
}
|
102
|
+
|
91
103
|
}
|
92
104
|
VALUE sd_attributes_set(VALUE self, VALUE attributes) {
|
93
105
|
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
@@ -162,9 +174,22 @@ VALUE public_sd_build_serializer(VALUE self) {
|
|
162
174
|
return sd_build_serializer(sd);
|
163
175
|
}
|
164
176
|
|
177
|
+
VALUE sd_context_set(VALUE self, VALUE context) {
|
178
|
+
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
179
|
+
sd->context = context;
|
180
|
+
return Qnil;
|
181
|
+
}
|
182
|
+
|
183
|
+
VALUE sd_scope_set(VALUE self, VALUE scope) {
|
184
|
+
SerializationDescriptor sd = (SerializationDescriptor)DATA_PTR(self);
|
185
|
+
sd->scope = scope;
|
186
|
+
return Qnil;
|
187
|
+
}
|
188
|
+
|
165
189
|
void panko_init_serialization_descriptor(VALUE mPanko) {
|
166
190
|
object_id = rb_intern("@object");
|
167
191
|
context_id = rb_intern("@context");
|
192
|
+
scope_id = rb_intern("@scope");
|
168
193
|
|
169
194
|
cSerializationDescriptor =
|
170
195
|
rb_define_class_under(mPanko, "SerializationDescriptor", rb_cObject);
|
@@ -202,6 +227,8 @@ void panko_init_serialization_descriptor(VALUE mPanko) {
|
|
202
227
|
rb_define_method(cSerializationDescriptor, "aliases=", sd_aliases_set, 1);
|
203
228
|
rb_define_method(cSerializationDescriptor, "aliases", sd_aliases_aref, 0);
|
204
229
|
|
230
|
+
rb_define_method(cSerializationDescriptor, "scope=", sd_scope_set, 1);
|
231
|
+
rb_define_method(cSerializationDescriptor, "context=", sd_context_set, 1);
|
205
232
|
rb_define_method(cSerializationDescriptor, "build_serializer",
|
206
233
|
public_sd_build_serializer, 0);
|
207
234
|
}
|
@@ -18,6 +18,8 @@ typedef struct _SerializationDescriptor {
|
|
18
18
|
VALUE has_one_associations;
|
19
19
|
VALUE has_many_associations;
|
20
20
|
|
21
|
+
VALUE context;
|
22
|
+
VALUE scope;
|
21
23
|
AttributesWriter attributes_writer;
|
22
24
|
} * SerializationDescriptor;
|
23
25
|
|
@@ -26,7 +28,7 @@ SerializationDescriptor sd_read(VALUE descriptor);
|
|
26
28
|
void sd_mark(SerializationDescriptor data);
|
27
29
|
|
28
30
|
VALUE sd_build_serializer(SerializationDescriptor descriptor);
|
29
|
-
void sd_apply_serializer_config(
|
31
|
+
void sd_apply_serializer_config(SerializationDescriptor descriptor, VALUE serializer, VALUE object);
|
30
32
|
void sd_set_writer(SerializationDescriptor sd, VALUE subject);
|
31
33
|
|
32
34
|
void panko_init_serialization_descriptor(VALUE mPanko);
|
@@ -8,10 +8,19 @@ module Panko
|
|
8
8
|
@subjects = subjects
|
9
9
|
@each_serializer = options[:each_serializer]
|
10
10
|
|
11
|
+
if @each_serializer.nil?
|
12
|
+
raise ArgumentError, %{
|
13
|
+
Please pass valid each_serializer to ArraySerializer, for example:
|
14
|
+
> Panko::ArraySerializer.new(posts, each_serializer: PostSerializer)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
11
18
|
|
12
19
|
serializer_options = {
|
13
20
|
only: options.fetch(:only, []),
|
14
|
-
except: options.fetch(:except, [])
|
21
|
+
except: options.fetch(:except, []),
|
22
|
+
context: options[:context],
|
23
|
+
scope: options[:scope]
|
15
24
|
}
|
16
25
|
|
17
26
|
@descriptor = Panko::SerializationDescriptor.build(@each_serializer, serializer_options)
|
@@ -32,7 +41,7 @@ module Panko
|
|
32
41
|
|
33
42
|
def serialize_to_json(subjects)
|
34
43
|
writer = Oj::StringWriter.new(mode: :rails)
|
35
|
-
Panko::serialize_subjects(subjects.to_a, writer, @descriptor
|
44
|
+
Panko::serialize_subjects(subjects.to_a, writer, @descriptor)
|
36
45
|
writer.to_s
|
37
46
|
end
|
38
47
|
end
|
@@ -9,9 +9,13 @@ module Panko
|
|
9
9
|
backend = Panko::SerializationDescriptor.duplicate(serializer._descriptor)
|
10
10
|
|
11
11
|
if serializer.respond_to? :filters_for
|
12
|
-
options.merge! serializer.filters_for(options[:context])
|
12
|
+
options.merge! serializer.filters_for(options[:context], options[:scope])
|
13
13
|
end
|
14
|
+
|
15
|
+
backend.context = options[:context]
|
16
|
+
backend.scope = options[:scope]
|
14
17
|
backend.apply_filters(options)
|
18
|
+
|
15
19
|
backend
|
16
20
|
end
|
17
21
|
|
@@ -35,6 +39,9 @@ module Panko
|
|
35
39
|
backend.has_many_associations = descriptor.has_many_associations.dup
|
36
40
|
backend.has_one_associations = descriptor.has_one_associations.dup
|
37
41
|
|
42
|
+
backend.context = nil
|
43
|
+
backend.scope = nil
|
44
|
+
|
38
45
|
backend
|
39
46
|
end
|
40
47
|
|
data/lib/panko/serializer.rb
CHANGED
@@ -6,16 +6,22 @@ module Panko
|
|
6
6
|
class Serializer
|
7
7
|
class << self
|
8
8
|
def inherited(base)
|
9
|
-
base._descriptor = Panko::SerializationDescriptor.new
|
10
|
-
base._descriptor.type = base
|
11
9
|
|
12
|
-
|
13
|
-
|
10
|
+
unless _descriptor.nil?
|
11
|
+
base._descriptor = Panko::SerializationDescriptor.duplicate(_descriptor)
|
12
|
+
else
|
13
|
+
base._descriptor = Panko::SerializationDescriptor.new
|
14
|
+
|
15
|
+
base._descriptor.attributes = []
|
16
|
+
base._descriptor.aliases = {}
|
17
|
+
|
18
|
+
base._descriptor.method_fields = []
|
14
19
|
|
15
|
-
|
20
|
+
base._descriptor.has_many_associations = []
|
21
|
+
base._descriptor.has_one_associations = []
|
22
|
+
end
|
23
|
+
base._descriptor.type = base
|
16
24
|
|
17
|
-
base._descriptor.has_many_associations = []
|
18
|
-
base._descriptor.has_one_associations = []
|
19
25
|
end
|
20
26
|
|
21
27
|
attr_accessor :_descriptor
|
@@ -72,9 +78,10 @@ module Panko
|
|
72
78
|
def initialize(options = {})
|
73
79
|
@descriptor = Panko::SerializationDescriptor.build(self.class, options)
|
74
80
|
@context = options[:context]
|
81
|
+
@scope = options[:scope]
|
75
82
|
end
|
76
83
|
|
77
|
-
attr_reader :object, :context
|
84
|
+
attr_reader :object, :context, :scope
|
78
85
|
|
79
86
|
def serialize(object)
|
80
87
|
Oj.load(serialize_to_json(object))
|
@@ -82,13 +89,14 @@ module Panko
|
|
82
89
|
|
83
90
|
def serialize_to_json(object)
|
84
91
|
writer = Oj::StringWriter.new(mode: :rails)
|
85
|
-
Panko::serialize_subject(object, writer, @descriptor
|
92
|
+
Panko::serialize_subject(object, writer, @descriptor)
|
86
93
|
writer.to_s
|
87
94
|
end
|
88
95
|
|
89
96
|
def reset
|
90
97
|
@object = nil
|
91
98
|
@context = nil
|
99
|
+
@scope = nil
|
92
100
|
|
93
101
|
self
|
94
102
|
end
|
data/lib/panko/version.rb
CHANGED
data/panko_serializer.gemspec
CHANGED
@@ -22,9 +22,9 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.extensions << "ext/panko_serializer/extconf.rb"
|
23
23
|
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.14"
|
25
|
-
spec.add_development_dependency "rake", "~> 10.
|
25
|
+
spec.add_development_dependency "rake", "~> 10.5"
|
26
26
|
spec.add_development_dependency "rspec", "~> 3.0"
|
27
27
|
spec.add_development_dependency "rake-compiler"
|
28
28
|
|
29
|
-
spec.add_dependency "oj", "~> 3.
|
29
|
+
spec.add_dependency "oj", "~> 3.6.0"
|
30
30
|
end
|
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.4.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-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '10.
|
33
|
+
version: '10.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '10.
|
40
|
+
version: '10.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 3.
|
75
|
+
version: 3.6.0
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 3.
|
82
|
+
version: 3.6.0
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- yosy101@gmail.com
|
@@ -102,7 +102,6 @@ files:
|
|
102
102
|
- benchmarks/app.rb
|
103
103
|
- benchmarks/benchmarking_support.rb
|
104
104
|
- benchmarks/bm_ams_0_10.rb
|
105
|
-
- benchmarks/bm_fastjsonapi.rb
|
106
105
|
- benchmarks/bm_panko_json.rb
|
107
106
|
- benchmarks/bm_panko_object.rb
|
108
107
|
- benchmarks/bm_plain_object.rb
|
@@ -172,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
171
|
version: '0'
|
173
172
|
requirements: []
|
174
173
|
rubyforge_project:
|
175
|
-
rubygems_version: 2.7.
|
174
|
+
rubygems_version: 2.7.6
|
176
175
|
signing_key:
|
177
176
|
specification_version: 4
|
178
177
|
summary: Fast serialization for ActiveModel
|
@@ -1,56 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
exit unless ENV.fetch("RAILS_VERSION", "4.2").start_with? "5"
|
3
|
-
|
4
|
-
require_relative "./benchmarking_support"
|
5
|
-
require_relative "./app"
|
6
|
-
require_relative "./setup"
|
7
|
-
require 'fast_jsonapi'
|
8
|
-
|
9
|
-
class AuthorSerializer
|
10
|
-
include FastJsonapi::ObjectSerializer
|
11
|
-
attributes :id, :name
|
12
|
-
end
|
13
|
-
|
14
|
-
|
15
|
-
class PostFastSerializer
|
16
|
-
include FastJsonapi::ObjectSerializer
|
17
|
-
attributes :id, :body, :title, :author_id, :created_at
|
18
|
-
end
|
19
|
-
|
20
|
-
class PostWithHasOneFastSerializer
|
21
|
-
include FastJsonapi::ObjectSerializer
|
22
|
-
attributes :id, :body, :title, :author_id
|
23
|
-
|
24
|
-
has_one :author
|
25
|
-
end
|
26
|
-
|
27
|
-
class AuthorWithHasManyFastSerializer
|
28
|
-
include FastJsonapi::ObjectSerializer
|
29
|
-
attributes :id, :name
|
30
|
-
|
31
|
-
has_many :posts, serializer: :post_fast
|
32
|
-
end
|
33
|
-
|
34
|
-
|
35
|
-
def benchmark(prefix, serializer, options = {})
|
36
|
-
data = Benchmark.data
|
37
|
-
posts = data[:all]
|
38
|
-
posts_50 = data[:small]
|
39
|
-
|
40
|
-
merged_options = options.merge(each_serializer: serializer)
|
41
|
-
|
42
|
-
Benchmark.run("FastJSONAPI_#{prefix}_Posts_#{posts.count}") do
|
43
|
-
serializer.new(posts, merged_options).serialized_json
|
44
|
-
end
|
45
|
-
|
46
|
-
data = Benchmark.data
|
47
|
-
posts = data[:all]
|
48
|
-
posts_50 = data[:small]
|
49
|
-
|
50
|
-
Benchmark.run("FastJSONAPI_#{prefix}_Posts_50") do
|
51
|
-
serializer.new(posts_50, merged_options).serialized_json
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
benchmark "Simple", PostFastSerializer
|
56
|
-
benchmark "HasOne", PostWithHasOneFastSerializer, include: [:author]
|