Skip to content

Commit

Permalink
Fixed support for IReadOnlyCollection and IReadOnlyList (#63)
Browse files Browse the repository at this point in the history
* Fixed support for IReadOnlyCollection and IReadOnlyList

* x

* .

* .

* .

* fix

* .

* yes?

* .

* 0.9.1

* .
  • Loading branch information
StefH authored Aug 24, 2024
1 parent a97289b commit af562f3
Show file tree
Hide file tree
Showing 55 changed files with 446 additions and 316 deletions.
7 changes: 7 additions & 0 deletions src-examples/BuilderConsumer/Content.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BuilderConsumer
{
public class Content
{
public string Id { get; set; }
}
}
9 changes: 9 additions & 0 deletions src-examples/BuilderConsumer/ContentBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentBuilder;

namespace BuilderConsumer
{
[AutoGenerateBuilder(typeof(Content))]
public partial class ContentBuilder
{
}
}
10 changes: 10 additions & 0 deletions src-examples/BuilderConsumer/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ class Program

static void Main(string[] args)
{
var settings = new SettingsBuilder()
// .WithContents(new[] { new Content { Id = "a"} })
.WithContents(x => x
.Add(cb => cb.WithId("b")
)
)
// .WithContents(Array.Empty<Content>)
.Build()
;

var t1 = new ThingWithOnlyParameterizedConstructorsBuilder()
.UsingConstructor(1, 2, "drie")
.WithL(444444)
Expand Down
9 changes: 9 additions & 0 deletions src-examples/BuilderConsumer/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace BuilderConsumer
{
public class Settings
{
public IReadOnlyCollection<Content> Contents { get; set; }
}
}
9 changes: 9 additions & 0 deletions src-examples/BuilderConsumer/SettingsBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using FluentBuilder;

namespace BuilderConsumer
{
[AutoGenerateBuilder(typeof(Settings))]
public partial class SettingsBuilder
{
}
}
3 changes: 2 additions & 1 deletion src/FluentBuilderGenerator/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ internal static class EnumExtensions
{ FluentTypeKind.Array, FileDataType.ArrayBuilder },
{ FluentTypeKind.IEnumerable, FileDataType.IEnumerableBuilder },
{ FluentTypeKind.ICollection, FileDataType.ICollectionBuilder },
{ FluentTypeKind.IList, FileDataType.IListBuilder },
{ FluentTypeKind.IReadOnlyCollection, FileDataType.IReadOnlyCollectionBuilder },
{ FluentTypeKind.IList, FileDataType.IListBuilder }
{ FluentTypeKind.IReadOnlyList, FileDataType.IReadOnlyListBuilder },
};
private static readonly IDictionary<FileDataType, FluentTypeKind> FileToKind = KindToFile.ToDictionary(x => x.Value, x => x.Key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ internal static class PropertySymbolExtensions
// ReSharper disable once InconsistentNaming
private static readonly FluentTypeKind[] IEnumerableKinds =
{
FluentTypeKind.ICollection,
FluentTypeKind.IEnumerable,
FluentTypeKind.IList,
FluentTypeKind.ICollection
FluentTypeKind.IReadOnlyCollection,
FluentTypeKind.IReadOnlyList
};

internal static bool IsPrivateSettable(this IPropertySymbol property)
Expand Down
5 changes: 5 additions & 0 deletions src/FluentBuilderGenerator/Extensions/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public static FluentTypeKind GetFluentTypeKind(this ITypeSymbol typeSymbol)
return FluentTypeKind.IList;
}

if (typeSymbol.ImplementsInterfaceOrBaseClass(typeof(IReadOnlyList<>)))
{
return FluentTypeKind.IReadOnlyList;
}

if (typeSymbol.ImplementsInterfaceOrBaseClass(typeof(IReadOnlyCollection<>)))
{
return FluentTypeKind.IReadOnlyCollection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ internal partial class FluentBuilderClassesGenerator : IFilesGenerator
private static readonly FileDataType[] ExtraBuilders =
{
FileDataType.ArrayBuilder,
FileDataType.ICollectionBuilder,
FileDataType.IEnumerableBuilder,
FileDataType.IListBuilder,
FileDataType.ICollectionBuilder
FileDataType.IReadOnlyCollectionBuilder,
FileDataType.IReadOnlyListBuilder
};

private readonly IGeneratorExecutionContextWrapper _context;
Expand Down
4 changes: 2 additions & 2 deletions src/FluentBuilderGenerator/FluentBuilderGenerator.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.9.0</Version>
<Version>0.9.1</Version>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>11</LangVersion>
<LangVersion>latest</LangVersion>
<ImplicitUsings>true</ImplicitUsings>
<Nullable>enable</Nullable>
<ProjectGuid>{12344228-91F4-4502-9595-39584E5A9944}</ProjectGuid>
Expand Down
10 changes: 7 additions & 3 deletions src/FluentBuilderGenerator/FluentBuilderSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ private static void InjectGeneratedClasses(IGeneratorExecutionContextWrapper con
{
var generators = new IFileGenerator[]
{
new ExtraFilesGenerator(context.AssemblyName, context.SupportsNullable),
new BaseBuilderGenerator(context.AssemblyName, context.SupportsNullable),

new ExtraFilesGenerator(context.AssemblyName, context.SupportsNullable),

new IDictionaryBuilderGenerator(context.AssemblyName, context.SupportsNullable),

new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.ArrayBuilder, context.SupportsNullable),
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.ICollectionBuilder, context.SupportsNullable),
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.IEnumerableBuilder, context.SupportsNullable),
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.IListBuilder, context.SupportsNullable),
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.IReadOnlyCollectionBuilder, context.SupportsNullable),
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.ICollectionBuilder, context.SupportsNullable),
new IDictionaryBuilderGenerator(context.AssemblyName, context.SupportsNullable)
new IEnumerableBuilderGenerator(context.AssemblyName, FileDataType.IReadOnlyListBuilder, context.SupportsNullable),
};

foreach (var generator in generators)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ public static (string GenericType, string ToArray) GetGenericTypeAndToArray(File
return dataType switch
{
FileDataType.ArrayBuilder => ($"{t}[]", ".ToArray()"),
FileDataType.IEnumerableBuilder => ($"IEnumerable<{t}>", string.Empty),
FileDataType.IReadOnlyCollectionBuilder => ($"IReadOnlyCollection<{t}>", string.Empty),
FileDataType.ICollectionBuilder => ($"ICollection<{t}>", string.Empty),
FileDataType.IEnumerableBuilder => ($"IEnumerable<{t}>", string.Empty),
FileDataType.IListBuilder => ($"IList<{t}>", string.Empty),
FileDataType.IReadOnlyCollectionBuilder => ($"IReadOnlyCollection<{t}>", string.Empty),
FileDataType.IReadOnlyListBuilder => ($"IReadOnlyList<{t}>", string.Empty),
_ => throw new ArgumentException()
};
}
Expand Down
10 changes: 6 additions & 4 deletions src/FluentBuilderGenerator/Types/FileDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ internal enum FileDataType : byte
{
None,

ArrayBuilder,

Attribute,

Base,

Builder,

ArrayBuilder,
ICollectionBuilder,

IDictionaryBuilder,

IEnumerableBuilder,

IListBuilder,

ICollectionBuilder,

IReadOnlyCollectionBuilder,

IDictionaryBuilder
IReadOnlyListBuilder,
}
16 changes: 9 additions & 7 deletions src/FluentBuilderGenerator/Types/FluentTypeKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ internal enum FluentTypeKind : byte
{
None,

String,

Array,

IEnumerable,

ICollection,

IReadOnlyCollection,

IDictionary,

IEnumerable,

IList,

IDictionary,
IReadOnlyCollection,

IReadOnlyList,

ReadOnlyCollection,

String,

Other
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
8 changes: 8 additions & 0 deletions tests/FluentBuilderGeneratorTests/DTO/Address.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ public class Address

public IReadOnlyCollection<string> IReadOnlyCollection { get; set; }

public IReadOnlyCollection<Thing> IReadOnlyCollectionThing { get; set; }

public IReadOnlyCollection<Address> IReadOnlyCollectionAddress { get; set; }

public ReadOnlyCollection<long> ReadOnlyCollection { get; set; }

public IReadOnlyList<float> IReadOnlyList { get; set; }

public IReadOnlyList<Address> IReadOnlyListAddress { get; set; }

public IEnumerable<byte> Enumerable { get; set; }

public IEnumerable<Address> Enumerable2 { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down Expand Up @@ -138,6 +138,42 @@ public AddressBuilder WithIReadOnlyCollection(Func<System.Collections.Generic.IR
_iReadOnlyCollectionIsSet = true;
return this;
}
public AddressBuilder WithIReadOnlyCollection(Action<FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyCollectionBuilder<string>> action, bool useObjectInitializer = true) => WithIReadOnlyCollection(() =>
{
var builder = new FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyCollectionBuilder<string>();
action(builder);
return builder.Build(useObjectInitializer);
});
private bool _iReadOnlyCollectionThingIsSet;
private Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Thing>> _iReadOnlyCollectionThing = new Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Thing>>(() => new List<FluentBuilderGeneratorTests.DTO.Thing>());
public AddressBuilder WithIReadOnlyCollectionThing(System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Thing> value) => WithIReadOnlyCollectionThing(() => value);
public AddressBuilder WithIReadOnlyCollectionThing(Func<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Thing>> func)
{
_iReadOnlyCollectionThing = new Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Thing>>(func);
_iReadOnlyCollectionThingIsSet = true;
return this;
}
public AddressBuilder WithIReadOnlyCollectionThing(Action<FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyCollectionBuilder<FluentBuilderGeneratorTests.DTO.Thing>> action, bool useObjectInitializer = true) => WithIReadOnlyCollectionThing(() =>
{
var builder = new FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyCollectionBuilder<FluentBuilderGeneratorTests.DTO.Thing>();
action(builder);
return builder.Build(useObjectInitializer);
});
private bool _iReadOnlyCollectionAddressIsSet;
private Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Address>> _iReadOnlyCollectionAddress = new Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Address>>(() => new List<FluentBuilderGeneratorTests.DTO.Address>());
public AddressBuilder WithIReadOnlyCollectionAddress(System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Address> value) => WithIReadOnlyCollectionAddress(() => value);
public AddressBuilder WithIReadOnlyCollectionAddress(Func<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Address>> func)
{
_iReadOnlyCollectionAddress = new Lazy<System.Collections.Generic.IReadOnlyCollection<FluentBuilderGeneratorTests.DTO.Address>>(func);
_iReadOnlyCollectionAddressIsSet = true;
return this;
}
public AddressBuilder WithIReadOnlyCollectionAddress(Action<FluentBuilderGeneratorTests.DTO.IReadOnlyCollectionAddressBuilder> action, bool useObjectInitializer = true) => WithIReadOnlyCollectionAddress(() =>
{
var builder = new FluentBuilderGeneratorTests.DTO.IReadOnlyCollectionAddressBuilder();
action(builder);
return builder.Build(useObjectInitializer);
});
private bool _readOnlyCollectionIsSet;
private Lazy<System.Collections.ObjectModel.ReadOnlyCollection<long>> _readOnlyCollection = new Lazy<System.Collections.ObjectModel.ReadOnlyCollection<long>>(() => new System.Collections.ObjectModel.ReadOnlyCollection<long>(new List<long>()));
public AddressBuilder WithReadOnlyCollection(System.Collections.ObjectModel.ReadOnlyCollection<long> value) => WithReadOnlyCollection(() => value);
Expand All @@ -147,6 +183,36 @@ public AddressBuilder WithReadOnlyCollection(Func<System.Collections.ObjectModel
_readOnlyCollectionIsSet = true;
return this;
}
private bool _iReadOnlyListIsSet;
private Lazy<System.Collections.Generic.IReadOnlyList<float>> _iReadOnlyList = new Lazy<System.Collections.Generic.IReadOnlyList<float>>(() => default(System.Collections.Generic.IReadOnlyList<float>));
public AddressBuilder WithIReadOnlyList(System.Collections.Generic.IReadOnlyList<float> value) => WithIReadOnlyList(() => value);
public AddressBuilder WithIReadOnlyList(Func<System.Collections.Generic.IReadOnlyList<float>> func)
{
_iReadOnlyList = new Lazy<System.Collections.Generic.IReadOnlyList<float>>(func);
_iReadOnlyListIsSet = true;
return this;
}
public AddressBuilder WithIReadOnlyList(Action<FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyListBuilder<float>> action, bool useObjectInitializer = true) => WithIReadOnlyList(() =>
{
var builder = new FluentBuilderGeneratorTests.FluentBuilder.IReadOnlyListBuilder<float>();
action(builder);
return builder.Build(useObjectInitializer);
});
private bool _iReadOnlyListAddressIsSet;
private Lazy<System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address>> _iReadOnlyListAddress = new Lazy<System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address>>(() => default(System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address>));
public AddressBuilder WithIReadOnlyListAddress(System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address> value) => WithIReadOnlyListAddress(() => value);
public AddressBuilder WithIReadOnlyListAddress(Func<System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address>> func)
{
_iReadOnlyListAddress = new Lazy<System.Collections.Generic.IReadOnlyList<FluentBuilderGeneratorTests.DTO.Address>>(func);
_iReadOnlyListAddressIsSet = true;
return this;
}
public AddressBuilder WithIReadOnlyListAddress(Action<FluentBuilderGeneratorTests.DTO.IReadOnlyListAddressBuilder> action, bool useObjectInitializer = true) => WithIReadOnlyListAddress(() =>
{
var builder = new FluentBuilderGeneratorTests.DTO.IReadOnlyListAddressBuilder();
action(builder);
return builder.Build(useObjectInitializer);
});
private bool _enumerableIsSet;
private Lazy<System.Collections.Generic.IEnumerable<byte>> _enumerable = new Lazy<System.Collections.Generic.IEnumerable<byte>>(() => new byte[0]);
public AddressBuilder WithEnumerable(System.Collections.Generic.IEnumerable<byte> value) => WithEnumerable(() => value);
Expand Down Expand Up @@ -326,7 +392,11 @@ public override Address Build(bool useObjectInitializer)
Thing = _thing.Value,
ThingIsStruct = _thingIsStruct.Value,
IReadOnlyCollection = _iReadOnlyCollection.Value,
IReadOnlyCollectionThing = _iReadOnlyCollectionThing.Value,
IReadOnlyCollectionAddress = _iReadOnlyCollectionAddress.Value,
ReadOnlyCollection = _readOnlyCollection.Value,
IReadOnlyList = _iReadOnlyList.Value,
IReadOnlyListAddress = _iReadOnlyListAddress.Value,
Enumerable = _enumerable.Value,
Enumerable2 = _enumerable2.Value,
List = _list.Value,
Expand Down Expand Up @@ -359,7 +429,11 @@ public override Address Build(bool useObjectInitializer)
if (_thingIsSet) { Instance.Value.Thing = _thing.Value; }
if (_thingIsStructIsSet) { Instance.Value.ThingIsStruct = _thingIsStruct.Value; }
if (_iReadOnlyCollectionIsSet) { Instance.Value.IReadOnlyCollection = _iReadOnlyCollection.Value; }
if (_iReadOnlyCollectionThingIsSet) { Instance.Value.IReadOnlyCollectionThing = _iReadOnlyCollectionThing.Value; }
if (_iReadOnlyCollectionAddressIsSet) { Instance.Value.IReadOnlyCollectionAddress = _iReadOnlyCollectionAddress.Value; }
if (_readOnlyCollectionIsSet) { Instance.Value.ReadOnlyCollection = _readOnlyCollection.Value; }
if (_iReadOnlyListIsSet) { Instance.Value.IReadOnlyList = _iReadOnlyList.Value; }
if (_iReadOnlyListAddressIsSet) { Instance.Value.IReadOnlyListAddress = _iReadOnlyListAddress.Value; }
if (_enumerableIsSet) { Instance.Value.Enumerable = _enumerable.Value; }
if (_enumerable2IsSet) { Instance.Value.Enumerable2 = _enumerable2.Value; }
if (_listIsSet) { Instance.Value.List = _list.Value; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.0.0
// This code was generated by https://github.com/StefH/FluentBuilder version 0.9.1.0
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
Expand Down
Loading

0 comments on commit af562f3

Please sign in to comment.