Skip to content

Commit

Permalink
fix #39
Browse files Browse the repository at this point in the history
  • Loading branch information
zdyu committed Jan 4, 2024
1 parent 4185c0d commit 5392ac3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 17 deletions.
44 changes: 32 additions & 12 deletions src/Anotation/Value.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public Value(string _value)
/// <returns></returns>
public override object ResolveParameter(ParameterInfo parameter, IComponentContext context)
{
return parameter == null ? null : Resolve(context, parameter.Member.DeclaringType, parameter.ParameterType, parameter.Name);
return parameter == null
? null
: Resolve(context, parameter.Member.DeclaringType, parameter.ParameterType, parameter.Name);
}

/// <summary>
Expand All @@ -96,7 +98,8 @@ public override bool CanResolveParameter(ParameterInfo parameter, IComponentCont
/// <param name="parameter"></param>
/// <param name="context"></param>
/// <returns></returns>
internal object ResolveParameterWithConfiguration(AutoConfigurationDetail detail, ParameterInfo parameter, IComponentContext context)
internal object ResolveParameterWithConfiguration(AutoConfigurationDetail detail, ParameterInfo parameter,
IComponentContext context)
{
return Resolve(context, parameter.Member.DeclaringType, parameter.ParameterType, parameter.Name, detail);
}
Expand Down Expand Up @@ -142,7 +145,8 @@ internal object Resolve(IComponentContext context, Type classType, Type memberTy
//判断类型是否是IValue
if ((typeof(IObjectFactory).IsAssignableFrom(memberType)))
{
return context.Resolve<ObjectBeanFactory>().CreateValueFactory(this, memberType, classType, parameterInfo, autoConfigurationDetail);
return context.Resolve<ObjectBeanFactory>().CreateValueFactory(this, memberType, classType,
parameterInfo, autoConfigurationDetail);
}

if (!UseSpel)
Expand All @@ -153,6 +157,14 @@ internal object Resolve(IComponentContext context, Type classType, Type memberTy
{
return section.Get(memberType);
}
else if (IgnoreUnresolvablePlaceholders)
{
return null;
}
else
{
throw new Exception($"Could not resolve placeholder '{value}' with [UseSpel:false].");
}
}

//先把 ${} 的 placehoder 全部替换掉
Expand All @@ -163,9 +175,10 @@ internal object Resolve(IComponentContext context, Type classType, Type memberTy
}
catch (Exception ex)
{
throw new DependencyResolutionException($"Value set error,can not resolve class type:{classType.FullName} =====>" +
$" {parameterInfo ?? memberType.Name} "
+ (!string.IsNullOrEmpty(this.value) ? $",with value:[{this.value}]" : ""), ex);
throw new DependencyResolutionException(
$"Value set error,can not resolve class type:{classType.FullName} =====>" +
$" {parameterInfo ?? memberType.Name} "
+ (!string.IsNullOrEmpty(this.value) ? $",with value:[{this.value}]" : ""), ex);
}
}

Expand Down Expand Up @@ -196,10 +209,12 @@ internal static object ResolveSpel(
{
return null;
}

int startIndex = parameterValue.ToString().IndexOf("#{", StringComparison.Ordinal);
if (startIndex != -1)
{
int endIndex = parameterValue.ToString().LastIndexOf(DefaultPlaceholderSuffix, StringComparison.Ordinal);
int endIndex = parameterValue.ToString()
.LastIndexOf(DefaultPlaceholderSuffix, StringComparison.Ordinal);
if (endIndex != -1)
{
Dictionary<string, object> vars = new Dictionary<string, object>
Expand Down Expand Up @@ -243,21 +258,25 @@ internal static object ResolveSpel(
#region ${xxx} 代表从配置源里面 获取属性名称为xxx的值

static object ResolveEmbeddedValue(EnvironmentVariableMode mode,
IComponentContext context, Type classType, string strVal, AutoConfigurationDetail autoConfigurationDetail = null, bool ignoreFail = false)
IComponentContext context, Type classType, string strVal,
AutoConfigurationDetail autoConfigurationDetail = null, bool ignoreFail = false)
{
int startIndex = strVal.IndexOf(DefaultPlaceholderPrefix, StringComparison.Ordinal);
while (startIndex != -1)
{
int endIndex = strVal.IndexOf(DefaultPlaceholderSuffix, startIndex + DefaultPlaceholderPrefix.Length, StringComparison.Ordinal);
int endIndex = strVal.IndexOf(DefaultPlaceholderSuffix, startIndex + DefaultPlaceholderPrefix.Length,
StringComparison.Ordinal);
if (endIndex != -1)
{
int pos = startIndex + DefaultPlaceholderPrefix.Length;
string placeholder = strVal.Substring(pos, endIndex - pos);
string resolvedValue = ResolvePlaceholder(mode, context, classType, placeholder, autoConfigurationDetail);
string resolvedValue =
ResolvePlaceholder(mode, context, classType, placeholder, autoConfigurationDetail);
if (resolvedValue != null)
{
strVal = strVal.Substring(0, startIndex) + resolvedValue + strVal.Substring(endIndex + 1);
startIndex = strVal.IndexOf(DefaultPlaceholderPrefix, startIndex + resolvedValue.Length, StringComparison.Ordinal);
startIndex = strVal.IndexOf(DefaultPlaceholderPrefix, startIndex + resolvedValue.Length,
StringComparison.Ordinal);
}
else if (ignoreFail)
{
Expand Down Expand Up @@ -287,7 +306,8 @@ static object ResolveEmbeddedValue(EnvironmentVariableMode mode,
/// <param name="placeholder">key</param>
/// <param name="autoConfigurationDetail"></param>
/// <returns></returns>
static string ResolvePlaceholder(EnvironmentVariableMode mode, IComponentContext context, Type classType, string placeholder,
static string ResolvePlaceholder(EnvironmentVariableMode mode, IComponentContext context, Type classType,
string placeholder,
AutoConfigurationDetail autoConfigurationDetail = null)
{
string propertyValue = null;
Expand Down
6 changes: 3 additions & 3 deletions src/Autofac.Annotation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<RootNamespace>Autofac.Annotation</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>4.4.4</Version>
<AssemblyVersion>4.4.4.0</AssemblyVersion>
<Version>4.4.5</Version>
<AssemblyVersion>4.4.5.0</AssemblyVersion>
<Authors>zdyu</Authors>
<PackageVersion>4.4.4</PackageVersion>
<PackageVersion>4.4.5</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

Expand Down
6 changes: 5 additions & 1 deletion test/file/testissue39.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"abc": "abc"
"abc": "abc" ,
"ValueInjectModel1": {
"Name": "yuzd",
"Age": 20
}
}
22 changes: 21 additions & 1 deletion test/issue39/IgnoreUnresolvablePlaceholdersTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Xunit;
using Autofac.Core;
using Xunit;

namespace Autofac.Annotation.Test.issue39;

Expand All @@ -15,6 +16,12 @@ public void Test()
var _container = builder.Build();

var h = _container.Resolve<MyConfig>();

Assert.Equal("abc", h.Abc);
Assert.Null(h.Test);
Assert.Null(h.Test2);

Assert.Throws<DependencyResolutionException>(() => _container.Resolve<MyConfig2>());
}
}

Expand All @@ -26,4 +33,17 @@ public class MyConfig

[Value("${test}", IgnoreUnresolvablePlaceholders = true)]
public string Test { get; set; }


// 设置了usespel=false则采用原生的方式去拿, 因为没有test并且设置了Ignore 所以不会报错
[Value("test", UseSpel = false, IgnoreUnresolvablePlaceholders = true)]
public string Test2 { get; set; }
}

[Component]
[PropertySource("/file/testissue39.json")]
public class MyConfig2
{
// 设置了usespel=false则采用原生的方式去拿, 因为没有test 会报错
[Value("test", UseSpel = false)] public string Test2 { get; set; }
}

0 comments on commit 5392ac3

Please sign in to comment.