Skip to content

Commit

Permalink
minor refactoring
Browse files Browse the repository at this point in the history
- remove `HasPublicData`
- use `CSharpFriendlyFullName` for id types
- fix typos
  • Loading branch information
dncsvr committed Oct 28, 2024
1 parent 2435d70 commit 8ead5c1
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ c.Method.DefaultOverload.BaseDefinition.DeclaringType is not null &&
builder.Conventions.AddTypeMetadata(new CasterAttribute(),
when: c => c.Type.IsClass && !c.Type.IsAbstract && c.Type.IsAssignableTo(typeof(ICasts<,>))
);
builder.Conventions.AddTypeMetadata(new HasPublicDataAttribute(),
when: c => c.Type.TryGetMembers(out var members) && members.Properties.Any(p => p.IsPublic)
);
});

configurator.ConfigureDomainModelBuilder(builder =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ public void Apply(ParameterModelContext context)
if (members.TryGetQueryType(_domain, out var _)) { return; }

var initializer = members.Methods.Having<InitializerAttribute>().Single();
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var parameter)) { return; }
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var idParameter)) { return; }

var factoryParameter = context.Action.AddFactoryAsService(context.Parameter.MappedParameter.ParameterType);

context.Parameter.Name = $"{context.Parameter.Name}Id";
context.Parameter.Type = "string";
context.Parameter.LookupRenderer = p => factoryParameter.BuildInitializer(p);
context.Parameter.Type = $"{idParameter.ParameterType.CSharpFriendlyFullName}";
context.Parameter.LookupRenderer = p => factoryParameter.BuildInitializerById(p);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public void Apply(ParameterModelContext context)
if (!context.Parameter.IsInvokeMethodParameter) { return; }
if (context.Parameter.MappedParameter is null) { return; }
if (!context.Parameter.MappedParameter.ParameterType.TryGetElementType(out var elementType)) { return; }
if (elementType.TryGetQueryContextType(_domain, out var queryContextType)) { return; }
if (!elementType.GetMetadata().Has<LocatableAttribute>()) { return; }
if (elementType.TryGetQueryType(_domain, out var _)) { return; }

var initializer = elementType.GetMembers().Methods.Having<InitializerAttribute>().Single();
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var parameter)) { return; }
if (!initializer.DefaultOverload.Parameters.TryGetValue("id", out var idParameter)) { return; }

var factoryParameter = context.Action.AddFactoryAsService(elementType);

context.Parameter.Type = "IEnumerable<string>";
context.Parameter.Type = $"IEnumerable<{idParameter.ParameterType.CSharpFriendlyFullName}>";
context.Parameter.Name = $"{context.Parameter.Name.Singularize()}Ids";
context.Parameter.LookupRenderer = p => factoryParameter.BuildInitializerByIds(p, isArray: context.Parameter.TypeModel.IsArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static ParameterModel AddFactoryAsService(this ActionModel action, TypeMo
return parameter;
}

public static string BuildInitializer(this ParameterModel factoryParameter, string valueExpression)
public static string BuildInitializerById(this ParameterModel factoryParameter, string valueExpression)
{
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter shold have mapped parameter"); }
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter should have mapped parameter"); }

var initializer = factoryParameter.TypeModel.GetMembers().Methods.Having<InitializerAttribute>().Single();

Expand All @@ -40,7 +40,7 @@ public static string BuildInitializerByIds(this ParameterModel factoryParameter,
bool isArray = default
)
{
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter shold have mapped parameter"); }
if (factoryParameter.TypeModel is null) { throw new("FactoryParameter should have mapped parameter"); }

var initializer = factoryParameter.TypeModel.GetMembers().Methods.Having<InitializerAttribute>().Single();
var byIds = $"{valueExpression}.Select(id => new{factoryParameter.TypeModel.Name.Pascalize()}().{initializer.Name}(id))";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,16 @@ public void Configure(LayerConfigurator configurator)
);
builder.Conventions.AddMethodMetadata(new ApiMethodAttribute(),
when: c =>
c.Type.Has<LocatableAttribute>() &&
c.Type.Has<HasPublicDataAttribute>() &&
c.Type.Has<TransientAttribute>() &&
c.Type.TryGetMembers(out var members) &&
members.Properties.Any(p => p.IsPublic) &&
c.Method.Has<InitializerAttribute>() &&
c.Method.DefaultOverload.IsPublic &&
c.Method.DefaultOverload.Parameters.Count == 1 &&
c.Method.DefaultOverload.Parameters.All(p =>
p.Name == "id" && (p.ParameterType.IsValueType || p.ParameterType.Is<string>())
),
order: 50
order: 20
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ public class RichTransientInitializerIsGetResourceConvention : IApiModelConventi
{
public void Apply(ActionModelContext context)
{
if (!context.Controller.MappedType.TryGetMetadata(out var metadata)) { return; }
if (!metadata.Has<LocatableAttribute>()) { return; }
if (!metadata.Has<HasPublicDataAttribute>()) { return; }
if (!context.Controller.MappedType.TryGetMembers(out var members)) { return; }
if (!members.Has<LocatableAttribute>()) { return; }
if (!members.Properties.Any(p => p.IsPublic)) { return; }
if (context.Action.MappedMethod is null) { return; }
if (!context.Action.MappedMethod.Has<InitializerAttribute>()) { return; }
if (!context.Action.Parameter.TryGetValue("id", out var parameter)) { return; }
Expand Down

This file was deleted.

0 comments on commit 8ead5c1

Please sign in to comment.