Skip to content

Commit

Permalink
deserialize attributes to a generic interface (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenschude authored Oct 14, 2021
1 parent a6a11c3 commit 60bacf9
Show file tree
Hide file tree
Showing 22 changed files with 769 additions and 44 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@ namespace commercetools.Api.Serialization.Tests
{
public static class Extensions
{
public static object Get(this List<IAttribute> attributes, string name)
{
return attributes.FirstOrDefault(a => a.Name.Equals(name))?.Value;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public static void UseCommercetoolsApiSerialization(this IServiceCollection serv
services.UseSerialization();
services.AddSingleton<IMapperTypeRetriever<IFieldContainer>, FieldMapperTypeRetriever>();
services.AddSingleton<IMapperTypeRetriever<IAttribute>, AttributeMapperTypeRetriever>();
services.AddSingleton<AttributeTypeRetriever>();
services.AddSingleton<SerializerService>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Collections.Generic;
using System.Linq;
using commercetools.Api.Models.Common;
using commercetools.Api.Models.Products;
using commercetools.Api.Models.ProductTypes;

namespace commercetools.Sdk.Api.Extensions
{
public static class AttributeExtensions
{
public static IAttribute Get(this List<IAttribute> attributes, string name)
{
return attributes.FirstOrDefault(a => a.Name.Equals(name));
}

public static T Get<T>(this List<IAttribute> attributes, string name) where T : IAttribute
{
return (T) attributes.FirstOrDefault(a => a.Name.Equals(name));
}

public static bool IsTextAttribute(this IAttribute attribute)
{
return
attribute is IGenericAttribute<string>;
}


public static bool IsBooleanAttribute(this IAttribute attribute)
{
return
attribute is BooleanAttribute;
}

public static bool IsEnumAttribute(this IAttribute attribute)
{
return
attribute is PlainEnumAttribute;
}
public static bool IsLocalizedEnumAttribute(this IAttribute attribute)
{
return
attribute is LocalizedEnumAttribute;
}

public static bool IsLocalizedStringAttribute(this IAttribute attribute)
{
return
attribute is LocalizedStringAttribute;
}

public static bool IsMoneyAttribute(this IAttribute attribute)
{
return
attribute is MoneyAttribute;
}

public static bool IsDecimalAttribute(this IAttribute attribute)
{
return
attribute is DecimalAttribute;
}

public static bool IsLongAttribute(this IAttribute attribute)
{
return
attribute is LongAttribute;
}

public static bool IsReferenceAttribute(this IAttribute attribute)
{
return
attribute is ReferenceAttribute;
}

public static bool IsNestedAttribute(this IAttribute attribute)
{
return
attribute is NestedAttribute ||
attribute is IGenericAttribute<List<IAttribute>>;
}

public static bool IsSetBooleanAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<bool>;
}

public static bool IsSetMoneyAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<ITypedMoney>;
}

public static bool IsSetEnumAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<IAttributePlainEnumValue>;
}

public static bool IsSetLocalizedEnumAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<IAttributeLocalizedEnumValue>;
}

public static bool IsSetLocalizedStringAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<LocalizedString>;
}

public static bool IsSetDecimalAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<decimal>;
}

public static bool IsSetLongAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<long>;
}

public static bool IsSetReferenceAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<IReference>;
}

public static bool IsSetStringAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<string>;
}

public static bool IsSetOfNestedAttribute(this IAttribute attribute)
{
return
attribute is SetAttribute<List<IAttribute>>;
}

public static bool IsSetAttribute<T>(this IAttribute attribute)
{
return
attribute is IGenericAttribute<List<T>>;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace commercetools.Api.Models.Products
{
public class BooleanAttribute : Attribute, IGenericAttribute<bool>
{
public Type GetValueType() => typeof(bool);

public bool GetValue() => (bool)Value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace commercetools.Api.Models.Products
{
public class DecimalAttribute : Attribute, IGenericAttribute<decimal>
{
public Type GetValueType() => typeof(decimal);

public decimal GetValue() => (decimal)Value;
}
}
151 changes: 151 additions & 0 deletions commercetools.Sdk/commercetools.Sdk.Api/Models/Products/IAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using System.Collections.Generic;
using commercetools.Api.Models.Common;
using commercetools.Api.Models.ProductTypes;

namespace commercetools.Api.Models.Products
{
public partial interface IAttribute
{
#nullable enable
public PlainEnumAttribute? ToPlainEnumAttribute()
{
if (this is PlainEnumAttribute t) { return t; }

return null;
}

public LocalizedEnumAttribute? ToLocalizedEnumAttribute()
{
if (this is LocalizedEnumAttribute t) { return t; }

return null;
}

public LocalizedStringAttribute? ToLocalizedStringAttribute()
{
if (this is LocalizedStringAttribute t) { return t; }

return null;
}

public StringAttribute? ToStringAttribute()
{
if (this is StringAttribute t) { return t; }

return null;
}

public MoneyAttribute? ToMoneyAttribute()
{
if (this is MoneyAttribute t) { return t; }

return null;
}

public BooleanAttribute? ToBooleanAttribute()
{
if (this is BooleanAttribute t) { return t; }

return null;
}

public DecimalAttribute? ToDecimalAttribute()
{
if (this is DecimalAttribute t) { return t; }

return null;
}

public LongAttribute? ToLongAttribute()
{
if (this is LongAttribute t) { return t; }

return null;
}

public ReferenceAttribute? ToReferenceAttribute()
{
if (this is ReferenceAttribute t) { return t; }

return null;
}

public NestedAttribute? ToNestedAttribute()
{
if (this is NestedAttribute t) { return t; }

return null;
}

public SetAttribute<string>? ToSetStringAttribute()
{
if (this is SetAttribute<string> t) { return t; }

return null;
}

public SetAttribute<LocalizedString>? ToSetLocalizedStringAttribute()
{
if (this is SetAttribute<LocalizedString> t) { return t; }

return null;
}

public SetAttribute<IAttributePlainEnumValue>? ToSetPlainEnumAttribute()
{
if (this is SetAttribute<IAttributePlainEnumValue> t) { return t; }

return null;
}

public SetAttribute<IAttributeLocalizedEnumValue>? ToSetLocalizedEnumAttribute()
{
if (this is SetAttribute<IAttributeLocalizedEnumValue> t) { return t; }

return null;
}

public SetAttribute<ITypedMoney>? ToSetMoneyAttribute()
{
if (this is SetAttribute<ITypedMoney> t) { return t; }

return null;
}

public SetAttribute<bool>? ToSetBooleanAttribute()
{
if (this is SetAttribute<bool> t) { return t; }

return null;
}

public SetAttribute<decimal>? ToSetDecimalAttribute()
{
if (this is SetAttribute<decimal> t) { return t; }

return null;
}

public SetAttribute<long>? ToSetLongAttribute()
{
if (this is SetAttribute<long> t) { return t; }

return null;
}

public SetAttribute<IReference>? ToSetReferenceAttribute()
{
if (this is SetAttribute<IReference> t) { return t; }

return null;
}

public SetAttribute<List<IAttribute>>? ToSetOfNestedAttribute()
{
if (this is SetAttribute<List<IAttribute>> t) { return t; }

return null;
}
#nullable disable
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace commercetools.Api.Models.Products
{
public interface IGenericAttribute<T>: IAttribute, IGenericTypeAttribute
{
T GetValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace commercetools.Api.Models.Products
{
public interface IGenericTypeAttribute
{
Type GetValueType();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using commercetools.Api.Models.ProductTypes;

namespace commercetools.Api.Models.Products
{
public class LocalizedEnumAttribute : Attribute, IGenericAttribute<IAttributeLocalizedEnumValue>
{
public Type GetValueType() => typeof(IAttributeLocalizedEnumValue);

public IAttributeLocalizedEnumValue GetValue() => (IAttributeLocalizedEnumValue)Value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using commercetools.Api.Models.Common;

namespace commercetools.Api.Models.Products
{
public class LocalizedStringAttribute : Attribute, IGenericAttribute<LocalizedString>
{
public Type GetValueType() => typeof(LocalizedString);

public LocalizedString GetValue() => (LocalizedString)Value;
}
}
Loading

0 comments on commit 60bacf9

Please sign in to comment.