Skip to content

Commit

Permalink
Don't use null in AST
Browse files Browse the repository at this point in the history
  • Loading branch information
ltrzesniewski committed Jan 26, 2024
1 parent be01d31 commit da966a7
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,13 @@ public async Task should_generate_attributes_on_parameters()
code.ShouldContain("] int foo");
}

[Test]
public void should_handle_missing_data()
{
_ = Generate(new MessageDefinition());
_ = Generate(new MessageDefinition { Parameters = { new ParameterDefinition() } });
}

protected override string SnapshotExtension => "cs";

protected override string GenerateRaw(ParsedContracts contracts)
Expand Down
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Ast/AttributeDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class AttributeDefinition : AstNode
{
private TypeName _typeName = default!;
private TypeName _typeName = TypeName.Empty;

public TypeName TypeName
{
Expand Down
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Ast/EnumDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class EnumDefinition : AstNode, IMemberNode
{
private MemberOptions? _options;

public string Name { get; set; } = default!;
public string Name { get; set; } = string.Empty;
public TypeName UnderlyingType { get; set; } = "int";
public AccessModifier AccessModifier { get; set; }
public AttributeSet Attributes { get; } = new();
Expand Down
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Ast/EnumMemberDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class EnumMemberDefinition : AstNode, INamedNode
{
public string Name { get; set; } = default!;
public string Name { get; set; } = string.Empty;
public string? Value { get; set; }
public AttributeSet Attributes { get; } = new();
internal int? ProtoValue { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Ast/GenericConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Abc.Zebus.MessageDsl.Ast;

public class GenericConstraint : AstNode
{
public string GenericParameterName { get; set; } = default!;
public string GenericParameterName { get; set; } = string.Empty;

public bool IsClass { get; set; }
public bool IsStruct { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/Abc.Zebus.MessageDsl/Ast/MessageDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class MessageDefinition : AstNode, IClassNode
{
private MemberOptions? _options;

public string Name { get; set; } = default!;
public string Name { get; set; } = string.Empty;
public AccessModifier AccessModifier { get; set; }
public InheritanceModifier InheritanceModifier { get; set; }
public IList<string> GenericParameters { get; } = new List<string>();
Expand Down
4 changes: 2 additions & 2 deletions src/Abc.Zebus.MessageDsl/Ast/ParameterDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public class ParameterDefinition : AstNode, INamedNode
{
public int Tag { get; set; }
public string Name { get; set; } = default!;
public TypeName Type { get; set; } = default!;
public string Name { get; set; } = string.Empty;
public TypeName Type { get; set; } = TypeName.Empty;
public bool IsMarkedOptional { get; set; }
public string? DefaultValue { get; set; }
public bool IsWritableProperty { get; set; }
Expand Down
2 changes: 2 additions & 0 deletions src/Abc.Zebus.MessageDsl/Ast/TypeName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public sealed class TypeName : IEquatable<TypeName>

private static readonly HashSet<string> _csharpNonTypeKeywords = CSharpSyntax.EnumerateCSharpKeywords().Except(_aliasTypeMap.Keys).ToHashSet();

internal static TypeName Empty { get; } = new(null);

static TypeName()
{
_clrTypeToAlias = new Dictionary<string, string>();
Expand Down
8 changes: 6 additions & 2 deletions src/Abc.Zebus.MessageDsl/Generator/GeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ protected string GeneratedOutput()
=> _stringBuilder.ToString();

protected static string ParameterCase(string s)
=> char.ToLowerInvariant(s[0]) + s.Substring(1);
=> !string.IsNullOrEmpty(s)
? char.ToLowerInvariant(s[0]) + s.Substring(1)
: string.Empty;

protected static string MemberCase(string s)
=> char.ToUpperInvariant(s[0]) + s.Substring(1);
=> !string.IsNullOrEmpty(s)
? char.ToUpperInvariant(s[0]) + s.Substring(1)
: string.Empty;

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly")]
public void Dispose()
Expand Down

0 comments on commit da966a7

Please sign in to comment.