Skip to content

Commit 01e9fbc

Browse files
committed
Update array and object convert to value
1 parent 24ca210 commit 01e9fbc

3 files changed

Lines changed: 28 additions & 7 deletions

File tree

openc3/lib/openc3/core_ext/string.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
require 'openc3/packets/binary_accessor'
2424
require 'openc3/ext/string' if RUBY_ENGINE == 'ruby' and !ENV['OPENC3_NO_EXT']
25+
require 'yaml'
2526

2627
# OpenC3 specific additions to the Ruby String class
2728
class String
@@ -40,6 +41,8 @@ class String
4041
HEX_CHECK_REGEX = /\A\s*0[xX][\dabcdefABCDEF]+\s*\z/
4142
# Regular expression to identify a String as an Array of numbers
4243
ARRAY_CHECK_REGEX = /\A\s*\[.*\]\s*\z/
44+
# Regular expression to identify a String containing object notation
45+
OBJECT_CHECK_REGEX = /\A\s*\{.*\}\s*\z/
4346

4447
# Displays a String containing binary data in a human readable format by
4548
# converting each byte to the hex representation.
@@ -209,6 +212,11 @@ def is_array?
209212
if ARRAY_CHECK_REGEX.match?(self) then true else false end
210213
end
211214

215+
# @return [Boolean] Whether the String represents an Object
216+
def is_object?
217+
if OBJECT_CHECK_REGEX.match?(self) then true else false end
218+
end
219+
212220
# @return [Boolean] Whether the string contains only printable characters
213221
def is_printable?
214222
if NON_PRINTABLE_REGEX.match?(self) then false else true end
@@ -238,9 +246,9 @@ def convert_to_value
238246
elsif self.is_hex?
239247
# Hex
240248
return_value = Integer(self)
241-
elsif self.is_array?
242-
# Array
243-
return_value = eval(self)
249+
elsif self.is_array? or self.is_object?
250+
# Array or Object
251+
return_value = YAML.safe_load(self)
244252
end
245253
rescue Exception
246254
# Something went wrong so just return the string as is

openc3/python/openc3/utilities/extract.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
# Regular expression to identify a String as an Array of numbers
4040
ARRAY_CHECK_REGEX = re.compile(r"\A\s*\[.*\]\s*\Z")
4141

42+
# Regular expression to identify a String as an Object
43+
OBJECT_CHECK_REGEX = re.compile(r"\A\s*\{.*\}\s*\Z")
4244

4345
# Pulls all string keyword arguments into the args array.
4446
def extract_string_kwargs_to_args(args: list, kwargs: dict):
@@ -83,6 +85,13 @@ def is_array(string):
8385
return False
8486

8587

88+
def is_object(string):
89+
"""Whether the String represents an Object"""
90+
if OBJECT_CHECK_REGEX.match(string):
91+
return True
92+
return False
93+
94+
8695
def convert_to_value(string):
8796
"""Converts the String into either a Float, Integer, or Array
8897
depending on what the String represents. It can successfully convert
@@ -101,8 +110,8 @@ def convert_to_value(string):
101110
elif is_hex(string):
102111
# Hex
103112
return_value = int(string, 0)
104-
elif is_array(string):
105-
# Array
113+
elif is_array(string) or is_object(string):
114+
# Array or Object
106115
return_value = ast.literal_eval(string)
107116
except Exception:
108117
# Something went wrong so just return the string as is

openc3/spec/core_ext/string_spec.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,12 @@
206206
expect("[0,1,2,3]".convert_to_value).to eql [0, 1, 2, 3]
207207
end
208208

209-
it "just returns the string if something goes wrong" do
210-
expect("[.a,2,3]".convert_to_value).to eql "[.a,2,3]"
209+
it "is very tolerant to array contents" do
210+
expect("[.a,2,3]".convert_to_value).to eql [".a",2,3]
211+
end
212+
213+
it "it handles objects" do
214+
expect("{'hello':'goodbye'}".convert_to_value).to eql({"hello" => "goodbye"})
211215
end
212216

213217
it "doesn't match(multiline strings" do

0 commit comments

Comments
 (0)