@@ -60,16 +60,37 @@ def display_string(value)
6060 final ? value_string : encode_display_string ( value_string )
6161 end
6262
63- def object_properties ( hash_like )
64- hash = hash_like . to_h
65- hash . keys . map do |key |
66- {
63+ def add_schema ( param , value , always : false )
64+ return unless always || AppMap . parameter_schema?
65+
66+ if value . blank? || value . is_a? ( String )
67+ # pass
68+ elsif value . is_a? ( Enumerable )
69+ if value . is_a? ( Hash )
70+ param [ :properties ] = object_properties ( value )
71+ elsif value . respond_to? ( :first ) && value . first
72+ param [ :properties ] = object_properties ( value . first )
73+ end
74+ elsif value . respond_to? ( :as_json )
75+ add_schema param , JSON . parse ( value . to_json ) , always : always
76+ end
77+ end
78+
79+ def object_properties ( hash )
80+ hash = hash . attributes if hash . respond_to? ( :attributes )
81+ hash = hash . to_h if hash . is_a? ( Struct )
82+
83+ return unless hash . respond_to? ( :each_with_object )
84+
85+ hash . each_with_object ( [ ] ) do |entry , memo |
86+ key , value = entry
87+ memo << {
6788 name : key ,
68- class : hash [ key ] . class . name ,
89+ class : value . class . name ,
6990 }
7091 end
7192 rescue
72- nil
93+ warn $!
7394 end
7495
7596 # Heuristic for dynamically defined class whose name can be nil
@@ -221,7 +242,9 @@ def build_from_invocation(defined_class, method, receiver, arguments, event: Met
221242 object_id : value . __id__ ,
222243 value : display_string ( value ) ,
223244 kind : param_type
224- }
245+ } . tap do |param |
246+ add_schema param , value
247+ end
225248 end
226249 event . receiver = {
227250 class : best_class_name ( receiver ) ,
@@ -276,15 +299,17 @@ class MethodReturn < MethodReturnIgnoreValue
276299 attr_accessor :return_value , :exceptions
277300
278301 class << self
279- def build_from_invocation ( parent_id , return_value , exception , elapsed : nil , event : MethodReturn . new )
302+ def build_from_invocation ( parent_id , return_value , exception , elapsed : nil , event : MethodReturn . new , parameter_schema : false )
280303 event ||= MethodReturn . new
281304 event . tap do |_ |
282305 if return_value
283306 event . return_value = {
284307 class : best_class_name ( return_value ) ,
285308 value : display_string ( return_value ) ,
286309 object_id : return_value . __id__
287- }
310+ } . tap do |param |
311+ add_schema param , return_value , always : parameter_schema
312+ end
288313 end
289314 if exception
290315 next_exception = exception
0 commit comments