panko_serializer 1.0.1 → 1.1.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/lib/panko/code_gen/generators/release.rb +41 -1
- data/lib/panko/code_gen/generators/written_ivars.rb +66 -0
- data/lib/panko/code_gen.rb +1 -0
- data/lib/panko/config.rb +35 -0
- data/lib/panko/serializer.rb +7 -7
- data/lib/panko/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2afd5697c1c27ce3b3fd6fcc295bb3cdf06b20813cda91582a63b2f6e43f9232
|
|
4
|
+
data.tar.gz: '0638cb2574637cdf1ef53f81b9dd11d8738fef4db57d08764418f0cbf04ec25f'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 86a70f39c27fe74de9e74b94771f41090d7229c0881d77a45a4b2da85125db3213c1ecfbb643af42a1592f10749c8f3bc345c9c67e25fcc1e77b3cbc6192ba18
|
|
7
|
+
data.tar.gz: 0a20cb918b55ef0346d0f12e29dd84b45a91fd108510ff05d0ab3ce8943dd7613562cef02ba96aba80cf475ecfc71899c4945b9c3d1bfcfd5a7dd85cf0850db5
|
|
@@ -32,6 +32,44 @@ module Panko::CodeGen
|
|
|
32
32
|
[[GeneratedNames.serializer_ivar(position), assoc.descriptor], *variants]
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# A serializer method's memo lasts one serialize call, as it did when each call built a new
|
|
36
|
+
# serializer. An unset ivar reads as nil, so nil clears it unless a method checks it with
|
|
37
|
+
# +defined?+; those are removed. Nil keeps the instance's shape: removing an ivar and
|
|
38
|
+
# setting it again on the next call costs a shape change each time.
|
|
39
|
+
def emit_serializer_ivar_clears(descriptor, builder)
|
|
40
|
+
written = WrittenIvars.of(descriptor.parent_class)
|
|
41
|
+
return emit_ivar_sweep(descriptor, builder) if written.nil?
|
|
42
|
+
generated = generated_ivars(descriptor)
|
|
43
|
+
written.each do |name, checked|
|
|
44
|
+
next if generated.include?(name)
|
|
45
|
+
builder.line(checked ? "remove_instance_variable(#{name.inspect}) if defined?(#{name})" : "#{name} = nil")
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def emit_ivar_sweep(descriptor, builder)
|
|
50
|
+
builder.line "instance_variables.each do |name|"
|
|
51
|
+
builder.indent do
|
|
52
|
+
builder.line "case name"
|
|
53
|
+
builder.line "when #{generated_ivars(descriptor).map(&:inspect).join(", ")}"
|
|
54
|
+
builder.line "else remove_instance_variable(name)"
|
|
55
|
+
builder.line "end"
|
|
56
|
+
end
|
|
57
|
+
builder.line "end"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Must match the ivars +ClassEmitter#emit_initialize+ and the record access code set.
|
|
61
|
+
def generated_ivars(descriptor)
|
|
62
|
+
names = %i[@object @context @scope]
|
|
63
|
+
descriptor.method_attributes.each_with_index do |method_attribute, index|
|
|
64
|
+
names << GeneratedNames.callable_ivar(index).to_sym unless method_attribute.body.is_a?(Symbol)
|
|
65
|
+
end
|
|
66
|
+
descriptor.associations.each_with_index do |assoc, position|
|
|
67
|
+
names << GeneratedNames.if_guard_ivar(position).to_sym if assoc.if
|
|
68
|
+
child_ivars(assoc, position).each { |ivar, _child| names << ivar.to_sym }
|
|
69
|
+
end
|
|
70
|
+
names
|
|
71
|
+
end
|
|
72
|
+
|
|
35
73
|
# Every class defines +_release+, even an empty one: the serializers call it
|
|
36
74
|
# on every checkin without checking.
|
|
37
75
|
def emit(descriptor, builder, cyclic_ids)
|
|
@@ -41,11 +79,13 @@ module Panko::CodeGen
|
|
|
41
79
|
builder.line "@object = nil"
|
|
42
80
|
builder.line "@context = nil"
|
|
43
81
|
builder.line "@scope = nil"
|
|
82
|
+
emit_serializer_ivar_clears(descriptor, builder)
|
|
44
83
|
end
|
|
45
84
|
descriptor.associations.each_with_index do |assoc, position|
|
|
46
85
|
child_ivars(assoc, position).each do |ivar, child|
|
|
47
86
|
next if child.equal?(descriptor)
|
|
48
|
-
# Ends the chain without per-call visited state; such serializers keep their last record
|
|
87
|
+
# Ends the chain without per-call visited state; such serializers keep their last record and memos
|
|
88
|
+
# until the next call. Only a hand-built Descriptor can form a cycle: the DSL snapshots each child.
|
|
49
89
|
next if cyclic_ids[descriptor.__id__] && cyclic_ids[child.__id__]
|
|
50
90
|
next unless subtree_releases?(child, cyclic_ids)
|
|
51
91
|
builder.line "#{ivar}._release"
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Panko::CodeGen
|
|
4
|
+
module Generators
|
|
5
|
+
# Reads the bytecode of a serializer's methods for the instance variables they
|
|
6
|
+
# set, so +_release+ clears exactly those and a serializer that sets none pays nothing.
|
|
7
|
+
module WrittenIvars
|
|
8
|
+
# Calls through which a method can set an instance variable, or check that one is set,
|
|
9
|
+
# without naming it in the bytecode.
|
|
10
|
+
REFLECTIVE_CALLS = %i[
|
|
11
|
+
instance_variable_set instance_variable_defined? instance_variables
|
|
12
|
+
instance_eval instance_exec eval binding
|
|
13
|
+
].freeze
|
|
14
|
+
|
|
15
|
+
module_function
|
|
16
|
+
|
|
17
|
+
# Each written name maps to whether a method checks it with +defined?+, or nil when
|
|
18
|
+
# a method can set or check one the scan cannot name. Names are sorted: method table order
|
|
19
|
+
# varies between runs, and the generated code must not.
|
|
20
|
+
# +initialize+ is skipped: a Generated Class defines its own, so the serializer's never runs on it.
|
|
21
|
+
def of(klass)
|
|
22
|
+
return nil unless defined?(RubyVM::InstructionSequence)
|
|
23
|
+
written = {}
|
|
24
|
+
checked = {}
|
|
25
|
+
klass.ancestors.each do |mod|
|
|
26
|
+
break if mod.equal?(Object)
|
|
27
|
+
(mod.instance_methods(false) + mod.private_instance_methods(false)).each do |name|
|
|
28
|
+
next if name == :initialize
|
|
29
|
+
return nil unless collect(mod.instance_method(name), written, checked)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
written.keys.sort.to_h { |name| [name, checked.key?(name)] }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# False when the method can set an instance variable without naming it.
|
|
36
|
+
def collect(method, written, checked)
|
|
37
|
+
iseq = RubyVM::InstructionSequence.of(method)
|
|
38
|
+
return collect_native(method, written) if iseq.nil?
|
|
39
|
+
walk(iseq, written, checked)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# +attr_writer+ and +attr_reader+ have no bytecode but a source location; a C method has neither.
|
|
43
|
+
def collect_native(method, written)
|
|
44
|
+
return false if method.source_location.nil?
|
|
45
|
+
written[:"@#{method.name.to_s.chomp("=")}"] = true if method.name.end_with?("=")
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Reads only the instruction list: the rest of +to_a+ holds locals and literals, which are data.
|
|
50
|
+
# Every call instruction carries its call data, with the method name, as the first operand.
|
|
51
|
+
def walk(iseq, written, checked)
|
|
52
|
+
iseq.to_a.last.each do |instruction|
|
|
53
|
+
next unless instruction.is_a?(Array)
|
|
54
|
+
operand = instruction[1]
|
|
55
|
+
case instruction[0]
|
|
56
|
+
when :setinstancevariable then written[operand] = true
|
|
57
|
+
when :definedivar then checked[operand] = true
|
|
58
|
+
else
|
|
59
|
+
return false if operand.is_a?(Hash) && REFLECTIVE_CALLS.include?(operand[:mid])
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
iseq.to_enum(:each_child).all? { |child| walk(child, written, checked) }
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
data/lib/panko/code_gen.rb
CHANGED
|
@@ -30,6 +30,7 @@ require_relative "code_gen/generators/record_access/specialized"
|
|
|
30
30
|
require_relative "code_gen/generators/cycle_membership"
|
|
31
31
|
require_relative "code_gen/generators/descriptor_walk"
|
|
32
32
|
require_relative "code_gen/generators/field_index"
|
|
33
|
+
require_relative "code_gen/generators/written_ivars"
|
|
33
34
|
require_relative "code_gen/generators/release"
|
|
34
35
|
require_relative "code_gen/generators/banner"
|
|
35
36
|
require_relative "code_gen/generators/class_emitter"
|
data/lib/panko/config.rb
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "active_support/inflector"
|
|
4
|
+
|
|
3
5
|
module Panko
|
|
4
6
|
# Process-global settings, read at serialize time. Set them in an
|
|
5
7
|
# initializer, before serializers compile:
|
|
@@ -41,8 +43,11 @@ module Panko
|
|
|
41
43
|
end
|
|
42
44
|
end
|
|
43
45
|
|
|
46
|
+
KEY_TRANSFORMS = [nil, :camel_lower, :camel, :dash, :underscore].freeze
|
|
47
|
+
|
|
44
48
|
@auto_specialization = AutoSpecialization.new
|
|
45
49
|
@writer_pool_max_bytes = 1_048_576
|
|
50
|
+
@key_transform = nil
|
|
46
51
|
|
|
47
52
|
class << self
|
|
48
53
|
attr_reader :auto_specialization
|
|
@@ -61,6 +66,36 @@ module Panko
|
|
|
61
66
|
end
|
|
62
67
|
@writer_pool_max_bytes = value
|
|
63
68
|
end
|
|
69
|
+
|
|
70
|
+
# Output key casing for declared attribute and association names:
|
|
71
|
+
# +:camel_lower+ (+created_at+ to +createdAt+), +:camel+ (+CreatedAt+),
|
|
72
|
+
# +:dash+ (+created-at+), +:underscore+ (+createdAt+ to +created_at+),
|
|
73
|
+
# or +nil+ to keep them as declared. Read when a serializer declares a
|
|
74
|
+
# field, so set it before serializer classes load. +aliases+ and
|
|
75
|
+
# +name:+ keys are kept as written.
|
|
76
|
+
#
|
|
77
|
+
# @return [Symbol, nil] default +nil+
|
|
78
|
+
attr_reader :key_transform
|
|
79
|
+
|
|
80
|
+
def key_transform=(value)
|
|
81
|
+
unless KEY_TRANSFORMS.include?(value)
|
|
82
|
+
raise ArgumentError,
|
|
83
|
+
"key_transform: must be :camel_lower, :camel, :dash, :underscore or nil; got #{value.inspect}:#{value.class}"
|
|
84
|
+
end
|
|
85
|
+
@key_transform = value
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# @param key [Symbol] a declared field name
|
|
89
|
+
# @return [Symbol] +key+ in the configured {key_transform} casing
|
|
90
|
+
def transform_key(key)
|
|
91
|
+
case @key_transform
|
|
92
|
+
when :camel_lower then ActiveSupport::Inflector.camelize(key.to_s, false).to_sym
|
|
93
|
+
when :camel then ActiveSupport::Inflector.camelize(key.to_s).to_sym
|
|
94
|
+
when :dash then ActiveSupport::Inflector.dasherize(ActiveSupport::Inflector.underscore(key.to_s)).to_sym
|
|
95
|
+
when :underscore then ActiveSupport::Inflector.underscore(key.to_s).to_sym
|
|
96
|
+
else key
|
|
97
|
+
end
|
|
98
|
+
end
|
|
64
99
|
end
|
|
65
100
|
end
|
|
66
101
|
|
data/lib/panko/serializer.rb
CHANGED
|
@@ -57,7 +57,7 @@ module Panko
|
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
def attributes(*attrs)
|
|
60
|
-
attrs.each { |attr| add_attribute(attr.to_sym, attr.to_sym) }
|
|
60
|
+
attrs.each { |attr| add_attribute(attr.to_sym, Panko::Config.transform_key(attr.to_sym)) }
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
def aliases(aliases = {})
|
|
@@ -69,10 +69,10 @@ module Panko
|
|
|
69
69
|
def method_added(method)
|
|
70
70
|
super
|
|
71
71
|
return if _cg_attributes.nil?
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
72
|
+
matching, self._cg_attributes = _cg_attributes.partition { |attribute| attribute.source == method }
|
|
73
|
+
matching.each do |attribute|
|
|
74
|
+
_cg_method_attributes << Panko::CodeGen::MethodAttribute.new(name: attribute.name, body: method)
|
|
75
|
+
end
|
|
76
76
|
end
|
|
77
77
|
|
|
78
78
|
# The static public view of this serializer's declared shape.
|
|
@@ -91,7 +91,7 @@ module Panko
|
|
|
91
91
|
private
|
|
92
92
|
|
|
93
93
|
def add_attribute(source, output)
|
|
94
|
-
return if _cg_attributes.any? { |attribute| attribute.source == source }
|
|
94
|
+
return if _cg_attributes.any? { |attribute| attribute.source == source && attribute.name == output }
|
|
95
95
|
_cg_attributes << Panko::CodeGen::Attribute.new(name: output, source: source)
|
|
96
96
|
end
|
|
97
97
|
|
|
@@ -101,7 +101,7 @@ module Panko
|
|
|
101
101
|
only, except = association_filters(serializer, options)
|
|
102
102
|
descriptor = Panko::CodeGen::DescriptorBuilder.narrow(descriptor, only, except)
|
|
103
103
|
_cg_associations << Panko::CodeGen::Association.new(
|
|
104
|
-
name: options.
|
|
104
|
+
name: options.key?(:name) ? options[:name].to_s.to_sym : Panko::Config.transform_key(name.to_sym),
|
|
105
105
|
kind: kind,
|
|
106
106
|
descriptor: descriptor,
|
|
107
107
|
source: name.to_sym,
|
data/lib/panko/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: panko_serializer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Yosi Attias
|
|
@@ -95,6 +95,7 @@ files:
|
|
|
95
95
|
- lib/panko/code_gen/generators/record_access/specialized.rb
|
|
96
96
|
- lib/panko/code_gen/generators/release.rb
|
|
97
97
|
- lib/panko/code_gen/generators/sink.rb
|
|
98
|
+
- lib/panko/code_gen/generators/written_ivars.rb
|
|
98
99
|
- lib/panko/code_gen/instance_pool.rb
|
|
99
100
|
- lib/panko/code_gen/runtime.rb
|
|
100
101
|
- lib/panko/code_gen/serializer_cache.rb
|