Skip to content

Commit

Permalink
Add the ToDynamic call for other Element types
Browse files Browse the repository at this point in the history
* Attempted to do RichTextElement but it broke too many tests, so may need further thought
* Make ToDynamic more consistent in the Element types
* Improve unit test coverage
  • Loading branch information
kylejuliandev committed May 31, 2024
1 parent 108543f commit fbd38b2
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 71 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Kontent.Ai.Management.Models.Shared;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;

Expand All @@ -20,6 +21,6 @@ public class AssetElement : BaseElement
/// </summary>
public override dynamic ToDynamic() => new {
element = Element.ToDynamic(),
value = Value,
value = Value.Select(v => v.ToDynamic())
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,7 @@ public static BaseElement FromDynamic(dynamic source, Type type)
return new AssetElement
{
Element = Reference.FromDynamic(source.element),
Value = (source.value as IEnumerable<dynamic>)?
.Select(assetWithRenditionsReferences =>
new AssetWithRenditionsReference(
Reference.ById(Guid.Parse(assetWithRenditionsReferences.id)),
(assetWithRenditionsReferences.renditions as IEnumerable<dynamic>)?.Select<dynamic, Reference>(renditionIdentifier => Reference.ById(Guid.Parse(renditionIdentifier.id))))),
Value = (source.value as IEnumerable<dynamic>)?.Select(AssetWithRenditionsReference.FromDynamic)
};
}
else if (type == typeof(DateTimeElement))
Expand All @@ -108,15 +104,15 @@ public static BaseElement FromDynamic(dynamic source, Type type)
return new LinkedItemsElement
{
Element = Reference.FromDynamic(source.element),
Value = (source.value as IEnumerable<dynamic>)?.Select<dynamic, Reference>(identifier => Reference.ById(Guid.Parse(identifier.id)))
Value = (source.value as IEnumerable<dynamic>)?.Select(Reference.FromDynamic)
};
}
else if (type == typeof(MultipleChoiceElement))
{
return new MultipleChoiceElement
{
Element = Reference.FromDynamic(source.element),
Value = (source.value as IEnumerable<dynamic>)?.Select<dynamic, Reference>(identifier => Reference.ById(Guid.Parse(identifier.id)))
Value = (source.value as IEnumerable<dynamic>)?.Select(Reference.FromDynamic)
};
}
else if (type == typeof(TaxonomyElement))
Expand Down Expand Up @@ -150,7 +146,7 @@ public static BaseElement FromDynamic(dynamic source, Type type)
return new SubpagesElement
{
Element = Reference.FromDynamic(source.element),
Value = (source.value as IEnumerable<dynamic>)?.Select<dynamic, Reference>(identifier => Reference.ById(Guid.Parse(identifier.id)))
Value = (source.value as IEnumerable<dynamic>)?.Select(Reference.FromDynamic)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Kontent.Ai.Management.Models.Shared;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;

Expand All @@ -20,6 +21,6 @@ public class LinkedItemsElement : BaseElement
/// </summary>
public override dynamic ToDynamic() => new {
element = Element.ToDynamic(),
value = Value,
value = Value.Select(v => v.ToDynamic())
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Kontent.Ai.Management.Models.Shared;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;

Expand All @@ -20,6 +21,6 @@ public class MultipleChoiceElement : BaseElement
/// </summary>
public override dynamic ToDynamic() => new {
element = Element.ToDynamic(),
value = Value,
value = Value.Select(v => v.ToDynamic())
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Kontent.Ai.Management.Models.Shared;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;

namespace Kontent.Ai.Management.Models.LanguageVariants.Elements;

Expand All @@ -20,6 +21,6 @@ public class SubpagesElement : BaseElement
/// </summary>
public override dynamic ToDynamic() => new {
element = Element.ToDynamic(),
value = Value,
value = Value.Select(v => v.ToDynamic())
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,58 @@ public AssetWithRenditionsReference(Reference assetReference, IEnumerable<Refere
_assetReference = assetReference;
_renditions = renditionReferences?.ToList() ?? new List<Reference>();
}

/// <summary>
/// Transforms the dynamic object to the <see cref="AssetWithRenditionsReference"/>
/// </summary>
public static AssetWithRenditionsReference FromDynamic(dynamic source)
{
try
{
var assetReference = Reference.FromDynamic(source);
var renditions = (source.renditions as IEnumerable<dynamic>)?.Select(Reference.FromDynamic);

return new AssetWithRenditionsReference(assetReference, renditions);
}
catch (Exception exception)
{
throw new DataMisalignedException(
"Object could not be converted to the strongly-typed reference. Please check if it has expected properties with expected type",
exception);
}

throw new DataMisalignedException("Dynamic element reference does not contain any identifier.");
}

/// <summary>
/// Transforms the <see cref="AssetWithRenditionsReference"/> to the dynamic object.
/// </summary>
public dynamic ToDynamic()
{
if (Id != null)
{
return new {
id = Id,
renditions = Renditions.Select(r => r.ToDynamic())
};
}

if (Codename != null)
{
return new {
codename = Codename,
renditions = Renditions.Select(r => r.ToDynamic())
};
}

if (ExternalId != null)
{
return new {
external_id = ExternalId,
renditions = Renditions.Select(r => r.ToDynamic())
};
}

throw new DataMisalignedException("Element reference does not contain any identifier.");
}
}

0 comments on commit fbd38b2

Please sign in to comment.