Skip to content

Commit

Permalink
CTR remove unsupported types in GraphSONv4 in gremlin-python
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazcy authored and kenhuuu committed Nov 3, 2024
1 parent ea26e79 commit 6c78d2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,32 +147,6 @@ def objectify(self, d, reader):
raise NotImplementedError()


class _BytecodeSerializer(_GraphSONTypeIO):
@classmethod
def _dictify_instructions(cls, instructions, writer):
out = []
for instruction in instructions:
inst = [instruction[0]]
inst.extend(writer.to_dict(arg) for arg in instruction[1:])
out.append(inst)
return out

@classmethod
def dictify(cls, bytecode, writer):
if isinstance(bytecode, Traversal):
bytecode = bytecode.bytecode
out = {}
if bytecode.source_instructions:
out["source"] = cls._dictify_instructions(bytecode.source_instructions, writer)
if bytecode.step_instructions:
out["step"] = cls._dictify_instructions(bytecode.step_instructions, writer)
return GraphSONUtil.typed_value("Bytecode", out)


class TraversalSerializer(_BytecodeSerializer):
python_type = Traversal


class VertexSerializer(_GraphSONTypeIO):
python_type = Vertex
graphson_type = "g:Vertex"
Expand Down Expand Up @@ -222,71 +196,6 @@ def dictify(cls, property, writer):
"value": writer.to_dict(property.value)})


class TraversalStrategySerializer(_GraphSONTypeIO):
python_type = TraversalStrategy

@classmethod
def dictify(cls, strategy, writer):
configuration = {}
for key in strategy.configuration:
configuration[key] = writer.to_dict(strategy.configuration[key])
return GraphSONUtil.typed_value(strategy.strategy_name, configuration)


class TraverserIO(_GraphSONTypeIO):
python_type = Traverser
graphson_type = "g:Traverser"

@classmethod
def dictify(cls, traverser, writer):
return GraphSONUtil.typed_value("Traverser", {"value": writer.to_dict(traverser.object),
"bulk": writer.to_dict(traverser.bulk)})

@classmethod
def objectify(cls, d, reader):
return Traverser(reader.to_object(d["value"]),
reader.to_object(d["bulk"]))


class EnumSerializer(_GraphSONTypeIO):
python_type = Enum

@classmethod
def dictify(cls, enum, _):
return GraphSONUtil.typed_value(SymbolUtil.to_camel_case(type(enum).__name__),
SymbolUtil.to_camel_case(str(enum.name)))


class PSerializer(_GraphSONTypeIO):
python_type = P

@classmethod
def dictify(cls, p, writer):
out = {"predicate": p.operator,
"value": [writer.to_dict(p.value), writer.to_dict(p.other)] if p.other is not None else
writer.to_dict(p.value)}
return GraphSONUtil.typed_value("P", out)


class TextPSerializer(_GraphSONTypeIO):
python_type = TextP

@classmethod
def dictify(cls, p, writer):
out = {"predicate": p.operator,
"value": [writer.to_dict(p.value), writer.to_dict(p.other)] if p.other is not None else
writer.to_dict(p.value)}
return GraphSONUtil.typed_value("TextP", out)


class TypeSerializer(_GraphSONTypeIO):
python_type = TypeType

@classmethod
def dictify(cls, typ, writer):
return writer.to_dict(typ())


class UUIDIO(_GraphSONTypeIO):
python_type = uuid.UUID
graphson_type = "g:UUID"
Expand Down Expand Up @@ -403,27 +312,6 @@ def objectify(cls, l, reader):
return new_dict


class BulkSetIO(_GraphSONTypeIO):
graphson_type = "g:BulkSet"

@classmethod
def objectify(cls, l, reader):
new_list = []

# this approach basically mimics what currently existed in 3.3.4 and prior versions where BulkSet is
# basically just coerced to list. the limitation here is that if the value of a bulk exceeds the size of
# a list (into the long space) then stuff won't work nice.
if len(l) > 0:
x = 0
while x < len(l):
obj = reader.to_object(l[x])
bulk = reader.to_object(l[x + 1])
for y in range(bulk):
new_list.append(obj)
x = x + 2
return new_list


class FloatIO(_NumberIO):
python_type = FloatType
graphson_type = "g:Float"
Expand Down Expand Up @@ -664,9 +552,14 @@ def objectify(cls, d, reader):
return Path(reader.to_object(d["labels"]), reader.to_object(d["objects"]))


class TDeserializer(_GraphSONTypeIO):
class TIO(_GraphSONTypeIO):
graphson_type = "g:T"
graphson_base_type = "T"
python_type = T

@classmethod
def dictify(cls, t, writer):
return GraphSONUtil.typed_value(cls.graphson_base_type, t.name, "g")
@classmethod
def objectify(cls, d, reader):
return T[d]
Expand All @@ -684,19 +577,3 @@ def dictify(cls, d, writer):
@classmethod
def objectify(cls, d, reader):
return Direction[d]


class TraversalMetricsDeserializer(_GraphSONTypeIO):
graphson_type = "g:TraversalMetrics"

@classmethod
def objectify(cls, d, reader):
return reader.to_object(d)


class MetricsDeserializer(_GraphSONTypeIO):
graphson_type = "g:Metrics"

@classmethod
def objectify(cls, d, reader):
return reader.to_object(d)
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,6 @@ def test_collections(self):
assert x['b'] == "marko"
assert len(x) == 2

# BulkSet gets coerced to a List - both have the same behavior
x = self.graphson_reader.read_object(
json.dumps({"@type": "g:BulkSet",
"@value": ["marko", {"@type": "g:Int64", "@value": 1}, "josh", {"@type": "g:Int64", "@value": 3}]}))
assert isinstance(x, list)
assert len(x) == 4
assert x.count("marko") == 1
assert x.count("josh") == 3

def test_number_input(self):
x = self.graphson_reader.read_object(json.dumps({
"@type": "g:Byte",
Expand Down Expand Up @@ -290,18 +281,6 @@ def test_uuid(self):
assert isinstance(prop, uuid.UUID)
assert str(prop) == '41d2e28a-20a4-4ab0-b379-d810dede3786'

def test_metrics(self):
prop = self.graphson_reader.read_object(
json.dumps([{'@type': 'g:TraversalMetrics', '@value': {'dur': 1.468594, 'metrics': [
{'@type': 'g:Metrics', '@value': {'dur': 1.380957, 'counts': {}, 'name': 'GraphStep(__.V())', 'annotations': {'percentDur': 94.03259171697556}, 'id': '4.0.0()'}},
{'@type': 'g:Metrics', '@value': {'dur': 0.087637, 'counts': {}, 'name': 'ReferenceElementStep', 'annotations': {'percentDur': 5.967408283024444}, 'id': '3.0.0()'}}
]}}]))
assert isinstance(prop, list)
assert prop == [{'dur': 1.468594, 'metrics': [
{'dur': 1.380957, 'counts': {}, 'name': 'GraphStep(__.V())', 'annotations': {'percentDur': 94.03259171697556}, 'id': '4.0.0()'},
{'dur': 0.087637, 'counts': {}, 'name': 'ReferenceElementStep', 'annotations': {'percentDur': 5.967408283024444}, 'id': '3.0.0()'}
]}]

def test_binary(self):
bb = self.graphson_reader.read_object(
json.dumps({"@type": "g:Binary", "@value": "c29tZSBieXRlcyBmb3IgeW91"}))
Expand Down Expand Up @@ -356,47 +335,9 @@ def test_numbers(self):
assert """true""" == self.graphson_writer.write_object(True)

def test_enum(self):
assert {"@type": "g:Merge", "@value": "onMatch"} == json.loads(self.graphson_writer.write_object(Merge.on_match))
assert {"@type": "g:Order", "@value": "shuffle"} == json.loads(self.graphson_writer.write_object(Order.shuffle))
assert {"@type": "g:Barrier", "@value": "normSack"} == json.loads(self.graphson_writer.write_object(Barrier.norm_sack))
assert {"@type": "g:Operator", "@value": "sum"} == json.loads(self.graphson_writer.write_object(Operator.sum_))
assert {"@type": "g:Operator", "@value": "sumLong"} == json.loads(self.graphson_writer.write_object(Operator.sum_long))
assert {"@type": "g:Direction", "@value": "OUT"} == json.loads(self.graphson_writer.write_object(Direction.OUT))
assert {"@type": "g:Direction", "@value": "OUT"} == json.loads(self.graphson_writer.write_object(Direction.from_))

def test_P(self):
result = {'@type': 'g:P',
'@value': {
'predicate': 'and',
'value': [{
'@type': 'g:P',
'@value': {
'predicate': 'or',
'value': [{
'@type': 'g:P',
'@value': {'predicate': 'lt', 'value': 'b'}
},
{'@type': 'g:P', '@value': {'predicate': 'gt', 'value': 'c'}}
]
}
},
{'@type': 'g:P', '@value': {'predicate': 'neq', 'value': 'd'}}]}}

assert result == json.loads(
self.graphson_writer.write_object(P.lt("b").or_(P.gt("c")).and_(P.neq("d"))))

result = {'@type': 'g:P', '@value': {'predicate': 'within', 'value': {'@type': 'g:List', '@value': [
{"@type": "g:Int32", "@value": 1}, {"@type": "g:Int32", "@value": 2}]}}}
assert result == json.loads(self.graphson_writer.write_object(P.within([1, 2])))
assert result == json.loads(self.graphson_writer.write_object(P.within({1, 2})))
assert result == json.loads(self.graphson_writer.write_object(P.within(1, 2)))

result = {'@type': 'g:P', '@value': {'predicate': 'within', 'value': {'@type': 'g:List', '@value': [
{"@type": "g:Int32", "@value": 1}]}}}
assert result == json.loads(self.graphson_writer.write_object(P.within([1])))
assert result == json.loads(self.graphson_writer.write_object(P.within({1})))
assert result == json.loads(self.graphson_writer.write_object(P.within(1)))

def test_graph(self):
assert {"@type": "g:Vertex",
"@value": {"id": {"@type": "g:Int64", "@value": 12}, "label": ["person"]}} == json.loads(
Expand Down

0 comments on commit 6c78d2b

Please sign in to comment.