Skip to content

Commit

Permalink
handle-hex-better
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiazza committed Nov 21, 2024
1 parent 6465249 commit 0e3bf33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ def determine_sql_type_for_key_as_id(): # noqa: F811
@staticmethod
def array_allowed():
return False

def generate_value(self, stix_type, value):
sql_type = stix_type.determine_sql_type(self)
if sql_type == self.determine_sql_type_for_string_property():
return value
elif sql_type == self.determine_sql_type_for_hex_property():
return bytes.fromhex(value)
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ def schema_for_core():

@staticmethod
def determine_sql_type_for_binary_property(): # noqa: F811
return Text
return PostgresBackend.determine_sql_type_for_string_property()

@staticmethod
def determine_sql_type_for_hex_property(): # noqa: F811
return LargeBinary
# return LargeBinary
return PostgresBackend.determine_sql_type_for_string_property()

@staticmethod
def determine_sql_type_for_timestamp_property(): # noqa: F811
Expand Down
7 changes: 3 additions & 4 deletions stix2/datastore/relational_db/input_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,8 @@ def generate_insert_information(self, name, stix_object, **kwargs): # noqa: F81


@add_method(HexProperty)
def generate_insert_information(self, name, stix_object, **kwargs): # noqa: F811
v = bytes.fromhex(stix_object[name])
return {name: v}
def generate_insert_information(self, name, stix_object, data_sink, **kwargs): # noqa: F811
return {name: data_sink.db_backend.generate_value(self, stix_object[name])}


def generate_insert_for_hashes(
Expand Down Expand Up @@ -249,7 +248,7 @@ def generate_insert_information( # noqa: F811
else:
if db_backend.array_allowed():
if isinstance(self.contained, HexProperty):
return {name: [bytes.fromhex(x) for x in stix_object[name]]}
return {name: [data_sink.db_backend.generate_value(self.contained, x) for x in stix_object[name]]}
else:
return {name: stix_object[name]}

Expand Down

0 comments on commit 0e3bf33

Please sign in to comment.