Skip to content

Commit 6f72b3e

Browse files
authored
[Parser] Default Naming Convention for Development (#4676)
1 parent d7272aa commit 6f72b3e

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

src/OpenTelemetry.AutoInstrumentation/Configurations/FileBasedConfiguration/Parser/EnvVarTypeConverter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ internal partial class EnvVarTypeConverter : IYamlTypeConverter
1515
[
1616
typeof(string),
1717
typeof(int),
18+
typeof(uint),
1819
typeof(long),
1920
typeof(float),
2021
typeof(double),
2122
typeof(bool),
2223
typeof(int?),
24+
typeof(uint?),
2325
typeof(object),
2426
];
2527

@@ -54,11 +56,13 @@ public bool Accepts(Type type)
5456
{
5557
Type t when t == typeof(string) => replacedValue,
5658
Type t when t == typeof(int) => int.Parse(replacedValue),
59+
Type t when t == typeof(uint) => uint.Parse(replacedValue),
5760
Type t when t == typeof(long) => long.Parse(replacedValue),
5861
Type t when t == typeof(float) => float.Parse(replacedValue, CultureInfo.InvariantCulture),
5962
Type t when t == typeof(double) => double.Parse(replacedValue, CultureInfo.InvariantCulture),
6063
Type t when t == typeof(bool) => bool.Parse(replacedValue),
6164
Type t when t == typeof(int?) => int.Parse(replacedValue),
65+
Type t when t == typeof(uint?) => uint.Parse(replacedValue),
6266
Type t when t == typeof(object) => replacedValue,
6367
_ => throw new NotSupportedException($"Type {type.FullName} is not supported by the converter")
6468
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Vendors.YamlDotNet.Serialization;
5+
using Vendors.YamlDotNet.Serialization.Utilities;
6+
7+
namespace OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration.Parser;
8+
9+
internal sealed class OtelDefaultNamingConvention : INamingConvention
10+
{
11+
public static readonly INamingConvention Instance = new OtelDefaultNamingConvention();
12+
13+
private OtelDefaultNamingConvention()
14+
{
15+
}
16+
17+
public string Apply(string value)
18+
{
19+
// If the string ends with "Development", replace the ending with "/development"
20+
const string target = "Development";
21+
if (value.EndsWith(target, StringComparison.Ordinal) && value.Length > target.Length)
22+
{
23+
var prefix = value.Substring(0, value.Length - target.Length);
24+
value = prefix + "/development";
25+
}
26+
27+
return value.FromCamelCase("_");
28+
}
29+
30+
public string Reverse(string value)
31+
{
32+
var result = value.ToPascalCase();
33+
return result;
34+
}
35+
}

src/OpenTelemetry.AutoInstrumentation/Configurations/FileBasedConfiguration/Parser/Parser.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using Vendors.YamlDotNet.Serialization;
5-
using Vendors.YamlDotNet.Serialization.NamingConventions;
65
using Vendors.YamlDotNet.Serialization.NodeDeserializers;
76

87
namespace OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration.Parser;
@@ -14,7 +13,7 @@ public static T ParseYaml<T>(string filePath)
1413
var deserializer = new DeserializerBuilder()
1514
.WithNodeDeserializer(existing => new ConditionalDeserializer(existing), s => s.InsteadOf<NullNodeDeserializer>())
1615
.WithTypeConverter(new EnvVarTypeConverter())
17-
.WithNamingConvention(UnderscoredNamingConvention.Instance)
16+
.WithNamingConvention(OtelDefaultNamingConvention.Instance)
1817
.IgnoreUnmatchedProperties()
1918
.Build();
2019

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
under_score: true
2+
under_score/development: true

test/OpenTelemetry.AutoInstrumentation.Tests/Configurations/FileBased/Files/TestGeneralFile.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ file_format: "1.0-rc.1"
22
disabled: false
33
fail_fast: false
44
flush_on_unhandled_exception: false
5-
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
using Xunit;
5+
using YamlParser = OpenTelemetry.AutoInstrumentation.Configurations.FileBasedConfiguration.Parser.Parser;
6+
7+
namespace OpenTelemetry.AutoInstrumentation.Tests.Configurations.FileBased.Parser;
8+
9+
public class DefaultNamingConventionTests
10+
{
11+
[Fact]
12+
public void Test_UnderScore()
13+
{
14+
var config = YamlParser.ParseYaml<DefaultNamingConvention>("Configurations/FileBased/Files/DefaultNamingConvention.yaml");
15+
16+
Assert.True(config.UnderScore);
17+
Assert.True(config.UnderScoreDevelopment);
18+
}
19+
20+
public class DefaultNamingConvention
21+
{
22+
public bool UnderScore { get; set; } = false;
23+
24+
public bool UnderScoreDevelopment { get; set; } = false;
25+
}
26+
}

test/OpenTelemetry.AutoInstrumentation.Tests/OpenTelemetry.AutoInstrumentation.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
<None Update="Configurations\FileBased\Files\TestResourceFileEnvVars.yaml">
6868
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
6969
</None>
70+
<None Update="Configurations\FileBased\Files\DefaultNamingConvention.yaml">
71+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
72+
</None>
7073
</ItemGroup>
7174

7275
</Project>

0 commit comments

Comments
 (0)