Skip to content

Commit

Permalink
Merge pull request #413 from commercetools/devx-551-product-tailoring…
Browse files Browse the repository at this point in the history
…-attributes

[DEVX-551] add json converter for ProductTailoringAttributes
  • Loading branch information
kodiakhq[bot] authored Feb 27, 2025
2 parents ab65f1e + f677363 commit 105dad2
Show file tree
Hide file tree
Showing 22 changed files with 961 additions and 12 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
{
"id": 1,
"key": "newKey",
"attributes": [
{
"name": "text",
"value": "txt"
},
{
"name": "ltext",
"value": {
"de": "de-txt",
"en": "en-txt"
}
},
{
"name": "enum",
"value": {
"key": "enum-key2",
"label": "enum-label2"
}
},
{
"name": "lenum",
"value": {
"key": "lenum-key2",
"label": {
"de": "enum-label2-de",
"en": "enum-label2-en"
}
}
},
{
"name": "date",
"value": "2021-03-24"
},
{
"name": "time",
"value": "13:23:00.000"
},
{
"name": "datetime",
"value": "2021-03-24T00:45:00.000Z"
},
{
"name": "boolean",
"value": true
},
{
"name": "integer",
"value": 12233344
},
{
"name": "double",
"value": 10.2
},
{
"name": "double-zero",
"value": 13.0
},
{
"name": "reference",
"value": {
"typeId": "product",
"id": "b7334b20-c570-4e1d-a71a-35efd1c1c895"
}
},
{
"name": "money",
"value": {
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 234500,
"fractionDigits": 2
}
},
{
"name": "set-text",
"value": ["foo"]
},
{
"name": "set-ltext",
"value": [{ "en": "foo" }]
},
{
"name": "set-integer",
"value": [10]
},
{
"name": "set-double",
"value": [11.2]
},
{
"name": "set-double-zero",
"value": [13.0]
},
{
"name": "set-enum",
"value": [{
"key": "foo",
"label": "foo"
}]
},
{
"name": "set-lenum",
"value": [{
"key": "foo",
"label": {
"en": "foo"
}
}]
},
{
"name": "set-money",
"value": [{
"type": "centPrecision",
"currencyCode": "EUR",
"centAmount": 100
}]
},
{
"name": "set-reference",
"value": [{
"typeId": "product",
"id": "12345"
}]
},
{
"name": "set-date",
"value": ["2020-01-01"]
},
{
"name": "set-datetime",
"value": ["2020-01-01T13:15:00.123Z"]
},
{
"name": "set-time",
"value": ["13:15:00.123"]
},
{
"name": "set-boolean",
"value": [true]
},
{
"name": "set-empty",
"value": []
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": 1,
"key": "newKey",
"attributes": [
{
"name": "set-mixed",
"value": [10, 10.3]
},
{
"name": "decimal",
"value": 10.3
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using commercetools.Base.Registration;
using commercetools.Base.Serialization;
using commercetools.Sdk.Api.Client;
using commercetools.Sdk.Api.Models.ProductTailorings;
using commercetools.Sdk.Api.Serialization;
using commercetools.Sdk.Api.Serialization.MapperTypeRetrievers;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -74,8 +75,10 @@ public static void UseCommercetoolsApiSerialization(this IServiceCollection serv
services.AddSingleton(serializationConfiguration ?? SerializationConfiguration.DefaultConfig);
services.AddSingleton<IMapperTypeRetriever<IFieldContainer>>(provider => new FieldMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<IMapperTypeRetriever<IAttribute>>(provider => new AttributeMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<IMapperTypeRetriever<IProductTailoringAttribute>>(provider => new ProductTailoringAttributeMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<AttributeTypeRetriever>(provider => new AttributeTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<FieldMapperTypeRetriever>(provider => new FieldMapperTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<ProductTailoringAttributeTypeRetriever>(provider => new ProductTailoringAttributeTypeRetriever(provider.GetService<ISerializationConfiguration>()));
services.AddSingleton<SerializerService>();
services.AddSingleton<IApiSerializerService, SerializerService>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Linq;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Models.Products;
using commercetools.Sdk.Api.Models.ProductTailorings;
using commercetools.Sdk.Api.Models.ProductTypes;

namespace commercetools.Sdk.Api.Extensions
Expand All @@ -12,6 +13,11 @@ public static IAttribute Get(this IList<IAttribute> attributes, string name)
{
return attributes.FirstOrDefault(a => a.Name.Equals(name));
}

public static IProductTailoringAttribute Get(this IList<IProductTailoringAttribute> attributes, string name)
{
return attributes.FirstOrDefault(a => a.Name.Equals(name));
}

public static T Get<T>(this IList<IAttribute> attributes, string name) where T : IAttribute
{
Expand All @@ -28,6 +34,22 @@ public static T Get<T>(this IList<IAttribute> attributes, string name) where T :
}
return (T)t;
}

public static T Get<T>(this IList<IProductTailoringAttribute> attributes, string name) where T : IProductTailoringAttribute
{
var t = attributes.FirstOrDefault(a => a.Name.Equals(name));
if (t is LongAttribute l && typeof(T).IsAssignableFrom(typeof(DecimalAttribute)))
{
var toD = (DecimalAttribute)l;
return (T)(IAttribute)toD;
}
if (t is DecimalAttribute d && typeof(T).IsAssignableFrom(typeof(LongAttribute)))
{
var toL = (LongAttribute)d;
return (T)(IAttribute)toL;
}
return (T)t;
}

public static bool IsTextAttribute(this IAttribute attribute)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using commercetools.Base.CustomAttributes;
using commercetools.Sdk.Api.Models.Products;

// ReSharper disable CheckNamespace
namespace commercetools.Sdk.Api.Models.ProductTailorings
{
public partial interface IProductTailoringAttribute : IAttribute
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using commercetools.Sdk.Api.Models.Products;

namespace commercetools.Sdk.Api.Models.ProductTailorings
{

public partial class ProductTailoringAttribute : Attribute
{
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;
using commercetools.Sdk.Api.Models.ProductTypes;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Models.ProductTailorings;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;

namespace commercetools.Sdk.Api.Models.Products
{
public class LongAttribute : Attribute, IGenericAttribute<long>
public class LongAttribute : Attribute, IGenericAttribute<long>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(long);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Models.ProductTailorings;

namespace commercetools.Sdk.Api.Models.Products
{
public class MoneyAttribute : Attribute, IGenericAttribute<ITypedMoney>
public class MoneyAttribute : Attribute, IGenericAttribute<ITypedMoney>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(ITypedMoney);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;
using commercetools.Sdk.Api.Models.ProductTypes;

namespace commercetools.Sdk.Api.Models.Products
{
public class PlainEnumAttribute : Attribute, IGenericAttribute<IAttributePlainEnumValue>
public class PlainEnumAttribute : Attribute, IGenericAttribute<IAttributePlainEnumValue>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(IAttributePlainEnumValue);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Models.ProductTailorings;

namespace commercetools.Sdk.Api.Models.Products
{
public class ReferenceAttribute : Attribute, IGenericAttribute<IReference>
public class ReferenceAttribute : Attribute, IGenericAttribute<IReference>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(IReference);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using commercetools.Sdk.Api.Models.ProductTailorings;

namespace commercetools.Sdk.Api.Models.Products
{
public class SetAttribute<T> : Attribute, IGenericAttribute<List<T>>
public class SetAttribute<T> : Attribute, IGenericAttribute<List<T>>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(List<T>);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using commercetools.Sdk.Api.Models.ProductTailorings;

namespace commercetools.Sdk.Api.Models.Products
{
public class StringAttribute : Attribute, IGenericAttribute<string>
public class StringAttribute : Attribute, IGenericAttribute<string>, IProductTailoringAttribute
{
public Type GetValueType() => typeof(string);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using commercetools.Sdk.Api.Models.Products;
using commercetools.Base.Serialization;
using commercetools.Base.Serialization.MapperTypeRetrievers;
using commercetools.Sdk.Api.Models.ProductTailorings;
using commercetools.Sdk.Api.Serialization.MapperTypeRetrievers;
using Attribute = commercetools.Sdk.Api.Models.Products.Attribute;
using Type = System.Type;
Expand All @@ -24,7 +25,7 @@ public AttributeConverter(IMapperTypeRetriever<IAttribute> mapperTypeRetriever,

public override bool CanConvert(Type typeToConvert)
{
return typeof(IAttribute).IsAssignableFrom(typeToConvert);
return typeof(IAttribute).IsAssignableFrom(typeToConvert) && !typeof(IProductTailoringAttribute).IsAssignableFrom(typeToConvert);
}

public override IAttribute Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Expand Down
Loading

0 comments on commit 105dad2

Please sign in to comment.