Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #1166 Exception with Code generator when an attribute name has no alfanumeric characters #1190

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ public string GetNewServiceArgumentsTypeExpression()

return $"new {TypeName} {{ { string.Join(", ", propertyInitializers) } }}";
}


/// <summary>
/// If the identifier matches a C# keyword we need to prefix it with a @
/// </summary>
private static string EscapeIfRequired(string name)
{
var match = SyntaxFacts.GetKeywordKind(name) != SyntaxKind.None ||
SyntaxFacts.GetContextualKeywordKind(name) != SyntaxKind.None;

return match ? "@" + name : name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public static string ToValidCSharpIdentifier(this string name)

name = Regex.Replace(name, "[^a-zA-Z0-9_]+", "", RegexOptions.Compiled);

if (char.IsAsciiDigit(name[0]))
if (name.Length == 0 || char.IsAsciiDigit(name[0]))
name = "_" + name;

return name;
}

public static string ToPascalCase(this string str)
{
var build = new StringBuilder(str.Length);
Expand Down Expand Up @@ -55,4 +55,4 @@ public static string ToCamelCase(this string str)

return char.ToLowerInvariant(camelCaseStr[0]) + camelCaseStr[1..];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Text.Json;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NetDaemon.Client.HomeAssistant.Model;
using NetDaemon.HassModel.CodeGenerator;
using NetDaemon.HassModel.CodeGenerator.Model;
Expand Down Expand Up @@ -176,6 +177,38 @@ public void Run(IHaContext ha)
CodeGenTestHelper.AssertCodeCompiles(generatedCode.ToString(), appCode);
}

[Fact]
public void TestAttributeWithoutValidCSharpChars()
{
// Numeric entities should be generated for input_numbers and sensors with a unit_of_measurement attribute
var entityStates = new HassState[] {
new()
{
EntityId = "InputNumber.TargetTemperature",
AttributesJson = JsonSerializer.Deserialize<JsonElement>("""{"----" : "bogus", "&^%" : "more bugus"} """),
}
};
var generatedCode = CodeGenTestHelper.GenerateCompilationUnit(_settings, entityStates, Array.Empty<HassServiceDomain>());
var appCode = """
using NetDaemon.HassModel.Entities;
using NetDaemon.HassModel;
using RootNameSpace;
using System.Collections.Generic;

public class Root
{
public void Run(Entities entities)
{
InputNumberEntity targetTempEntity = entities.InputNumber.TargetTemperature;
string? bogus1 = targetTempEntity.Attributes?.__0;
string? bogus2 = targetTempEntity.Attributes?.__1;
}
}
""";
CodeGenTestHelper.AssertCodeCompiles(generatedCode.ToString(), appCode);
}


[Fact]
public void TestNumberExtensionMethodGeneration()
{
Expand Down
Loading