private IRpcMethodInfo[] FilterMatchesByCaseSensitiveMethod(RpcRequestSignature requestSignature, Span <IRpcMethodInfo> matches) { //Try to remove ambiguity with case sensitive check IRpcMethodInfo[] caseSensitiveMatches = ArrayPool <IRpcMethodInfo> .Shared.Rent(matches.Length); try { int caseSensitiveCount = 0; for (int i = 0; i < matches.Length; i++) { IRpcMethodInfo m = matches[i]; Memory <char> requestMethodName = requestSignature.GetMethodName(); if (m.Name.Length == requestMethodName.Length) { if (!RpcUtil.NamesMatch(m.Name.AsSpan(), requestMethodName.Span)) { //TODO do we care about the case where 2+ parameters have very similar names and types? continue; } caseSensitiveMatches[caseSensitiveCount++] = m; } } return(caseSensitiveMatches.AsSpan(0, caseSensitiveCount).ToArray()); } finally { ArrayPool <IRpcMethodInfo> .Shared.Return(caseSensitiveMatches, clearArray : false); } }
public void GetMatchingMethod_ListParam_Match_Snake_Case(string parameterNameCase) { DefaultRequestMatcher matcher = this.GetMatcher(); IEnumerable <KeyValuePair <string, RpcParameterType> > parameters = new[] { new KeyValuePair <string, RpcParameterType>(parameterNameCase, RpcParameterType.String) }; string methodName = nameof(MethodMatcherController.SnakeCaseParams); var requestSignature = RpcRequestSignature.Create(methodName, parameters); RpcMethodInfo methodInfo = matcher.GetMatchingMethod(requestSignature); Assert.NotNull(methodInfo); MethodInfo expectedMethodInfo = typeof(MethodMatcherController).GetMethod(methodName) !; Assert.Equal(expectedMethodInfo, methodInfo.MethodInfo); Assert.Single(methodInfo.Parameters); Assert.False(methodInfo.Parameters[0].IsOptional); Assert.Equal(typeof(string), methodInfo.Parameters[0].RawType); Assert.Equal(RpcParameterType.String, methodInfo.Parameters[0].Type); Assert.True(RpcUtil.NamesMatch(methodInfo.Parameters[0].Name, parameterNameCase)); }
private IRpcMethodInfo[] GetMatchingMethods(RpcRequestSignature requestSignature, IReadOnlyList <IRpcMethodInfo> methods) { IRpcMethodInfo[] methodsWithSameName = ArrayPool <IRpcMethodInfo> .Shared.Rent(methods.Count); try { //Case insenstive check for hybrid approach. Will check for case sensitive if there is ambiguity int methodsWithSameNameCount = 0; for (int i = 0; i < methods.Count; i++) { IRpcMethodInfo methodInfo = methods[i]; if (RpcUtil.NamesMatch(methodInfo.Name.AsSpan(), requestSignature.GetMethodName().Span)) { methodsWithSameName[methodsWithSameNameCount++] = methodInfo; } } if (methodsWithSameNameCount < 1) { return(Array.Empty <IRpcMethodInfo>()); } return(this.FilterBySimilarParams(requestSignature, methodsWithSameName.AsSpan(0, methodsWithSameNameCount))); } finally { ArrayPool <IRpcMethodInfo> .Shared.Return(methodsWithSameName, clearArray : false); } }
public void MatchMethodNamesCulturallyInvariantTest() { var previousCulture = System.Globalization.CultureInfo.CurrentCulture; // Switch to a locale that would result in lowercasing 'I' to // U+0131, if not done with invariant culture. System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("az"); var methodInfo = "IsLunchTime"; var requestMethodName = "isLunchtIme"; Assert.True(RpcUtil.NamesMatch(methodInfo, requestMethodName)); System.Globalization.CultureInfo.CurrentCulture = previousCulture; }
public async Task MatchMethodNamesTest(string methodInfo, string requestMethodName) { Assert.True(RpcUtil.NamesMatch(methodInfo, requestMethodName)); }
private bool ParametersMatch(RpcRequestSignature requestSignature, IReadOnlyList <IRpcParameterInfo> parameters) { if (!requestSignature.HasParameters) { return(parameters == null || !parameters.Any(p => !p.IsOptional)); } int parameterCount = 0; if (requestSignature.IsDictionary) { foreach ((Memory <char> name, RpcParameterType type) in requestSignature.ParametersAsDict) { bool found = false; for (int paramIndex = 0; paramIndex < parameters.Count; paramIndex++) { IRpcParameterInfo parameter = parameters[paramIndex]; if (!RpcUtil.NamesMatch(parameter.Name.AsSpan(), name.Span) || !RpcParameterUtil.TypesCompatible(parameter.Type, type)) { continue; } found = true; break; } if (!found) { return(false); } parameterCount++; } } else { foreach (RpcParameterType parameterType in requestSignature.ParametersAsList) { if (parameters.Count <= parameterCount) { return(false); } IRpcParameterInfo info = parameters[parameterCount]; if (!RpcParameterUtil.TypesCompatible(info.Type, parameterType)) { return(false); } parameterCount++; } for (int i = parameterCount; i < parameters.Count; i++) { //Only if the last parameters in the method are optional does the request match //Will be skipped if they are equal length if (!parameters[i].IsOptional) { return(false); } } } if (parameterCount != parameters.Count) { return(false); } return(true); }
public void NotMatchMethodNamesTest(string methodInfo, string requestMethodName) { Assert.False(RpcUtil.NamesMatch(methodInfo, requestMethodName)); }