Skip to content

Commit

Permalink
Merge pull request #11 from Relewise/feature/also-match-on-descendant…
Browse files Browse the repository at this point in the history
…-properties-for-constructors

Feature/also match on descendant properties for constructors
  • Loading branch information
KSGRelewise authored Aug 12, 2024
2 parents 516f6cb + f42c9d3 commit 3906035
Show file tree
Hide file tree
Showing 55 changed files with 748 additions and 92 deletions.
58 changes: 51 additions & 7 deletions Generator/JavaMemberWriters/JavaCreatorMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Write(IndentedTextWriter writer, Type type, string typeName, (Proper
&& c.GetParameters().Length == propertyInformations.Length // There are as many parameters as there are properties.
&& c.GetParameters()
.All(parameter => propertyInformations
.Any(property => ParameterIsPersuadableIntoPropertyType(property.info, parameter))
.Any(property => ParameterHasSameNullabilityAsProperty(property.info, parameter))
) // There is a property type that matches each parameter type.
);

Expand All @@ -45,7 +45,14 @@ public void Write(IndentedTextWriter writer, Type type, string typeName, (Proper
&& ParameterIsPersuadableIntoPropertyType(property.info, parameter)) == 1
) // There is exactly 1 property type that matches each parameter type and name
)
.MaxBy(c => c.GetParameters().Length); // We take the largest constructor to be more deterministic.
.Where(c => c.GetParameters().Length > 0)
.ToArray();

var largestCoveringTypeAndNameMappableConstructorParameters = coveringTypeAndNameMappableConstructorParameters
.MaxBy(c => c.GetParameters().Length);

var smallestCoveringTypeAndNameMappableConstructorParameters = coveringTypeAndNameMappableConstructorParameters
.MinBy(c => c.GetParameters().Length);

var allConstructorParametersIntersectionWithMappableNamesAndTypes = type
.GetConstructors()
Expand All @@ -62,14 +69,27 @@ public void Write(IndentedTextWriter writer, Type type, string typeName, (Proper
WriteConstructor(writer, type, typeName, propertyInformations, coveringUniqueTypeMappableConstructorParameters, defaultParameters);
}
}
else if (coveringTypeAndNameMappableConstructorParameters != null)
else if (largestCoveringTypeAndNameMappableConstructorParameters != null || smallestCoveringTypeAndNameMappableConstructorParameters != null)
{
string[] defaultParameters = coveringTypeAndNameMappableConstructorParameters.GetParameters().Where(parameter => parameter.HasDefaultValue).Select(parameter => parameter.Name).ToArray()!;
if (largestCoveringTypeAndNameMappableConstructorParameters != null)
{
string[] defaultParameters = largestCoveringTypeAndNameMappableConstructorParameters.GetParameters().Where(parameter => parameter.HasDefaultValue).Select(parameter => parameter.Name).ToArray()!;

WriteConstructor(writer, type, typeName, propertyInformations, coveringTypeAndNameMappableConstructorParameters, Array.Empty<string>());
if (defaultParameters.Any())
WriteConstructor(writer, type, typeName, propertyInformations, largestCoveringTypeAndNameMappableConstructorParameters, Array.Empty<string>());
if (defaultParameters.Any())
{
WriteConstructor(writer, type, typeName, propertyInformations, largestCoveringTypeAndNameMappableConstructorParameters, defaultParameters);
}
}
if (smallestCoveringTypeAndNameMappableConstructorParameters != null)
{
WriteConstructor(writer, type, typeName, propertyInformations, coveringTypeAndNameMappableConstructorParameters, defaultParameters);
string[] defaultParameters = smallestCoveringTypeAndNameMappableConstructorParameters.GetParameters().Where(parameter => parameter.HasDefaultValue).Select(parameter => parameter.Name).ToArray()!;

WriteConstructor(writer, type, typeName, propertyInformations, smallestCoveringTypeAndNameMappableConstructorParameters, Array.Empty<string>());
if (defaultParameters.Any())
{
WriteConstructor(writer, type, typeName, propertyInformations, smallestCoveringTypeAndNameMappableConstructorParameters, defaultParameters);
}
}
}
else if (allConstructorParametersIntersectionWithMappableNamesAndTypes != null)
Expand Down Expand Up @@ -291,6 +311,30 @@ private static bool ParameterIsPersuadableIntoPropertyType(PropertyInfo property
return false;
}

private static bool ParameterHasSameNullabilityAsProperty(PropertyInfo property, ParameterInfo parameter)
{
if (EqualCollectionElementType(property.PropertyType, parameter.ParameterType))
{
return true;
}

Type propertyType = property.PropertyType.IsConstructedGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) ? property.PropertyType.GetGenericArguments()[0] : property.PropertyType;
Type parameterType = parameter.ParameterType.IsConstructedGenericType && parameter.ParameterType.GetGenericTypeDefinition() == typeof(Nullable<>) ? parameter.ParameterType.GetGenericArguments()[0] : parameter.ParameterType;

var propertyNullabilityContext = new NullabilityInfoContext().Create(property);
var parameterNullabilityContext = new NullabilityInfoContext().Create(parameter);

bool propertyIsNullable = property.PropertyType != propertyType || propertyNullabilityContext.WriteState is NullabilityState.Nullable;
bool parameterIsNullable = parameter.ParameterType != parameterType || parameterNullabilityContext.WriteState is NullabilityState.Nullable;

if (propertyType != parameterType)
{
return false;
}

return parameterIsNullable == propertyIsNullable;
}

private static bool ContainedWithinEitherOne(string? first, string? second)
{
if (first is null || second is null)
Expand Down
2 changes: 1 addition & 1 deletion Generator/JavaTypeWriters/JavaClassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void Write(IndentedTextWriter writer, Type type, string typeName)
javaWriter.SettablePropertiesWriter.Write(writer, ownedProperties);
javaWriter.StaticReadonlyPropertiesWriter.Write(writer, type, staticGetterProperties);

javaWriter.CreatorMethodWriter.Write(writer, type, typeName, ownedProperties);
javaWriter.CreatorMethodWriter.Write(writer, type, typeName, settableProperties);
javaWriter.PropertyGetterMethodsWriter.Write(writer, ownedProperties);
javaWriter.PropertySetterMethodsWriter.Write(writer, type, typeName, settableProperties, ownedProperties.Select(p => p.propertyName).ToArray());

Expand Down
38 changes: 35 additions & 3 deletions src/src/main/java/com/relewise/client/model/BrandDataFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,56 @@
public class BrandDataFilter extends DataFilter implements IBrandFilter
{
public String $type = "Relewise.Client.Requests.Filters.BrandDataFilter, Relewise.Client";
public static BrandDataFilter create(String key, String... objectPath)
{
return new BrandDataFilter(key, objectPath);
}
public BrandDataFilter(String key, String... objectPath)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static BrandDataFilter create(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new BrandDataFilter(key, objectPath, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public BrandDataFilter(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
}
public static BrandDataFilter create(String key)
{
return new BrandDataFilter(key);
}
public BrandDataFilter(String key)
{
this.key = key;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static BrandDataFilter create(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public static BrandDataFilter create(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new BrandDataFilter(key, filterOutIfKeyIsNotFound, language, currency);
return new BrandDataFilter(key, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public BrandDataFilter(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public BrandDataFilter(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
Expand Down
20 changes: 20 additions & 0 deletions src/src/main/java/com/relewise/client/model/ContainsCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ public class ContainsCondition extends ValueCondition
public @Nullable DataValue value;
public ContainsConditionCollectionArgumentEvaluationMode valueCollectionEvaluationMode;
public @Nullable DataObjectFilter objectFilter;
public static ContainsCondition create(DataValue value, ContainsConditionCollectionArgumentEvaluationMode valueCollectionEvaluationMode)
{
return new ContainsCondition(value, valueCollectionEvaluationMode);
}
public ContainsCondition(DataValue value, ContainsConditionCollectionArgumentEvaluationMode valueCollectionEvaluationMode)
{
this.value = value;
this.valueCollectionEvaluationMode = valueCollectionEvaluationMode;
this.negated = false;
}
public static ContainsCondition create(DataValue value, ContainsConditionCollectionArgumentEvaluationMode valueCollectionEvaluationMode, Boolean negated)
{
return new ContainsCondition(value, valueCollectionEvaluationMode, negated);
}
public ContainsCondition(DataValue value, ContainsConditionCollectionArgumentEvaluationMode valueCollectionEvaluationMode, Boolean negated)
{
this.value = value;
this.valueCollectionEvaluationMode = valueCollectionEvaluationMode;
this.negated = negated;
}
public static ContainsCondition create(DataValue value)
{
return new ContainsCondition(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,56 @@
public class ContentCategoryDataFilter extends DataFilter implements ICategoryFilter
{
public String $type = "Relewise.Client.Requests.Filters.ContentCategoryDataFilter, Relewise.Client";
public static ContentCategoryDataFilter create(String key, String... objectPath)
{
return new ContentCategoryDataFilter(key, objectPath);
}
public ContentCategoryDataFilter(String key, String... objectPath)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static ContentCategoryDataFilter create(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new ContentCategoryDataFilter(key, objectPath, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public ContentCategoryDataFilter(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
}
public static ContentCategoryDataFilter create(String key)
{
return new ContentCategoryDataFilter(key);
}
public ContentCategoryDataFilter(String key)
{
this.key = key;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static ContentCategoryDataFilter create(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public static ContentCategoryDataFilter create(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new ContentCategoryDataFilter(key, filterOutIfKeyIsNotFound, language, currency);
return new ContentCategoryDataFilter(key, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public ContentCategoryDataFilter(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public ContentCategoryDataFilter(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public class ContentCategorySearchRequest extends PaginatedSearchRequest impleme
public String $type = "Relewise.Client.Requests.Search.ContentCategorySearchRequest, Relewise.Client";
public String term;
public @Nullable ContentCategorySearchSettings settings;
public static ContentCategorySearchRequest create(@Nullable Language language, @Nullable Currency currency, @Nullable SearchIndexSelector indexSelector, User user, String displayedAtLocation, String term, Integer skip, Integer take)
{
return new ContentCategorySearchRequest(language, currency, indexSelector, user, displayedAtLocation, term, skip, take);
}
public ContentCategorySearchRequest(@Nullable Language language, @Nullable Currency currency, @Nullable SearchIndexSelector indexSelector, User user, String displayedAtLocation, String term, Integer skip, Integer take)
{
this.language = language;
this.currency = currency;
this.indexSelector = indexSelector;
this.user = user;
this.displayedAtLocation = displayedAtLocation;
this.term = term;
this.skip = skip;
this.take = take;
}
public static ContentCategorySearchRequest create(@Nullable Language language, @Nullable Currency currency, User user, String displayedAtLocation, String term, Integer skip, Integer take)
{
return new ContentCategorySearchRequest(language, currency, user, displayedAtLocation, term, skip, take);
Expand Down
38 changes: 35 additions & 3 deletions src/src/main/java/com/relewise/client/model/ContentDataFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,56 @@
public class ContentDataFilter extends DataFilter implements IContentFilter
{
public String $type = "Relewise.Client.Requests.Filters.ContentDataFilter, Relewise.Client";
public static ContentDataFilter create(String key, String... objectPath)
{
return new ContentDataFilter(key, objectPath);
}
public ContentDataFilter(String key, String... objectPath)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static ContentDataFilter create(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new ContentDataFilter(key, objectPath, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public ContentDataFilter(String key, String[] objectPath, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.objectPath = objectPath;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
}
public static ContentDataFilter create(String key)
{
return new ContentDataFilter(key);
}
public ContentDataFilter(String key)
{
this.key = key;
this.conditions = null;
this.mustMatchAllConditions = true;
this.filterOutIfKeyIsNotFound = true;
this.language = null;
this.currency = null;
}
public static ContentDataFilter create(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public static ContentDataFilter create(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
return new ContentDataFilter(key, filterOutIfKeyIsNotFound, language, currency);
return new ContentDataFilter(key, conditions, mustMatchAllConditions, filterOutIfKeyIsNotFound, language, currency);
}
public ContentDataFilter(String key, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
public ContentDataFilter(String key, @Nullable ValueConditionCollection conditions, Boolean mustMatchAllConditions, Boolean filterOutIfKeyIsNotFound, @Nullable Language language, Currency currency)
{
this.key = key;
this.conditions = conditions;
this.mustMatchAllConditions = mustMatchAllConditions;
this.filterOutIfKeyIsNotFound = filterOutIfKeyIsNotFound;
this.language = language;
this.currency = currency;
Expand Down
Loading

0 comments on commit 3906035

Please sign in to comment.