Skip to content

Commit

Permalink
handle nullables for routesegment deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
runeanielsen committed Aug 19, 2020
1 parent 9586130 commit 55406f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Text;
using System.Linq;
using Topos.Serialization;
using Newtonsoft.Json.Linq;
using OpenFTTH.GDBIntegrator.RouteNetwork;
Expand Down Expand Up @@ -62,7 +63,7 @@ private RouteSegmentMessage CreateRouteSegmentMessage(dynamic payload)

private RouteSegment CreateRouteSegment(dynamic routeSegment)
{
return new RouteSegment
var mappedRouteSegment = new RouteSegment
{
Mrid = new Guid(routeSegment.mrid.ToString()),
Coord = Convert.FromBase64String(routeSegment.coord.wkb.ToString()),
Expand Down Expand Up @@ -98,6 +99,20 @@ private RouteSegment CreateRouteSegment(dynamic routeSegment)
(string)routeSegment.routesegment_height
)
};

// Make fully empty objects into nulls.
mappedRouteSegment.LifeCycleInfo = AreAnyPropertiesNotNull<LifecycleInfo>(mappedRouteSegment.LifeCycleInfo) ? mappedRouteSegment.LifeCycleInfo : null;
mappedRouteSegment.MappingInfo = AreAnyPropertiesNotNull<MappingInfo>(mappedRouteSegment.MappingInfo) ? mappedRouteSegment.MappingInfo : null;
mappedRouteSegment.NamingInfo = AreAnyPropertiesNotNull<NamingInfo>(mappedRouteSegment.NamingInfo) ? mappedRouteSegment.NamingInfo : null;
mappedRouteSegment.RouteSegmentInfo = AreAnyPropertiesNotNull<RouteSegmentInfo>(mappedRouteSegment.RouteSegmentInfo) ? mappedRouteSegment.RouteSegmentInfo : null;
mappedRouteSegment.SafetyInfo = AreAnyPropertiesNotNull<SafetyInfo>(mappedRouteSegment.SafetyInfo) ? mappedRouteSegment.SafetyInfo : null;

return mappedRouteSegment;
}

private bool AreAnyPropertiesNotNull<T>(object obj)
{
return typeof(T).GetProperties().Any(propertyInfo => propertyInfo.GetValue(obj) != null);
}

public TransportMessage Serialize(LogicalMessage message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public void Deserialize_ShouldReturnDeserializedMessage_OnValidReceivedTransport
MarkAsDeleted = false,
ApplicationInfo = string.Empty,
DeleteMe = false,
LifeCycleInfo = new LifecycleInfo(null, null, null),
MappingInfo = new MappingInfo(null, null, null, null, null),
NamingInfo = new NamingInfo(null, null),
RouteSegmentInfo = new RouteSegmentInfo(null, null, null),
SafetyInfo = new SafetyInfo(null, null)
LifeCycleInfo = null,
MappingInfo = null,
NamingInfo = null,
RouteSegmentInfo = null,
SafetyInfo = null
};

var expectedRouteSegmentAfter = new RouteSegment
Expand All @@ -105,11 +105,11 @@ public void Deserialize_ShouldReturnDeserializedMessage_OnValidReceivedTransport
MarkAsDeleted = true,
ApplicationInfo = string.Empty,
DeleteMe = false,
LifeCycleInfo = new LifecycleInfo(null, null, null),
MappingInfo = new MappingInfo(null, null, null, null, null),
NamingInfo = new NamingInfo(null, null),
RouteSegmentInfo = new RouteSegmentInfo(null, null, null),
SafetyInfo = new SafetyInfo(null, null)
LifeCycleInfo = null,
MappingInfo = null,
NamingInfo = null,
RouteSegmentInfo = null,
SafetyInfo = null
};

var expectedMessage = new RouteSegmentMessage(expectedRouteSegmentBefore, expectedRouteSegmentAfter);
Expand Down

0 comments on commit 55406f7

Please sign in to comment.