panko_serializer 0.8.5 → 1.0.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/README.md +6 -4
- data/lib/panko/array_serializer.rb +55 -16
- data/lib/panko/code_gen/active_record/access_classifier.rb +65 -0
- data/lib/panko/code_gen/active_record/define_attribute_methods.rb +13 -0
- data/lib/panko/code_gen/code_builder.rb +32 -0
- data/lib/panko/code_gen/compile_cache.rb +28 -0
- data/lib/panko/code_gen/compiler.rb +56 -0
- data/lib/panko/code_gen/config.rb +79 -0
- data/lib/panko/code_gen/datetime_format.rb +38 -0
- data/lib/panko/code_gen/descriptor.rb +136 -0
- data/lib/panko/code_gen/descriptor_builder.rb +166 -0
- data/lib/panko/code_gen/dump.rb +52 -0
- data/lib/panko/code_gen/errors.rb +21 -0
- data/lib/panko/code_gen/filter.rb +29 -0
- data/lib/panko/code_gen/filter_adapter.rb +44 -0
- data/lib/panko/code_gen/filters/indexed.rb +62 -0
- data/lib/panko/code_gen/filters/none.rb +19 -0
- data/lib/panko/code_gen/generator.rb +23 -0
- data/lib/panko/code_gen/generators/banner.rb +27 -0
- data/lib/panko/code_gen/generators/class_emitter.rb +120 -0
- data/lib/panko/code_gen/generators/cycle_membership.rb +53 -0
- data/lib/panko/code_gen/generators/descriptor_walk.rb +22 -0
- data/lib/panko/code_gen/generators/fanout.rb +60 -0
- data/lib/panko/code_gen/generators/field_index.rb +31 -0
- data/lib/panko/code_gen/generators/field_walk.rb +38 -0
- data/lib/panko/code_gen/generators/generated_names.rb +73 -0
- data/lib/panko/code_gen/generators/hash_sink.rb +217 -0
- data/lib/panko/code_gen/generators/json_sink.rb +290 -0
- data/lib/panko/code_gen/generators/record_access/generic.rb +92 -0
- data/lib/panko/code_gen/generators/record_access/specialized.rb +90 -0
- data/lib/panko/code_gen/generators/release.rb +60 -0
- data/lib/panko/code_gen/generators/sink.rb +80 -0
- data/lib/panko/code_gen/instance_pool.rb +27 -0
- data/lib/panko/code_gen/runtime.rb +27 -0
- data/lib/panko/code_gen/serializer_cache.rb +242 -0
- data/lib/panko/code_gen/validators/callable_arity.rb +42 -0
- data/lib/panko/code_gen/validators/name_uniqueness.rb +44 -0
- data/lib/panko/code_gen/validators/source_resolution.rb +48 -0
- data/lib/panko/code_gen/validators/validator.rb +19 -0
- data/lib/panko/code_gen/writers_pool.rb +51 -0
- data/lib/panko/code_gen.rb +82 -0
- data/lib/panko/compile_all.rb +80 -0
- data/lib/panko/compile_all_result.rb +21 -0
- data/lib/panko/config.rb +71 -0
- data/lib/panko/descriptor.rb +93 -0
- data/lib/panko/response.rb +40 -8
- data/lib/panko/serializer.rb +175 -91
- data/lib/panko/serializer_resolver.rb +2 -8
- data/lib/panko/version.rb +1 -1
- data/lib/panko_serializer.rb +3 -5
- metadata +49 -35
- data/ext/panko_serializer/attributes_writer/active_record.c +0 -181
- data/ext/panko_serializer/attributes_writer/active_record.h +0 -17
- data/ext/panko_serializer/attributes_writer/attributes_writer.c +0 -55
- data/ext/panko_serializer/attributes_writer/attributes_writer.h +0 -35
- data/ext/panko_serializer/attributes_writer/common.c +0 -10
- data/ext/panko_serializer/attributes_writer/common.h +0 -9
- data/ext/panko_serializer/attributes_writer/hash.c +0 -13
- data/ext/panko_serializer/attributes_writer/hash.h +0 -7
- data/ext/panko_serializer/attributes_writer/plain.c +0 -13
- data/ext/panko_serializer/attributes_writer/plain.h +0 -7
- data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.c +0 -156
- data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.h +0 -10
- data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +0 -409
- data/ext/panko_serializer/attributes_writer/type_cast/type_cast.h +0 -81
- data/ext/panko_serializer/common.h +0 -9
- data/ext/panko_serializer/extconf.rb +0 -28
- data/ext/panko_serializer/panko_serializer.c +0 -181
- data/ext/panko_serializer/panko_serializer.h +0 -12
- data/ext/panko_serializer/serialization_descriptor/association.c +0 -90
- data/ext/panko_serializer/serialization_descriptor/association.h +0 -20
- data/ext/panko_serializer/serialization_descriptor/attribute.c +0 -96
- data/ext/panko_serializer/serialization_descriptor/attribute.h +0 -27
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.c +0 -177
- data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +0 -30
- data/lib/panko/association.rb +0 -17
- data/lib/panko/attribute.rb +0 -29
- data/lib/panko/object_writer.rb +0 -68
- data/lib/panko/serialization_descriptor.rb +0 -180
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Panko
|
|
4
|
-
class SerializationDescriptor
|
|
5
|
-
#
|
|
6
|
-
# Creates new description and apply the options
|
|
7
|
-
# on the new descriptor
|
|
8
|
-
#
|
|
9
|
-
def self.build(serializer, options = {}, serialization_context = nil)
|
|
10
|
-
backend = Panko::SerializationDescriptor.duplicate(serializer._descriptor)
|
|
11
|
-
|
|
12
|
-
options.merge! serializer.filters_for(options[:context], options[:scope]) if serializer.respond_to? :filters_for
|
|
13
|
-
|
|
14
|
-
backend.apply_filters(options)
|
|
15
|
-
|
|
16
|
-
backend.set_serialization_context(serialization_context)
|
|
17
|
-
|
|
18
|
-
backend
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
#
|
|
22
|
-
# Create new descriptor with same properties
|
|
23
|
-
# useful when you want to apply filters
|
|
24
|
-
#
|
|
25
|
-
def self.duplicate(descriptor)
|
|
26
|
-
backend = Panko::SerializationDescriptor.new
|
|
27
|
-
|
|
28
|
-
backend.type = descriptor.type
|
|
29
|
-
|
|
30
|
-
backend.attributes = descriptor.attributes.dup
|
|
31
|
-
|
|
32
|
-
backend.method_fields = descriptor.method_fields.dup
|
|
33
|
-
backend.serializer = descriptor.type.new(_skip_init: true) unless backend.method_fields.empty?
|
|
34
|
-
|
|
35
|
-
backend.has_many_associations = descriptor.has_many_associations.map(&:duplicate)
|
|
36
|
-
backend.has_one_associations = descriptor.has_one_associations.map(&:duplicate)
|
|
37
|
-
|
|
38
|
-
backend
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def set_serialization_context(context)
|
|
42
|
-
serializer.serialization_context = context if !method_fields.empty? && !serializer.nil?
|
|
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
|
-
|
|
53
|
-
#
|
|
54
|
-
# Applies attributes and association filters
|
|
55
|
-
#
|
|
56
|
-
def apply_filters(options)
|
|
57
|
-
return unless options.key?(:only) || options.key?(:except)
|
|
58
|
-
|
|
59
|
-
attributes_only_filters, associations_only_filters = resolve_filters(options, :only)
|
|
60
|
-
attributes_except_filters, associations_except_filters = resolve_filters(options, :except)
|
|
61
|
-
|
|
62
|
-
self.attributes = apply_attribute_filters(
|
|
63
|
-
attributes,
|
|
64
|
-
attributes_only_filters,
|
|
65
|
-
attributes_except_filters
|
|
66
|
-
)
|
|
67
|
-
|
|
68
|
-
self.method_fields = apply_attribute_filters(
|
|
69
|
-
method_fields,
|
|
70
|
-
attributes_only_filters,
|
|
71
|
-
attributes_except_filters
|
|
72
|
-
)
|
|
73
|
-
|
|
74
|
-
unless has_many_associations.empty?
|
|
75
|
-
self.has_many_associations = apply_association_filters(
|
|
76
|
-
has_many_associations,
|
|
77
|
-
{attributes: attributes_only_filters, associations: associations_only_filters},
|
|
78
|
-
attributes: attributes_except_filters, associations: associations_except_filters
|
|
79
|
-
)
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
unless has_one_associations.empty?
|
|
83
|
-
self.has_one_associations = apply_association_filters(
|
|
84
|
-
has_one_associations,
|
|
85
|
-
{attributes: attributes_only_filters, associations: associations_only_filters},
|
|
86
|
-
attributes: attributes_except_filters, associations: associations_except_filters
|
|
87
|
-
)
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
def apply_association_filters(associations, only_filters, except_filters)
|
|
92
|
-
attributes_only_filters = only_filters[:attributes] || []
|
|
93
|
-
unless attributes_only_filters.empty?
|
|
94
|
-
associations.select! do |association|
|
|
95
|
-
attributes_only_filters.include?(association.name_sym)
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
attributes_except_filters = except_filters[:attributes] || []
|
|
100
|
-
unless attributes_except_filters.empty?
|
|
101
|
-
associations.reject! do |association|
|
|
102
|
-
attributes_except_filters.include?(association.name_sym)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
associations_only_filters = only_filters[:associations]
|
|
107
|
-
associations_except_filters = except_filters[:associations]
|
|
108
|
-
|
|
109
|
-
return associations if associations_only_filters.empty? && associations_except_filters.empty?
|
|
110
|
-
|
|
111
|
-
associations.map do |association|
|
|
112
|
-
name = association.name_sym
|
|
113
|
-
descriptor = association.descriptor
|
|
114
|
-
|
|
115
|
-
only_filter = associations_only_filters[name]
|
|
116
|
-
except_filter = associations_except_filters[name]
|
|
117
|
-
|
|
118
|
-
filters = {}
|
|
119
|
-
filters[:only] = only_filter unless only_filter.nil?
|
|
120
|
-
filters[:except] = except_filter unless except_filter.nil?
|
|
121
|
-
|
|
122
|
-
unless filters.empty?
|
|
123
|
-
next Panko::Association.new(
|
|
124
|
-
name,
|
|
125
|
-
association.name_str,
|
|
126
|
-
Panko::SerializationDescriptor.build(descriptor.type, filters)
|
|
127
|
-
)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
association
|
|
131
|
-
end
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
def resolve_filters(options, filter)
|
|
135
|
-
filters = options.fetch(filter, {})
|
|
136
|
-
return filters, {} if filters.is_a? Array
|
|
137
|
-
|
|
138
|
-
# hash filters looks like this
|
|
139
|
-
# { instance: [:a], foo: [:b] }
|
|
140
|
-
# which mean, for the current instance use `[:a]` as filter
|
|
141
|
-
# and for association named `foo` use `[:b]`
|
|
142
|
-
|
|
143
|
-
return [], {} if filters.empty?
|
|
144
|
-
|
|
145
|
-
attributes_filters = filters.fetch(:instance, [])
|
|
146
|
-
association_filters = filters.except(:instance)
|
|
147
|
-
|
|
148
|
-
[attributes_filters, association_filters]
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def apply_fields_filters(fields, only, except)
|
|
152
|
-
return fields & only unless only.empty?
|
|
153
|
-
return fields - except unless except.empty?
|
|
154
|
-
|
|
155
|
-
fields
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
def apply_attribute_filters(attributes, only, except)
|
|
159
|
-
unless only.empty?
|
|
160
|
-
attributes = attributes.select do |attribute|
|
|
161
|
-
name_to_check = attribute.name
|
|
162
|
-
name_to_check = attribute.alias_name unless attribute.alias_name.nil?
|
|
163
|
-
|
|
164
|
-
only.include?(name_to_check.to_sym)
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
unless except.empty?
|
|
169
|
-
attributes = attributes.reject do |attribute|
|
|
170
|
-
name_to_check = attribute.name
|
|
171
|
-
name_to_check = attribute.alias_name unless attribute.alias_name.nil?
|
|
172
|
-
|
|
173
|
-
except.include?(name_to_check.to_sym)
|
|
174
|
-
end
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
attributes
|
|
178
|
-
end
|
|
179
|
-
end
|
|
180
|
-
end
|