Skip to content

Commit

Permalink
Better check for valid c# identifier, (#112)
Browse files Browse the repository at this point in the history
fixes net-daemon/netdaemon #111

Co-authored-by: John Vert <john@groopit.co>
  • Loading branch information
jvert and jvert committed May 6, 2020
1 parent 45edbcf commit 0d421d9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/DaemonRunner/DaemonRunner/Service/App/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ public class CodeGenerator
{
var (fluent, fluentInterface) = _FluentApiMapper[domain];
var name = entity[(entity.IndexOf(".") + 1)..];
// Make sure the name is valid
if (long.TryParse(name, out _))
// Quick check to make sure the name is a valid C# identifier. Should really check to make
// sure it doesn't collide with a reserved keyword as well.
if (!char.IsLetter(name[0]) && (name[0] != '_'))
{
name = "e_" + name;
}

var propertyCode = $@"public {fluentInterface} {name.ToCamelCase()} => _app.{fluent}(""{entity}"");";
var propDeclaration = CSharpSyntaxTree.ParseText(propertyCode).GetRoot().ChildNodes().OfType<PropertyDeclarationSyntax>().FirstOrDefault();
Expand Down

0 comments on commit 0d421d9

Please sign in to comment.