Skip to content

Commit

Permalink
Adds support GraphML support for long (#1142)
Browse files Browse the repository at this point in the history
* Add support for long in GraphML

* Formatting changes

* Add details to Release Note
  • Loading branch information
jamesdbaker committed Mar 16, 2024
1 parent af0d36c commit b5ac124
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Adds missing support for the Long attribute type to GraphML
7 changes: 6 additions & 1 deletion src/graphml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl From<ParseBoolError> for Error {
impl From<ParseIntError> for Error {
#[inline]
fn from(e: ParseIntError) -> Error {
Error::ParseValue(format!("Failed conversion to 'int': {}", e))
Error::ParseValue(format!("Failed conversion to 'int' or 'long': {}", e))
}
}

Expand Down Expand Up @@ -121,6 +121,7 @@ enum Type {
Float,
Double,
String,
Long,
}

#[derive(Clone)]
Expand All @@ -130,6 +131,7 @@ enum Value {
Float(f32),
Double(f64),
String(String),
Long(isize),
UnDefined,
}

Expand All @@ -141,6 +143,7 @@ impl IntoPy<PyObject> for Value {
Value::Float(val) => val.into_py(py),
Value::Double(val) => val.into_py(py),
Value::String(val) => val.into_py(py),
Value::Long(val) => val.into_py(py),
Value::UnDefined => py.None(),
}
}
Expand All @@ -160,6 +163,7 @@ impl Key {
Type::Float => Value::Float(val.parse()?),
Type::Double => Value::Double(val.parse()?),
Type::String => Value::String(val),
Type::Long => Value::Long(val.parse()?),
})
}

Expand Down Expand Up @@ -419,6 +423,7 @@ impl GraphML {
b"float" => Type::Float,
b"double" => Type::Double,
b"string" => Type::String,
b"long" => Type::Long,
_ => {
return Err(Error::InvalidDoc(format!(
"Invalid 'attr.type' attribute in key with id={}.",
Expand Down
33 changes: 32 additions & 1 deletion tests/test_graphml.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,37 @@ def test_string(self):
edges = []
self.assertGraphEqual(graph, nodes, edges, directed=True)

def test_long(self):
graph_xml = self.HEADER.format(
"""
<key id="d0" for="node" attr.name="test" attr.type="long">
<default>42</default>
</key>
<graph id="G" edgedefault="directed">
<node id="n0">
<data key="d0">8</data>
</node>
<node id="n1"/>
<node id="n2">
<data key="d0">42</data>
</node>
</graph>
"""
)

with tempfile.NamedTemporaryFile("wt") as fd:
fd.write(graph_xml)
fd.flush()
graphml = rustworkx.read_graphml(fd.name)
graph = graphml[0]
nodes = [
{"id": "n0", "test": 8},
{"id": "n1", "test": 42},
{"id": "n2", "test": 42},
]
edges = []
self.assertGraphEqual(graph, nodes, edges, directed=True)

def test_convert_error(self):
graph_xml = self.HEADER.format(
"""
Expand All @@ -390,7 +421,7 @@ def test_convert_error(self):
"""
)

for type in ["boolean", "int", "float", "double"]:
for type in ["boolean", "int", "float", "double", "long"]:
self.assertGraphMLRaises(graph_xml=graph_xml.format(type))

def test_invalid_xml(self):
Expand Down

0 comments on commit b5ac124

Please sign in to comment.