/// <summary> /// Generates the complete MethodName for a given <see cref="OperationContext"/>, <see cref="RestierPipelineState"/>, and <see cref="RestierEntitySetOperation"/>. /// </summary> /// <param name="operationImport">The <see cref="OperationContext"/> to generate a name for.</param> /// <param name="restierPipelineState">The part of the Restier pipeline currently executing.</param> /// <param name="restierOperation">The <see cref="RestierOperationMethod"/> currently being executed.</param> /// <returns>A string representing the fully-realized MethodName.</returns> public static string GetFunctionMethodName(OperationContext operationImport, RestierPipelineState restierPipelineState, RestierOperationMethod restierOperation) { if (operationImport == null) { return(string.Empty); } return(GetFunctionMethodNameInternal(operationImport.OperationName, restierPipelineState, restierOperation)); }
/// <summary> /// Generates the right OperationName string for a given <see cref="RestierOperationMethod"/> and <see cref="RestierPipelineState"/>. /// </summary> /// <param name="operation">The string representing the Operation to determine the method name for.</param> /// <param name="restierPipelineState">The <see cref="RestierPipelineState"/> to determine the method name for.</param> /// <returns>A string containing the corrected OperationName, accounting for what the suffix will end up being.</returns> /// <remarks>This method is for base processing. The other overloads should be used to ensure the right name gets generated.</remarks> private static string GetRestierOperationNameInternal(string operation, RestierPipelineState restierPipelineState) { switch (restierPipelineState) { case RestierPipelineState.PreSubmit: case RestierPipelineState.PostSubmit: //RWM: If the last letter of the string is an e, cut off it's head. return(operation.LastIndexOf("e", StringComparison.InvariantCulture) == operation.Length - 1 ? operation.Substring(0, operation.Length - 1) : operation); default: return(operation); } }
public static void CanCallGetFunctionMethodNameWithIEdmOperationImportAndRestierPipelineStateAndRestierOperationMethod( RestierPipelineState pipelineState, string expected) { var operationImportMock = new Mock <IEdmOperationImport>(); var operationMock = new Mock <IEdmOperation>(); operationMock.Setup(x => x.Name).Returns("Calculate"); operationImportMock.Setup(x => x.Operation).Returns(operationMock.Object); var restierOperation = RestierOperationMethod.Execute; var result = ConventionBasedMethodNameFactory.GetFunctionMethodName(operationImportMock.Object, pipelineState, restierOperation); result.Should().Be(expected); }
/// <summary> /// Returns a method suffix string for a given <see cref="RestierPipelineState"/>. /// </summary> /// <param name="restierPipelineState">The <see cref="RestierPipelineState"/> to determine the suffix for.</param> /// <returns></returns> internal static string GetPipelineSuffixInternal(RestierPipelineState restierPipelineState) { switch (restierPipelineState) { case RestierPipelineState.PreSubmit: return(Ing); case RestierPipelineState.PostSubmit: return(Ed); default: return(string.Empty); } }
private Task InvokeProcessorMethodAsync(OperationContext context, RestierPipelineState pipelineState) { var parameters = context.ParameterValues?.ToArray() ?? Array.Empty <object>(); var expectedMethodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, pipelineState, RestierOperationMethod.Execute); var expectedMethod = targetApiType.GetQualifiedMethod(expectedMethodName); if (expectedMethod == null) { return(Task.CompletedTask); } if (!expectedMethod.IsFamily && !expectedMethod.IsFamilyOrAssembly) { Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it is inaccessible due to its protection level. Your method will not be called until you change it to 'protected internal'."); return(Task.CompletedTask); } if (expectedMethod.ReturnType != typeof(void) && !typeof(Task).IsAssignableFrom(expectedMethod.ReturnType)) { Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it does not return void or a Task. Your method will not be called until you correct the return type."); return(Task.CompletedTask); } object target = null; if (!expectedMethod.IsStatic) { target = context.Api; if (!targetApiType.IsInstanceOfType(target)) { Trace.WriteLine("The Restier API is of the incorrect type."); return(Task.CompletedTask); } } var methodParameters = expectedMethod.GetParameters(); if (ParametersMatch(methodParameters, parameters)) { var result = expectedMethod.Invoke(target, parameters); if (result is Task resultTask) { return(resultTask); } } Trace.WriteLine($"Restier Authorizer found '{expectedMethod}', but it has an incorrect number of arguments or the types don't match. The number of arguments should be 1."); return(Task.CompletedTask); }
public void CanCallGetFunctionMethodNameWithOperationContextAndRestierPipelineStateAndRestierOperationMethod( RestierPipelineState pipelineState, string expected) { var operationImport = new OperationContext( new EmptyApi(serviceProvider), name => this, "Calculate", false, new Mock <IEnumerable>().Object); var restierOperation = RestierOperationMethod.Execute; var result = ConventionBasedMethodNameFactory.GetFunctionMethodName(operationImport, pipelineState, restierOperation); result.Should().Be(expected); }
/// <summary> /// Returns a method prefix string for a given <see cref="RestierPipelineState"/>. /// </summary> /// <param name="restierPipelineState">The <see cref="RestierPipelineState"/> to determine the prefix for.</param> /// <returns></returns> internal static string GetPipelinePrefixInternal(RestierPipelineState restierPipelineState) { switch (restierPipelineState) { case RestierPipelineState.Authorization: return(Can); case RestierPipelineState.PreSubmit: case RestierPipelineState.Submit: case RestierPipelineState.PostSubmit: return(On); default: return(string.Empty); } }
public static void CanCallGetEntitySetMethodNameWithItemAndRestierPipelineState( RestierPipelineState pipelineState, RestierEntitySetOperation entitySetOperation, string expected) { var item = new DataModificationItem( "Tests", typeof(Test), typeof(Test), entitySetOperation, new Mock <IReadOnlyDictionary <string, object> >().Object, new Mock <IReadOnlyDictionary <string, object> >().Object, new Mock <IReadOnlyDictionary <string, object> >().Object); var result = ConventionBasedMethodNameFactory.GetEntitySetMethodName(item, pipelineState); result.Should().Be(expected); }
/// <summary> /// Generates the complete MethodName for a given <see cref="IEdmOperationImport"/>, <see cref="RestierPipelineState"/>, and <see cref="RestierEntitySetOperation"/>. /// </summary> /// <param name="operationName">The <see cref="string"/> containing the name of the operation.</param> /// <param name="restierPipelineState">The part of the Restier pipeline currently executing.</param> /// <param name="restierOperation">The <see cref="RestierOperationMethod"/> currently being executed.</param> /// <returns>A string representing the fully-realized MethodName.</returns> private static string GetFunctionMethodNameInternal(string operationName, RestierPipelineState restierPipelineState, RestierOperationMethod restierOperation) { if (restierPipelineState == RestierPipelineState.Submit && ExcludedMethodSubmitOperations.Contains(restierOperation)) { return(string.Empty); } var prefix = GetPipelinePrefixInternal(restierPipelineState); //RWM: If, for some reason, we don't have a prefix, then we don't have a method for this operation. So don't do anything. if (string.IsNullOrWhiteSpace(prefix)) { return(string.Empty); } var restierOperationName = GetRestierOperationNameInternal(restierOperation, restierPipelineState); var suffix = GetPipelineSuffixInternal(restierPipelineState); return($"{prefix}{restierOperationName}{suffix}{operationName}"); }
public static void CanCallGetEntitySetMethodNameWithEntitySetAndRestierPipelineStateAndOperation( RestierPipelineState pipelineState, RestierEntitySetOperation entitySetOperation, string expected) { var entitySetMock = new Mock <IEdmEntitySet>(); var entityCollectionTypeMock = new Mock <IEdmCollectionType>(); var entityTypeReferenceMock = new Mock <IEdmEntityTypeReference>(); var entityTypeMock = new Mock <IEdmEntityType>(); entityTypeMock.Setup(x => x.Name).Returns("Test"); entityTypeReferenceMock.Setup(x => x.Definition).Returns(entityTypeMock.Object); entityCollectionTypeMock.Setup(x => x.ElementType).Returns(entityTypeReferenceMock.Object); entitySetMock.Setup(x => x.Name).Returns("Tests"); entitySetMock.Setup(x => x.Type).Returns(entityCollectionTypeMock.Object); var result = ConventionBasedMethodNameFactory.GetEntitySetMethodName(entitySetMock.Object, pipelineState, entitySetOperation); result.Should().Be(expected); }
/// <summary> /// Generates the complete MethodName for a given <see cref="IEdmOperationImport"/>, <see cref="RestierPipelineState"/>, and <see cref="RestierEntitySetOperation"/>. /// </summary> /// <param name="entitySet">The <see cref="IEdmEntitySet"/> that contains the details for the EntitySet and the Entities it holds.</param> /// <param name="restierPipelineState">The part of the Restier pipeline currently executing.</param> /// <param name="operation">The <see cref="RestierEntitySetOperation"/> currently being executed.</param> /// <returns>A string representing the fully-realized MethodName.</returns> /// <returns></returns> public static string GetEntitySetMethodName(IEdmEntitySet entitySet, RestierPipelineState restierPipelineState, RestierEntitySetOperation operation) { if ((operation == RestierEntitySetOperation.Filter && ExcludedFilterStates.Contains(restierPipelineState)) || restierPipelineState == RestierPipelineState.Submit && ExcludedEntitySetSubmitOperations.Contains(operation)) { return(string.Empty); } var prefix = GetPipelinePrefixInternal(restierPipelineState); //RWM: If, for some reason, we don't have a prefix, then we don't have a method for this operation. So don't do anything. if (string.IsNullOrWhiteSpace(prefix)) { return(string.Empty); } var operationName = GetRestierOperationNameInternal(operation, restierPipelineState); var suffix = operation != RestierEntitySetOperation.Filter ? GetPipelineSuffixInternal(restierPipelineState) : string.Empty; var entityReferenceName = GetEntityReferenceNameInternal(operation, entitySet); return($"{prefix}{operationName}{suffix}{entityReferenceName}"); }
/// <summary> /// Generates the complete MethodName for a given <see cref="IEdmOperationImport"/>, <see cref="RestierPipelineState"/>, and <see cref="RestierEntitySetOperation"/>. /// </summary> /// <param name="item">The <see cref="DataModificationItem"/> that contains the details for the EntitySet and the Entities it holds.</param> /// <param name="restierPipelineState">The part of the Restier pipeline currently executing.</param> /// <returns>A string representing the fully-realized MethodName.</returns> /// <returns></returns> public static string GetEntitySetMethodName(DataModificationItem item, RestierPipelineState restierPipelineState) { if ((item.EntitySetOperation == RestierEntitySetOperation.Filter && ExcludedFilterStates.Contains(restierPipelineState)) || restierPipelineState == RestierPipelineState.Submit && ExcludedEntitySetSubmitOperations.Contains(item.EntitySetOperation)) { return(string.Empty); } var prefix = GetPipelinePrefixInternal(restierPipelineState); //RWM: If, for some reason, we don't have a prefix, then we don't have a method for this operation. So don't do anything. if (string.IsNullOrWhiteSpace(prefix)) { return(string.Empty); } var operationName = GetRestierOperationNameInternal(item.EntitySetOperation, restierPipelineState); var suffix = item.EntitySetOperation != RestierEntitySetOperation.Filter ? GetPipelineSuffixInternal(restierPipelineState) : string.Empty; var entityReferenceName = GetEntityReferenceNameInternal(item.EntitySetOperation, item.ResourceSetName, item.ExpectedResourceType.Name); return($"{prefix}{operationName}{suffix}{entityReferenceName}"); }
private Task InvokeProcessorMethodAsync(OperationContext context, RestierPipelineState pipelineState) { var methodName = ConventionBasedMethodNameFactory.GetFunctionMethodName(context, pipelineState, RestierOperationMethod.Execute); object[] parameters = null; if (context.ParameterValues != null) { parameters = context.ParameterValues.ToArray(); } var method = targetType.GetQualifiedMethod(methodName); if (method != null && (method.ReturnType == typeof(void) || typeof(Task).IsAssignableFrom(method.ReturnType))) { object target = null; if (!method.IsStatic) { target = context.Api; if (target == null || !targetType.IsInstanceOfType(target)) { return(Task.WhenAll()); } } var methodParameters = method.GetParameters(); if (ParametersMatch(methodParameters, parameters)) { var result = method.Invoke(target, parameters); if (result is Task resultTask) { return(resultTask); } } } return(Task.WhenAll()); }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="pipelineState"></param> /// <param name="methodName"></param> /// <param name="methodOperation"></param> public RestierConventionMethodDefinition(string name, RestierPipelineState pipelineState, string methodName, RestierOperationMethod methodOperation) : base(name, pipelineState) { MethodName = methodName; MethodOperation = methodOperation; }
/// <summary> /// /// </summary> /// <param name="name"></param> /// <param name="pipelineState"></param> internal RestierConventionDefinition(string name, RestierPipelineState pipelineState) { Name = name; PipelineState = pipelineState; }
/// <summary> /// Creates a new <see cref="RestierConventionEntitySetDefinition"/> instance. /// </summary> /// <param name="name"></param> /// <param name="pipelineState"></param> /// <param name="entitySetName"></param> /// <param name="entitySetOperation"></param> internal RestierConventionEntitySetDefinition(string name, RestierPipelineState pipelineState, string entitySetName, RestierEntitySetOperation entitySetOperation) : base(name, pipelineState) { EntitySetName = entitySetName; EntitySetOperation = entitySetOperation; }
/// <summary> /// Generates the right OperationName string for a given <see cref="RestierOperationMethod"/> and <see cref="RestierPipelineState"/>. /// </summary> /// <param name="operation">The <see cref="RestierOperationMethod"/> to determine the method name for.</param> /// <param name="restierPipelineState">The <see cref="RestierPipelineState"/> to determine the method name for.</param> /// <returns>A string containing the corrected OperationName, accounting for what the suffix will end up being.</returns> internal static string GetRestierOperationNameInternal(RestierOperationMethod operation, RestierPipelineState restierPipelineState) { return(GetRestierOperationNameInternal(operation.ToString(), restierPipelineState)); }
/// <summary> /// Generates the complete MethodName for a given <see cref="IEdmOperationImport"/>, <see cref="RestierPipelineState"/>, and <see cref="RestierEntitySetOperation"/>. /// </summary> /// <param name="operationImport">The <see cref="IEdmOperationImport"/> to generate a name for.</param> /// <param name="restierPipelineState">The part of the Restier pipeline currently executing.</param> /// <param name="restierOperation">The <see cref="RestierOperationMethod"/> currently being executed.</param> /// <returns>A string representing the fully-realized MethodName.</returns> public static string GetFunctionMethodName(IEdmOperationImport operationImport, RestierPipelineState restierPipelineState, RestierOperationMethod restierOperation) { return(GetFunctionMethodNameInternal(operationImport.Operation.Name, restierPipelineState, restierOperation)); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="item"></param> /// <param name="pipelineState"></param> /// <returns></returns> private Task InvokeProcessorMethodAsync(SubmitContext context, ChangeSetItem item, RestierPipelineState pipelineState) { var dataModification = (DataModificationItem)item; var expectedMethodName = ConventionBasedMethodNameFactory.GetEntitySetMethodName(dataModification, pipelineState); var expectedMethod = targetApiType.GetQualifiedMethod(expectedMethodName); if (!IsUsable(expectedMethod)) { if (expectedMethod != null) { Trace.WriteLine($"Restier Filter found '{expectedMethodName}' but it is unaccessible due to its protection level. Your method will not be called until you change it to 'protected internal'."); } else { var actualMethodName = expectedMethodName.Replace(dataModification.ExpectedResourceType.Name, dataModification.ResourceSetName); var actualMethod = targetApiType.GetQualifiedMethod(actualMethodName); if (actualMethod != null) { Trace.WriteLine($"BREAKING: Restier Filter expected'{expectedMethodName}' but found '{actualMethodName}'. Your method will not be called until you correct the method name."); } } } else { object target = null; if (!expectedMethod.IsStatic) { target = context.Api; if (target == null || !targetApiType.IsInstanceOfType(target)) { return(Task.WhenAll()); } } var parameters = GetParameters(item); var methodParameters = expectedMethod.GetParameters(); if (ParametersMatch(methodParameters, parameters)) { var result = expectedMethod.Invoke(target, parameters); if (result is Task resultTask) { return(resultTask); } } } return(Task.WhenAll()); }
/// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="item"></param> /// <param name="pipelineState"></param> /// <returns></returns> private Task InvokeProcessorMethodAsync(SubmitContext context, ChangeSetItem item, RestierPipelineState pipelineState) { var dataModification = (DataModificationItem)item; var expectedMethodName = ConventionBasedMethodNameFactory.GetEntitySetMethodName(dataModification, pipelineState); var expectedMethod = targetApiType.GetQualifiedMethod(expectedMethodName); if (expectedMethod == null) { var actualMethodName = expectedMethodName.Replace(dataModification.ExpectedResourceType.Name, dataModification.ResourceSetName); var actualMethod = targetApiType.GetQualifiedMethod(actualMethodName); if (actualMethod != null) { Trace.WriteLine($"Restier Filter expected'{expectedMethodName}' but found '{actualMethodName}'. Your method will not be called until you correct the method name."); } return(Task.CompletedTask); } if (!expectedMethod.IsFamily && !expectedMethod.IsFamilyOrAssembly) { Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it is inaccessible due to its protection level. Your method will not be called until you change it to 'protected internal'."); return(Task.CompletedTask); } if (expectedMethod.ReturnType != typeof(void) && !typeof(Task).IsAssignableFrom(expectedMethod.ReturnType)) { Trace.WriteLine($"Restier Filter found '{expectedMethod}' but it does not return void or a Task. Your method will not be called until you correct the return type."); return(Task.CompletedTask); } object target = null; if (!expectedMethod.IsStatic) { target = context.Api; if (target == null || !targetApiType.IsInstanceOfType(target)) { Trace.WriteLine("The Restier API is of the incorrect type."); return(Task.CompletedTask); } } var parameters = GetParameters(item); var methodParameters = expectedMethod.GetParameters(); if (ParametersMatch(methodParameters, parameters)) { var result = expectedMethod.Invoke(target, parameters); if (result is Task resultTask) { return(resultTask); } } Trace.WriteLine($"Restier Authorizer found '{expectedMethod}', but it has an incorrect number of arguments or the types don't match. The number of arguments should be 1."); return(Task.CompletedTask); }