Skip to content

Commit 3532a61

Browse files
Make safe output a bit less likely to truncate data.
1 parent 54601c1 commit 3532a61

1 file changed

Lines changed: 34 additions & 42 deletions

File tree

lib/console/format/safe.rb

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Safe
1717
# @parameter format [JSON] The format to use for serialization.
1818
# @parameter limit [Integer] The maximum depth to recurse into objects.
1919
# @parameter encoding [Encoding] The encoding to use for strings.
20-
def initialize(format: ::JSON, limit: 8, encoding: ::Encoding::UTF_8)
20+
def initialize(format: ::JSON, limit: 12, encoding: ::Encoding::UTF_8)
2121
@format = format
2222
@limit = limit
2323
@encoding = encoding
@@ -96,21 +96,6 @@ def safe_dump(object, error)
9696
return object
9797
end
9898

99-
# Replace the given object with a safe truncated representation.
100-
#
101-
# @parameter object [Object] The object to replace.
102-
# @returns [String] The replacement string.
103-
def replacement_for(object)
104-
case object
105-
when Array
106-
"[...]"
107-
when Hash
108-
"{...}"
109-
else
110-
"..."
111-
end
112-
end
113-
11499
# Create a new hash with identity comparison.
115100
def default_objects
116101
Hash.new.compare_by_identity
@@ -123,40 +108,47 @@ def default_objects
123108
# @parameter objects [Hash] The objects that have already been visited.
124109
# @returns [Object] The dumped object as a primitive representation.
125110
def safe_dump_recurse(object, limit = @limit, objects = default_objects)
126-
if limit <= 0 || objects[object]
127-
return replacement_for(object)
128-
end
129-
130111
case object
131112
when Hash
132-
objects[object] = true
133-
134-
object.to_h do |key, value|
135-
[
136-
String(key).encode(@encoding, invalid: :replace, undef: :replace),
137-
safe_dump_recurse(value, limit - 1, objects)
138-
]
113+
if limit <= 0 || objects[object]
114+
return "{...}"
115+
else
116+
objects[object] = true
117+
118+
return object.to_h do |key, value|
119+
[
120+
String(key).encode(@encoding, invalid: :replace, undef: :replace),
121+
safe_dump_recurse(value, limit - 1, objects)
122+
]
123+
end
139124
end
140125
when Array
141-
objects[object] = true
142-
143-
object.map do |value|
144-
safe_dump_recurse(value, limit - 1, objects)
126+
if limit <= 0 || objects[object]
127+
return "[...]"
128+
else
129+
objects[object] = true
130+
131+
return object.map do |value|
132+
safe_dump_recurse(value, limit - 1, objects)
133+
end
145134
end
146135
when String
147-
object.encode(@encoding, invalid: :replace, undef: :replace)
136+
return object.encode(@encoding, invalid: :replace, undef: :replace)
148137
when Numeric, TrueClass, FalseClass, NilClass
149-
object
138+
return object
150139
else
151-
objects[object] = true
152-
153-
# We could do something like this but the chance `as_json` will blow up.
154-
# We'd need to be extremely careful about it.
155-
# if object.respond_to?(:as_json)
156-
# safe_dump_recurse(object.as_json, limit - 1, objects)
157-
# else
158-
159-
safe_dump_recurse(object.to_s, limit - 1, objects)
140+
if limit <= 0 || objects[object]
141+
return "..."
142+
else
143+
objects[object] = true
144+
145+
# We could do something like this but the chance `as_json` will blow up.
146+
# We'd need to be extremely careful about it.
147+
# if object.respond_to?(:as_json)
148+
# safe_dump_recurse(object.as_json, limit - 1, objects)
149+
# else
150+
return safe_dump_recurse(object.to_s, limit - 1, objects)
151+
end
160152
end
161153
end
162154
end

0 commit comments

Comments
 (0)