Skip to content

Commit 7b11136

Browse files
committed
refactor: Simplify schema generation by considering only simple types
1 parent 9607b2a commit 7b11136

File tree

1 file changed

+10
-57
lines changed

1 file changed

+10
-57
lines changed

lib/appmap/event.rb

+10-57
Original file line numberDiff line numberDiff line change
@@ -60,66 +60,19 @@ def display_string(value)
6060
final ? value_string : encode_display_string(value_string)
6161
end
6262

63-
def add_schema(param, value, always: false)
64-
param[:size] = value.size if value.respond_to?(:size) && value.is_a?(Enumerable)
65-
66-
return unless always || AppMap.parameter_schema?
67-
68-
if value.blank? || value.is_a?(String)
69-
# pass
70-
elsif value.is_a?(Enumerable)
71-
if value.is_a?(Hash)
72-
param[:properties] = object_properties(value)
63+
def add_schema(param, value)
64+
begin
65+
if value.respond_to?(:keys)
66+
param[:properties] = value.keys.map { |key| { name: key, class: best_class_name(value[key]) } }
7367
elsif value.respond_to?(:first) && value.first
74-
param[:properties] = object_properties(value.first)
75-
end
76-
else
77-
json_value = try_as_json(value) || try_to_json(value)
78-
if value != json_value
79-
add_schema param, json_value, always: always
68+
if value.first != value
69+
add_schema param, value.first
70+
end
8071
end
81-
end
82-
end
83-
84-
def try_as_json(value)
85-
value.respond_to?(:as_json) && value.as_json
86-
end
87-
private_instance_methods :try_as_json
88-
89-
def try_to_json(value)
90-
value.respond_to?(:to_json) && JSON.parse(value.to_json)
91-
end
92-
private_instance_methods :try_to_json
93-
94-
def object_properties(hash)
95-
hash = hash.attributes if hash.respond_to?(:attributes)
96-
97-
hash = try_to_h(hash)
98-
99-
return unless hash.respond_to?(:each_with_object)
100-
101-
hash.map { |k, v| { name: k, class: v.class.name } }
102-
rescue
103-
warn $!
104-
end
105-
106-
def try_to_h(value)
107-
return value unless value.respond_to?(:to_h)
108-
109-
# Includes such bad actors as Psych::Nodes::Scalar.
110-
# Also don't try and hashifiy list-ish things.
111-
@unhashifiable_classes ||= Set.new([ Array, Set ])
112-
113-
return value if @unhashifiable_classes.include?(value.class)
114-
115-
begin
116-
value.to_h
11772
rescue
118-
# warn "#{value.class}#to_h failed: #{$!.message}"
119-
@unhashifiable_classes << value.class
73+
warn "Error in add_schema(#{value.class})", $!
12074
end
12175
end
122-
private_instance_methods :try_to_h
12376

12477
# Heuristic for dynamically defined class whose name can be nil
12578
def best_class_name(value)
@@ -271,7 +224,7 @@ def build_from_invocation(defined_class, method, receiver, arguments, event: Met
271224
value: display_string(value),
272225
kind: param_type
273226
}.tap do |param|
274-
add_schema param, value
227+
param[:size] = value.size if value.respond_to?(:size) && value.is_a?(Enumerable)
275228
end
276229
end
277230
event.receiver = {
@@ -337,7 +290,7 @@ def build_from_invocation(parent_id, return_value, exception, elapsed: nil, even
337290
object_id: return_value.__id__
338291
}.tap do |param|
339292
param[:size] = return_value.size if return_value.respond_to?(:size) && return_value.is_a?(Enumerable)
340-
add_schema param, return_value, always: parameter_schema
293+
add_schema param, return_value if parameter_schema && !exception
341294
end
342295
end
343296
if exception

0 commit comments

Comments
 (0)