Skip to content

Commit

Permalink
Merge pull request #632 from Dessia-tech/fix/path-cycles
Browse files Browse the repository at this point in the history
Patch 0.14.2
  • Loading branch information
GhislainJ authored Nov 14, 2023
2 parents 02fc116 + fd8ebae commit 4c6c12d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
27 changes: 14 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.14.2

### Fix

- Set right path in pointers for non DessiaObject equal elements


## 0.14.1

Expand Down Expand Up @@ -95,19 +101,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- CI : Upload coverage is now optional



## 0.13.3

### Changed

- package_version is removed from serialization
- License changed from GPL to Lesser GPL
- Add rotation speed in measures

### Fix

- Workflow name correction: correct the name if it contains an apostrophe.

## 0.13.5 [released 09/25/2023]

### Fix
Expand All @@ -120,8 +113,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 0.13.3 [released 05/04/2023]

### Changed

- Add rotation speed in measures
- License changed from GPL to Lesser GPL
- package_version is removed from serialization

### Fix

- Fixes a bug when generating a script from a workflow : names containing special quote characters are now properly escaped
- Workflow name correction: correct the name if it contains an apostrophe.

## 0.13.2 [released 03/01/2023]

Expand Down
20 changes: 10 additions & 10 deletions dessia_common/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ def serialize_with_pointers(value, memo=None, path='#', id_method=True, id_memo=

if isinstance(value, SerializableObject):
if value in memo:
path_value, serialized_value, id_, _ = memo[value]
path_to_refs, serialized_value, id_, _ = memo[value]
id_memo[id_] = serialized_value
return {REF_MARKER: path_value}, memo
return {REF_MARKER: path_to_refs}, memo
try:
serialized = value.to_dict(use_pointers=True, memo=memo, path=path, id_memo=id_memo)
except TypeError:
Expand All @@ -149,34 +149,34 @@ def serialize_with_pointers(value, memo=None, path='#', id_method=True, id_memo=

if id_method:
id_ = str(uuid.uuid1())
path_value = f"#/_references/{id_}"
memo[value] = path_value, serialized, id_, path
path_to_refs = f"#/_references/{id_}"
memo[value] = path_to_refs, serialized, id_, path
if value._standalone_in_db:
id_memo[id_] = serialized
serialized = {REF_MARKER: path_value}
serialized = {REF_MARKER: path_to_refs}
else:
memo[value] = path, serialized, None, path

elif isinstance(value, type):
# TODO Why do we serialize types with pointers ? These are only just strings.
if value in memo:
path_value, serialized_value, id_, _ = memo[value]
path_to_refs, serialized_value, id_, _ = memo[value]
id_memo[id_] = serialized_value
return {REF_MARKER: memo[value]}, memo
serialized = serialize_annotation(value)

# Regular object
elif hasattr(value, 'to_dict'):
if value in memo:
path_value, serialized_value, id_, _ = memo[value]
path_to_refs, serialized_value, id_, path_to_value = memo[value]
id_memo[id_] = serialized_value
return {REF_MARKER: path}, memo
return {REF_MARKER: path_to_value}, memo
serialized = value.to_dict()

if id_method:
id_ = str(uuid.uuid1())
path_value = f"#/_references/{id_}"
memo[value] = path_value, serialized, id_, path
path_to_refs = f"#/_references/{id_}"
memo[value] = path_to_refs, serialized, id_, path
else:
memo[value] = path, serialized, None, path

Expand Down
Empty file.
66 changes: 66 additions & 0 deletions tests/test_serialization/test_pointers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import unittest
from dessia_common import REF_MARKER

from typing import List
from dessia_common.core import DessiaObject


class Point:
def __init__(self, x: float):
self.x = x

def __eq__(self, other):
return self.x == other.x

def __hash__(self):
return self.x

def to_dict(self):
return {"x": self.x}

@classmethod
def dict_to_object(cls, dict_):
return cls(x=dict_["x"])


class Container(DessiaObject):
def __init__(self, points: List[Point], name: str = ""):
self.points = points
self.name = name

super().__init__(name)


class Line(DessiaObject):
def __init__(self, p1: Point, p2: Point, name: str = ""):
self.p1 = p1
self.p2 = p2
self.name = name

super().__init__(name)


a = Point(0)
b = Point(1)
c = Point(0)

container = Container([a, b, c])
line = Line(a, c)


class TestNonDessiaObjects(unittest.TestCase):

def test_container(self):
# container._check_platform()
dict_ = container.to_dict()
self.assertIn(REF_MARKER, dict_["points"][0])
self.assertIn("#/_references/", dict_["points"][0][REF_MARKER])
self.assertEqual(dict_["points"][1], container.points[1].to_dict())
self.assertEqual(dict_["points"][2], {REF_MARKER: "#/points/0"})

def test_line(self):
# line._check_platform()
dict_ = line.to_dict()
self.assertIn(REF_MARKER, dict_["p1"])
self.assertIn("#/_references/", dict_["p1"][REF_MARKER])
self.assertEqual(dict_["p2"], {REF_MARKER: "#/p1"})

0 comments on commit 4c6c12d

Please sign in to comment.