集成gRPC模块
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
|
||||
namespace ZR.Infrastructure.Resolver;
|
||||
|
||||
public static class AntPathMatcher
|
||||
{
|
||||
public static bool Match(string pattern, string value)
|
||||
{
|
||||
if (pattern == null || value == null)
|
||||
return false;
|
||||
|
||||
if (pattern == "**")
|
||||
return true;
|
||||
|
||||
var patternSegments = pattern.Split('/');
|
||||
var valueSegments = value.Split('/');
|
||||
|
||||
int patternIndex = 0;
|
||||
int valueIndex = 0;
|
||||
int patternWildcardIndex = -1;
|
||||
int valueWildcardIndex = -1;
|
||||
|
||||
while (valueIndex < valueSegments.Length)
|
||||
{
|
||||
if (patternIndex < patternSegments.Length && IsMatch(patternSegments[patternIndex], valueSegments[valueIndex]))
|
||||
{
|
||||
patternIndex++;
|
||||
valueIndex++;
|
||||
}
|
||||
else if (patternWildcardIndex != -1)
|
||||
{
|
||||
patternIndex = patternWildcardIndex + 1;
|
||||
valueIndex = ++valueWildcardIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
while (patternIndex < patternSegments.Length && patternSegments[patternIndex] == "**")
|
||||
{
|
||||
patternIndex++;
|
||||
}
|
||||
|
||||
return patternIndex >= patternSegments.Length && valueIndex >= valueSegments.Length;
|
||||
}
|
||||
|
||||
private static bool IsMatch(string patternSegment, string valueSegment)
|
||||
{
|
||||
if (patternSegment == "*")
|
||||
return true;
|
||||
|
||||
if (patternSegment.StartsWith("{") && patternSegment.EndsWith("}"))
|
||||
return true; // 支持占位符的匹配逻辑
|
||||
|
||||
return string.Equals(patternSegment, valueSegment, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace ZR.Infrastructure.Resolver;
|
||||
|
||||
public class PropertyFilterContractResolver : CamelCasePropertyNamesContractResolver
|
||||
{
|
||||
private readonly HashSet<string> _includedPaths;
|
||||
private readonly HashSet<string> _excludedPaths;
|
||||
|
||||
public PropertyFilterContractResolver(IEnumerable<string> includedPaths, IEnumerable<string> excludedPaths)
|
||||
{
|
||||
_includedPaths = includedPaths.ToHashSet();
|
||||
_excludedPaths = excludedPaths.ToHashSet();
|
||||
}
|
||||
|
||||
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
|
||||
{
|
||||
var properties = base.CreateProperties(type, memberSerialization);
|
||||
|
||||
properties = properties.Where(p => IsIncluded(p) && !IsExcluded(p)).ToList();
|
||||
|
||||
return properties;
|
||||
}
|
||||
|
||||
private bool IsIncluded(JsonProperty property)
|
||||
{
|
||||
if (_includedPaths == null || _includedPaths.Count == 0)
|
||||
return true;
|
||||
|
||||
return _includedPaths.Any(path => AntPathMatcher.Match(path, property.PropertyName));
|
||||
}
|
||||
|
||||
private bool IsExcluded(JsonProperty property)
|
||||
{
|
||||
if (_excludedPaths == null || _excludedPaths.Count == 0)
|
||||
return false;
|
||||
|
||||
return _excludedPaths.Any(path => AntPathMatcher.Match(path, property.PropertyName));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user