-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
deserialize attributes to a generic interface (#98)
- Loading branch information
Showing
22 changed files
with
769 additions
and
44 deletions.
There are no files selected for viewing
204 changes: 174 additions & 30 deletions
204
commercetools.Sdk/Tests/commercetools.Api.Serialization.Tests/AttributesTests.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
commercetools.Sdk/commercetools.Sdk.Api/Extensions/AttributeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/BooleanAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/DecimalAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
151
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/IAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/IGenericAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/IGenericTypeAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/LocalizedEnumAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
commercetools.Sdk/commercetools.Sdk.Api/Models/Products/LocalizedStringAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.