Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ToDynamic is called consistently when converting a Element(s) #266

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

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,23 +104,23 @@ 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))
{
return new TaxonomyElement
{
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(UrlSlugElement))
Expand All @@ -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
@@ -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 TaxonomyElement : 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.");
}
}
Loading