Skip to content

Commit

Permalink
implements Record class style
Browse files Browse the repository at this point in the history
  • Loading branch information
ili committed Mar 5, 2018
1 parent bda8b16 commit c0699cb
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/.editorconfig
@@ -0,0 +1,10 @@
; What is EditorConfig? http://editorconfig.org/

root = true

; general settings
[*]
indent_style = space
indent_size = 4
end_of_line = crlf
insert_final_newline = false
Expand Up @@ -1401,5 +1401,58 @@ public async Task When_definition_contains_datetime_converter_should_not_be_adde
Assert.DoesNotContain(@"class DateFormatConverter", code);
Assert.DoesNotContain(@"[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]", code);
}

[Fact]
public async Task When_record_no_setter_in_class_and_constructor_provided()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<Address>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record
});

//// Act
var output = generator.GenerateFile("Address");

//// Assert
Assert.Contains (@"public string Street { get; }", output);
Assert.DoesNotContain(@"public string Street { get; set; }", output);

Assert.Contains("public Address(string Street, string City)", output);
Assert.Contains("public Address(string Street, string City)", output);
}

public abstract class AbstractAddress
{
[JsonProperty("city")]
[DefaultValue("Innsmouth")]
public string City { get; set; }

[JsonProperty("street")]
public string Street { get; set; }
}

[Fact]
public async Task When_class_is_abstract_constructor_is_protected_for_record()
{
//// Arrange
var schema = await JsonSchema4.FromTypeAsync<AbstractAddress>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record,
});

//// Act
var output = generator.GenerateFile("AbstractAddress");

//// Assert
Assert.Contains (@"public string Street { get; }", output);
Assert.DoesNotContain(@"public string Street { get; set; }", output);

Assert.Contains("protected AbstractAddress(string street, string city = \"Innsmouth\")", output);
}
}
}
5 changes: 4 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp/CSharpClassStyle.cs
Expand Up @@ -20,6 +20,9 @@ public enum CSharpClassStyle
Inpc,

/// <summary>Generates classes implementing the Prism base class.</summary>
Prism
Prism,

/// <summary>Generates Records - read only POCOs (Plain Old C# Objects).</summary>
Record
}
}
Expand Up @@ -70,6 +70,9 @@ public class ClassTemplateModel : ClassTemplateModelBase
/// <summary>Gets a value indicating whether the class style is Prism.</summary>
public bool RenderPrism => _settings.ClassStyle == CSharpClassStyle.Prism;

/// <summary>Gets a value indicating whether the class style is Record.</summary>
public bool RenderRecord => _settings.ClassStyle == CSharpClassStyle.Record;

/// <summary>Gets a value indicating whether to render ToJson() and FromJson() methods.</summary>
public bool GenerateJsonMethods => _settings.GenerateJsonMethods;

Expand Down
Expand Up @@ -23,6 +23,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="Templates\Class.Body.liquid" />
<None Remove="Templates\Class.Constructor.Record.liquid" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\Class.Annotations.liquid">
Expand All @@ -31,6 +32,7 @@
</EmbeddedResource>
<EmbeddedResource Include="Templates\Class.Body.liquid" />
<EmbeddedResource Include="Templates\Class.Constructor.liquid" />
<EmbeddedResource Include="Templates\Class.Constructor.Record.liquid" />
<EmbeddedResource Include="Templates\Class.FromJson.liquid">
<CustomToolNamespace>
</CustomToolNamespace>
Expand Down
@@ -0,0 +1,9 @@
{% assign skipComma = true -%}
{% assign sortedProperties = (Properties | sort: "HasDefaultValue") -%}
[Newtonsoft.Json.JsonConstructor]
{% if IsAbstract %}protected{% else %}public{% endif %} {{ClassName}}({% for property in sortedProperties -%}{% if skipComma -%}{% assign skipComma = false %}{% else %}, {% endif -%} {{ property.Type }} {{ property.Name }}{% if property.HasDefaultValue == true %} = {{ property.DefaultValue }}{% endif %}{% endfor -%})
{
{% for property in Properties -%}
this.{{property.PropertyName}} = @{{property.Name}};
{% endfor -%}
}
6 changes: 5 additions & 1 deletion src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid
Expand Up @@ -18,6 +18,10 @@

{% endif -%}
{% template Class.Constructor %}
{% if RenderRecord == true -%}
{% template Class.Constructor.Record %}

{% endif -%}
{% for property in Properties -%}
{% if property.HasDescription -%}
/// <summary>{{ property.Description | csharpdocs }}</summary>
Expand All @@ -42,7 +46,7 @@
[Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]
{% endif -%}
{% template Class.Property.Annotations %}
public {{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter %}set; {% endif %}}{% if property.HasDefaultValue %} = {{ property.DefaultValue }};{% endif %}
public {{ property.Type }} {{ property.PropertyName }}{% if RenderInpc == false and RenderPrism == false %} { get; {% if property.HasSetter and RenderRecord == false %}set; {% endif %}}{% if property.HasDefaultValue %} = {{ property.DefaultValue }};{% endif %}
{% else %}
{
get { return {{ property.FieldName }}; }
Expand Down
Expand Up @@ -22,7 +22,7 @@
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DotLiquid" Version="2.0.200" />
<PackageReference Include="DotLiquid" Version="2.0.254" />
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.3'">
Expand Down

0 comments on commit c0699cb

Please sign in to comment.