Skip to content

Commit bef1a50

Browse files
author
ladeak
committed
Incremental compilesIncremental re-write part 1
1 parent 9ef122f commit bef1a50

File tree

54 files changed

+1083
-1490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1083
-1490
lines changed

sample/AspNetCoreMinimal.Entities/AspNetCoreMinimal.Entities.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<IsPackable>false</IsPackable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>

sample/AspNetCoreMinimal.Entities/Entities.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text.Json.Serialization;
1+
using System.Text.Json.Serialization;
42
using LaDeak.JsonMergePatch.Abstractions;
53

64
namespace AspNetCoreMinimal.Entities;

sample/AspNetCoreMinimal/AspNetCoreMinimal.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<IsPackable>false</IsPackable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>

sample/AspNetCoreMinimal/Controllers/SampleController.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Net.Http;
4-
using System.Text.Json;
5-
using System.Threading.Tasks;
1+
using System.Text.Json;
62
using AspNetCoreMinimal.Entities;
73
using LaDeak.JsonMergePatch.Abstractions;
84
using LaDeak.JsonMergePatch.Http;

sample/AspNetCoreMinimal/Program.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
using System.Text.Json.Serialization;
22
using LaDeak.JsonMergePatch.AspNetCore;
3-
using Microsoft.AspNetCore.Builder;
4-
using Microsoft.Extensions.DependencyInjection;
5-
using Microsoft.Extensions.Hosting;
63

74
var builder = WebApplication.CreateBuilder(args);
85

96
var mvcBuilder = builder.Services.AddControllers().AddMvcOptions(options =>
107
{
11-
LaDeak.JsonMergePatch.Abstractions.JsonMergePatchOptions.Repository = LaDeak.JsonMergePatch.Generated.SafeAspNetCoreMinimal.Entities.TypeRepository.Instance;
8+
LaDeak.JsonMergePatch.Abstractions.JsonMergePatchOptions.Repository = LaDeak.JsonMergePatch.Generated.Safe.TypeRepository.Instance;
129
var jsonOptions = new Microsoft.AspNetCore.Http.Json.JsonOptions();
13-
jsonOptions.SerializerOptions.AddContext<SampleJsonContext>();
10+
jsonOptions.SerializerOptions.TypeInfoResolver = SampleJsonContext.Default;
1411
options.InputFormatters.Insert(0, new JsonMergePatchInputReader(jsonOptions));
1512
});
1613
builder.Services.AddHttpClient();
17-
18-
1914
var app = builder.Build();
20-
2115
app.UseHttpsRedirection();
22-
2316
app.UseAuthorization();
24-
2517
app.MapControllers();
26-
2718
app.Run();
2819

29-
3020
[JsonSerializable(typeof(LaDeak.JsonMergePatch.Generated.SafeAspNetCoreMinimal.Entities.WeatherForecastWrapped))]
3121
[JsonSerializable(typeof(LaDeak.JsonMergePatch.Generated.SafeAspNetCoreMinimal.Entities.CitiesDataWrapped))]
3222
public partial class SampleJsonContext : JsonSerializerContext

sample/ConsoleApp/ConsoleApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<OutputType>Exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<IsPackable>false</IsPackable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>

sample/ConsoleApp/Program.cs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,34 @@
1-
using System;
2-
using System.Net.Http;
3-
using System.Threading.Tasks;
4-
using ConsoleAppLibrary;
1+
using ConsoleAppLibrary;
52
using LaDeak.JsonMergePatch.Abstractions;
63
using LaDeak.JsonMergePatch.Http;
74

8-
namespace ReadJsonPatchAsync
5+
namespace ReadJsonPatchAsync;
6+
7+
public class Program
98
{
10-
public class Program
9+
public class WeatherForecast
1110
{
12-
public class WeatherForecast
13-
{
14-
public DateTime Date { get; set; }
15-
public int Temp { get; set; }
16-
public string Summary { get; set; }
17-
}
11+
public DateTime Date { get; set; }
12+
public int Temp { get; set; }
13+
public string Summary { get; set; }
14+
}
1815

19-
public static async Task Main(string[] args)
20-
{
21-
LaDeak.JsonMergePatch.Abstractions.JsonMergePatchOptions.Repository = LaDeak.JsonMergePatch.Generated.SafeConsoleApp.TypeRepository.Instance.Extend(LaDeak.JsonMergePatch.Generated.SafeConsoleAppLibrary.TypeRepository.Instance);
22-
await ReadAsJsonMergePatchAsync();
23-
}
16+
public static async Task Main(string[] args)
17+
{
18+
LaDeak.JsonMergePatch.Abstractions.JsonMergePatchOptions.Repository = LaDeak.JsonMergePatch.Generated.SafeConsoleApp.TypeRepository.Instance.Extend(LaDeak.JsonMergePatch.Generated.SafeConsoleAppLibrary.TypeRepository.Instance);
19+
await ReadAsJsonMergePatchAsync();
20+
}
2421

25-
public static async Task ReadAsJsonMergePatchAsync()
26-
{
27-
var httpClient = new HttpClient();
28-
var response = await httpClient.GetAsync("https://localhost:5001/Sample/Weather", HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
29-
var responseData = await response.Content.ReadJsonPatchAsync<WeatherForecast>().ConfigureAwait(false);
30-
var target = new WeatherForecast() { Date = DateTime.UtcNow, Summary = "Sample weather forecast", Temp = 24 };
31-
var result = responseData.ApplyPatch(target);
32-
Console.WriteLine($"Patched: Date={result.Date}, Summary={result.Summary}, Temp={result.Temp}");
22+
public static async Task ReadAsJsonMergePatchAsync()
23+
{
24+
var httpClient = new HttpClient();
25+
var response = await httpClient.GetAsync("https://localhost:5001/Sample/Weather", HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
26+
var responseData = await response.Content.ReadJsonPatchAsync<WeatherForecast>().ConfigureAwait(false);
27+
var target = new WeatherForecast() { Date = DateTime.UtcNow, Summary = "Sample weather forecast", Temp = 24 };
28+
var result = responseData.ApplyPatch(target);
29+
Console.WriteLine($"Patched: Date={result.Date}, Summary={result.Summary}, Temp={result.Temp}");
3330

34-
var client = new Client();
35-
await client.ReadAsJsonMergePatchAsync();
36-
}
31+
var client = new Client();
32+
await client.ReadAsJsonMergePatchAsync();
3733
}
3834
}

sample/ConsoleAppLibrary/ConsoleAppLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<IsPackable>false</IsPackable>
6+
<ImplicitUsings>true</ImplicitUsings>
67
</PropertyGroup>
78

89
<ItemGroup>

sample/ConsoleAppLibrary/Sample.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
using System;
2-
using System.Net.Http;
3-
using System.Threading.Tasks;
4-
using LaDeak.JsonMergePatch.Http;
1+
using LaDeak.JsonMergePatch.Http;
52

6-
namespace ConsoleAppLibrary
3+
namespace ConsoleAppLibrary;
4+
5+
public class DeviceData
76
{
8-
public class DeviceData
9-
{
10-
public double Watts { get; set; }
11-
public string Name { get; set; }
12-
}
7+
public double Watts { get; set; }
8+
public string Name { get; set; }
9+
}
1310

14-
public class Client
11+
public class Client
12+
{
13+
public async Task ReadAsJsonMergePatchAsync()
1514
{
16-
public async Task ReadAsJsonMergePatchAsync()
17-
{
18-
var httpClient = new HttpClient();
19-
var response = await httpClient.GetAsync("https://localhost:5001/Sample/DeviceData", HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
20-
var responseData = await response.Content.ReadJsonPatchAsync<DeviceData>().ConfigureAwait(false);
21-
var target = new DeviceData() { Watts = 5, Name = "phone" };
22-
var result = responseData.ApplyPatch(target);
23-
Console.WriteLine($"Patched: Name={result.Name}, Watts={result.Watts}");
24-
}
15+
var httpClient = new HttpClient();
16+
var response = await httpClient.GetAsync("https://localhost:5001/Sample/DeviceData", HttpCompletionOption.ResponseHeadersRead).ConfigureAwait(false);
17+
var responseData = await response.Content.ReadJsonPatchAsync<DeviceData>().ConfigureAwait(false);
18+
var target = new DeviceData() { Watts = 5, Name = "phone" };
19+
var result = responseData.ApplyPatch(target);
20+
Console.WriteLine($"Patched: Name={result.Name}, Watts={result.Watts}");
2521
}
2622
}

src/JsonMergePatch.Abstractions/ITypeRepository.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Diagnostics.CodeAnalysis;
1+
using System.Diagnostics.CodeAnalysis;
42

53
namespace LaDeak.JsonMergePatch.Abstractions;
64

src/JsonMergePatch.Abstractions/ITypeRepositoryExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
5-
namespace LaDeak.JsonMergePatch.Abstractions;
1+
namespace LaDeak.JsonMergePatch.Abstractions;
62

73
public static class ITypeRepositoryExtensions
84
{

src/JsonMergePatch.Abstractions/JsonMergePatch.Abstractions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>netstandard2.1</TargetFramework>
55
<RootNamespace>LaDeak.JsonMergePatch.Abstractions</RootNamespace>
66
<Nullable>enable</Nullable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
</Project>

src/JsonMergePatch.Abstractions/Patch.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public abstract class Patch<T>
77
/// <summary>
88
/// List of properties used by the request.
99
/// </summary>
10-
protected bool[] Properties;
10+
protected bool[] Properties = [];
1111

1212
/// <summary>
1313
/// Applies updates on input type using Json Merge Patch rules.

src/JsonMergePatch.Abstractions/PatchableAttribute.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
3-
namespace LaDeak.JsonMergePatch.Abstractions;
1+
namespace LaDeak.JsonMergePatch.Abstractions;
42

53
[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class)]
64
public class PatchableAttribute : Attribute

src/JsonMergePatch.AspNetCore/JsonMergePatch.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<RootNamespace>LaDeak.JsonMergePatch.AspNetCore</RootNamespace>
66
<Nullable>enable</Nullable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>

src/JsonMergePatch.AspNetCore/JsonMergePatchInputReader.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.IO;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Text;
52
using System.Text.Json;
6-
using System.Threading.Tasks;
73
using LaDeak.JsonMergePatch.Abstractions;
84
using Microsoft.AspNetCore.Http;
95
using Microsoft.AspNetCore.Http.Json;

src/JsonMergePatch.Http/HttpContentExtensions.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
using System;
2-
using System.Net.Http;
3-
using System.Text;
1+
using System.Text;
42
using System.Text.Json;
5-
using System.Threading;
6-
using System.Threading.Tasks;
73
using LaDeak.JsonMergePatch.Abstractions;
84

95
namespace LaDeak.JsonMergePatch.Http;
@@ -16,21 +12,18 @@ public static class HttpContentExtensions
1612

1713
public static async Task<Patch<TResult>?> ReadJsonPatchAsync<TResult>(this HttpContent content, ITypeRepository typeRepository, JsonSerializerOptions? options = null, CancellationToken cancellationToken = default)
1814
{
19-
if (content == null)
20-
throw new ArgumentNullException(nameof(content));
21-
if (typeRepository == null)
22-
throw new ArgumentNullException(nameof(typeRepository));
15+
ArgumentNullException.ThrowIfNull(content);
16+
ArgumentNullException.ThrowIfNull(typeRepository);
2317
if (!typeRepository.TryGet(typeof(TResult), out var wrapperType))
2418
throw new ArgumentException($"{typeof(TResult)} is missing generated wrapper type. Check if all members of the type definition is supported.");
2519
if (content.Headers.ContentType?.MediaType != "application/merge-patch+json" && content.Headers.ContentType?.MediaType != "application/json" && !string.IsNullOrWhiteSpace(content.Headers.ContentType?.MediaType))
2620
return null;
2721

2822
var contentStream = await content.ReadAsStreamAsync().ConfigureAwait(false);
29-
#if NET5_0_OR_GREATER
3023
Encoding? encoding = content.Headers.ContentType?.CharSet != null ? GetEncoding(content.Headers.ContentType.CharSet) : null;
3124
if (encoding != null && encoding != Encoding.UTF8)
3225
contentStream = Encoding.CreateTranscodingStream(contentStream, encoding, Encoding.UTF8);
33-
#endif
26+
3427
await using (contentStream.ConfigureAwait(false))
3528
{
3629
var contentData = await JsonSerializer.DeserializeAsync(contentStream, wrapperType, options ?? _serializerOptions, cancellationToken).ConfigureAwait(false);

src/JsonMergePatch.Http/JsonMergePatch.Http.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<RootNamespace>LaDeak.JsonMergePatch.Http</RootNamespace>
66
<Nullable>enable</Nullable>
7+
<ImplicitUsings>true</ImplicitUsings>
78
</PropertyGroup>
89

910
<ItemGroup>

src/JsonMergePatch.SourceGenerator/ApplyPatchBuilders/ApplyPatchBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
public abstract class ApplyPatchBuilder
44
{
55
/// <summary>
6-
/// Generates logic to instantiate object for init property.
6+
/// Generates logic to instantiate object with init property.
77
/// </summary>
88
/// <param name="state">The state representing the ApplyPatch method.</param>
99
/// <param name="i">The index of the property in the 'Properties' collection.</param>
1010
/// <returns>Returns the new state.</returns>
1111
public abstract BuilderState BuildInitOnly(BuilderState state, int i);
1212

1313
/// <summary>
14-
/// Generates logic to instantiate object for non-init property.
14+
/// Generates logic to instantiate object with non-init property.
1515
/// </summary>
1616
/// <param name="state">The state representing the ApplyPatch method.</param>
1717
/// <param name="i">The index of the property in the 'Properties' collection.</param>

src/JsonMergePatch.SourceGenerator/ApplyPatchBuilders/GeneratableListBuilder.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Linq;
3-
using Microsoft.CodeAnalysis;
1+
using Microsoft.CodeAnalysis;
42

53
namespace LaDeak.JsonMergePatch.SourceGenerator.ApplyPatchBuilders;
64

@@ -10,9 +8,9 @@ namespace LaDeak.JsonMergePatch.SourceGenerator.ApplyPatchBuilders;
108
public class GeneratableListBuilder : ApplyPatchBuilder
119
{
1210
private readonly ITypeSymbol _firstGenericType;
13-
private readonly IPropertySymbol _property;
11+
private readonly string _propertyName;
1412

15-
public GeneratableListBuilder(INamedTypeSymbol namedType, IPropertySymbol property)
13+
public GeneratableListBuilder(INamedTypeSymbol namedType, string propertyName)
1614
{
1715
if (!namedType.Name.Contains("List")
1816
|| namedType.ContainingNamespace.ToDisplayString() != "System.Collections.Generic"
@@ -21,32 +19,31 @@ public GeneratableListBuilder(INamedTypeSymbol namedType, IPropertySymbol proper
2119
throw new ArgumentException("Input argument type is not a generic list with generatable type.");
2220
}
2321
_firstGenericType = namedType.TypeArguments.First();
24-
_property = property;
22+
_propertyName = propertyName;
2523
}
2624

2725
public override BuilderState BuildInitOnly(BuilderState state, int i)
2826
{
29-
state.AppendLine($"{_property.Name} = Properties[{i}] && input.{_property.Name} == null ? new() : input.{_property.Name},");
27+
state.AppendLine($"{_propertyName} = Properties[{i}] && input.{_propertyName} == null ? new() : input.{_propertyName},");
3028
return state;
3129
}
3230

3331
public override BuilderState BuildInstantiation(BuilderState state, int i)
3432
{
3533
state.AppendLine($"if (Properties[{i}])");
36-
state.IncrementIdentation().AppendLine($"input.{_property.Name} = new();");
34+
state.IncrementIdentation().AppendLine($"input.{_propertyName} = new();");
3735
return state;
3836
}
3937

4038
public override BuilderState BuildPatch(BuilderState state)
4139
{
4240
if (_firstGenericType != null && GeneratedTypeFilter.IsGeneratableType(_firstGenericType))
43-
PopulateGeneratableListProperties(state, _property);
41+
PopulateGeneratableListProperties(state, _propertyName);
4442
return state;
4543
}
4644

47-
private void PopulateGeneratableListProperties(BuilderState state, IPropertySymbol property)
45+
private void PopulateGeneratableListProperties(BuilderState state, string propertyName)
4846
{
49-
var propertyName = property.Name;
5047
state.AppendLine($"if({propertyName} != null)");
5148
state.AppendLine("{");
5249
var ifBody = state.IncrementIdentation();

0 commit comments

Comments
 (0)