Skip to content

Instantly share code, notes, and snippets.

@jspuij
Created May 21, 2020 15:59
Show Gist options
  • Save jspuij/3f46c7adc1af68f1aa917dbf086947e0 to your computer and use it in GitHub Desktop.
Save jspuij/3f46c7adc1af68f1aa917dbf086947e0 to your computer and use it in GitHub Desktop.
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using RedCow;
using RedCow.Immutable;
using RedCow.Immutable.Collections;
namespace RedCow.Test
{
using System.Collections.Generic;
public partial interface ITestPerson
{
/// <summary>
/// Produces the next <see cref = "ITestPerson"/> based on the
/// intial state.
/// </summary>
/// <param name = "initialState">The initial State.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>The next immutable state.</returns>
[ExcludeFromCodeCoverage]
public static ITestPerson Produce(TestPerson initialState, IProducerOptions producerOptions = null) => Produce(initialState, p =>
{
}
, producerOptions);
/// <summary>
/// Produces the next <see cref = "ITestPerson"/> based on the
/// specified recipe action.
/// </summary>
/// <param name = "recipe">The recipe action.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>The next immutable state.</returns>
[ExcludeFromCodeCoverage]
public ITestPerson Produce(Action<TestPerson> recipe, IProducerOptions producerOptions = null) => Produce((TestPerson)this, recipe, producerOptions);
/// <summary>
/// Produces the next <see cref = "ITestPerson"/> based on the
/// specified recipe action.
/// </summary>
/// <param name = "initialState">The initial State.</param>
/// <param name = "recipe">The recipe action.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>The next immutable state.</returns>
[ExcludeFromCodeCoverage]
public static ITestPerson Produce(TestPerson initialState, Action<TestPerson> recipe, IProducerOptions producerOptions = null) => Producer(recipe, producerOptions)(initialState);
/// <summary>
/// Produces the next <see cref = "ITestPerson"/> based on the
/// specified recipe function.
/// </summary>
/// <param name = "recipe">The recipe function.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>The next immutable state.</returns>
[ExcludeFromCodeCoverage]
public ITestPerson Produce(Func<TestPerson> recipe, IProducerOptions producerOptions = null) => Produce((TestPerson)this, recipe, producerOptions);
/// <summary>
/// Produces the next <see cref = "ITestPerson"/> based on the
/// specified recipe function.
/// </summary>
/// <param name = "initialState">The initial State.</param>
/// <param name = "recipe">The recipe function.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>The next immutable state.</returns>
[ExcludeFromCodeCoverage]
public static ITestPerson Produce(TestPerson initialState, Func<TestPerson> recipe, IProducerOptions producerOptions = null) => Producer(recipe, producerOptions)(initialState);
/// <summary>
/// Creates a Producer delegate that can be used to curry on an Immutable State.
/// </summary>
/// <param name = "recipe">The recipe action that operates on an object of type T.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>A producer delegate.</returns>
[ExcludeFromCodeCoverage]
public static Func<ITestPerson, ITestPerson> Producer(Action<TestPerson> recipe, IProducerOptions producerOptions = null) => (immutable1) => Producer<object>((argument1, _) => recipe(argument1), producerOptions)(immutable1, null);
/// <summary>
/// Creates a Producer delegate that can be used to curry on an Immutable State.
/// </summary>
/// <param name = "recipe">The recipe function that returns an object of type T.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <returns>A recipe delegate.</returns>
[ExcludeFromCodeCoverage]
public static Func<ITestPerson, ITestPerson> Producer(Func<TestPerson> recipe, IProducerOptions producerOptions = null) => (immutable1) => Producer<object>(_ => recipe(), producerOptions)(immutable1, null);
/// <summary>
/// Creates a Producer delegate that can be used to curry on an Immutable State.
/// </summary>
/// <param name = "recipe">The recipe action that operates on an object of type T with a single argument.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <typeparam name = "TArg">The type of the argument.</typeparam>
/// <returns>A producer delegate.</returns>
[ExcludeFromCodeCoverage]
public static Func<ITestPerson, TArg, ITestPerson> Producer<TArg>(Action<TestPerson, TArg> recipe, IProducerOptions producerOptions = null) => (immutable1, argument1) =>
{
using var scope = ((TestPerson)immutable1).CreateDraft<TestPerson>(out var draft, producerOptions);
recipe(draft, argument1);
return scope.FinishDraft<TestPerson, ITestPerson>(draft);
}
;
/// <summary>
/// Creates a Producer delegate that can be used to curry on an Immutable State.
/// </summary>
/// <param name = "recipe">The recipe function that returns an object of type T with a single argument.</param>
/// <param name = "producerOptions">The producer options to use. If you leave them null, the default options will be used.</param>
/// <typeparam name = "TArg">The type of the argument.</typeparam>
/// <returns>A producer delegate.</returns>
[ExcludeFromCodeCoverage]
public static Func<ITestPerson, TArg, ITestPerson> Producer<TArg>(Func<TArg, TestPerson> recipe, IProducerOptions producerOptions = null) => (immutable1, argument1) =>
{
using var scope = ((TestPerson)immutable1).CreateDraft<TestPerson>(out var _, producerOptions);
TestPerson draft = recipe(argument1);
return scope.FinishDraft<TestPerson, ITestPerson>(draft);
}
;
}
}
// ------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using RedCow;
using RedCow.Immutable;
using RedCow.Immutable.Collections;
namespace RedCow.Test
{
[ProxyType(typeof(ProxyTestPerson))]
[ExcludeFromCodeCoverage]
public partial class TestPerson : ITestPerson, IPropertyAccessors, ILockable
{
/// <summary>
/// FirstName field.
/// </summary>
private string _FirstName;
/// <summary>
/// Gets or sets FirstName.
/// </summary>
public virtual string FirstName
{
get => this._FirstName;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"FirstName cannot be changed as this object is immutable");
}
this._FirstName = value;
}
}
/// <summary>
/// LastName field.
/// </summary>
private string _LastName;
/// <summary>
/// Gets or sets LastName.
/// </summary>
public virtual string LastName
{
get => this._LastName;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"LastName cannot be changed as this object is immutable");
}
this._LastName = value;
}
}
/// <summary>
/// A boolean indicating whether IsAdult is true.
/// </summary>
private bool _IsAdult;
/// <summary>
/// Gets or sets a value indicating whether IsAdult is true.
/// </summary>
public virtual bool IsAdult
{
get => this._IsAdult;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"IsAdult cannot be changed as this object is immutable");
}
this._IsAdult = value;
}
}
/// <summary>
/// FirstChild field.
/// </summary>
private RedCow.Test.TestPerson _FirstChild;
/// <summary>
/// Gets or sets FirstChild.
/// </summary>
public virtual RedCow.Test.TestPerson FirstChild
{
get => this._FirstChild;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"FirstChild cannot be changed as this object is immutable");
}
this._FirstChild = value;
}
}
/// <summary>
/// Gets FirstChild.
/// </summary>
RedCow.Test.ITestPerson ITestPerson.FirstChild => this.FirstChild;
/// <summary>
/// SecondChild field.
/// </summary>
private RedCow.Test.TestPerson _SecondChild;
/// <summary>
/// Gets or sets SecondChild.
/// </summary>
public virtual RedCow.Test.TestPerson SecondChild
{
get => this._SecondChild;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"SecondChild cannot be changed as this object is immutable");
}
this._SecondChild = value;
}
}
/// <summary>
/// Gets SecondChild.
/// </summary>
RedCow.Test.ITestPerson ITestPerson.SecondChild => this.SecondChild;
/// <summary>
/// Cars field.
/// </summary>
private System.Collections.Generic.IList<RedCow.Test.Car> _Cars;
/// <summary>
/// Gets or sets Cars.
/// </summary>
public virtual System.Collections.Generic.IList<RedCow.Test.Car> Cars
{
get => this._Cars;
set
{
if (this.locked)
{
throw new ImmutableException(this, $"Cars cannot be changed as this object is immutable");
}
this._Cars = value;
}
}
/// <summary>
/// Gets Cars.
/// </summary>
System.Collections.Generic.IReadOnlyList<RedCow.Test.ICar> ITestPerson.Cars => (System.Collections.Generic.IReadOnlyList<RedCow.Test.ICar>)this.Cars;
/// <summary>
/// Dictionary with public property getters.
/// </summary>
private IReadOnlyDictionary<string, Func<object>> publicPropertyGetters;
/// <summary>
/// Gets the Public Property Getters.
/// </summary>
IReadOnlyDictionary<string, Func<object>> IPropertyAccessors.PublicPropertyGetters
{
get
{
if (publicPropertyGetters == null)
{
publicPropertyGetters = new Dictionary<string, Func<object>>{[nameof(FirstName)] = () => FirstName, [nameof(LastName)] = () => LastName, [nameof(IsAdult)] = () => IsAdult, [nameof(FirstChild)] = () => FirstChild, [nameof(SecondChild)] = () => SecondChild, [nameof(Cars)] = () => Cars, };
}
return publicPropertyGetters;
}
}
/// <summary>
/// Dictionary with public property setters.
/// </summary>
private IReadOnlyDictionary<string, Action<object>> publicPropertySetters;
/// <summary>
/// Gets the Public Property Setters.
/// </summary>
IReadOnlyDictionary<string, Action<object>> IPropertyAccessors.PublicPropertySetters
{
get
{
if (publicPropertySetters == null)
{
publicPropertySetters = new Dictionary<string, Action<object>>{[nameof(FirstName)] = value => FirstName = (string)value, [nameof(LastName)] = value => LastName = (string)value, [nameof(IsAdult)] = value => IsAdult = (bool)value, [nameof(FirstChild)] = value => FirstChild = (RedCow.Test.TestPerson)value, [nameof(SecondChild)] = value => SecondChild = (RedCow.Test.TestPerson)value, [nameof(Cars)] = value => Cars = (System.Collections.Generic.IList<RedCow.Test.Car>)value, };
}
return publicPropertySetters;
}
}
/// <summary>
/// Whether the instance is locked.
/// </summary>
private bool locked;
/// <summary>
/// Gets a value indicating whether the immutable is locked.
/// </summary>
bool ILockable.Locked => this.locked;
/// <summary>
/// Locks the immutable.
/// </summary>
void ILockable.Lock()
{
this.locked = true;
if (this.FirstChild is ILockable && !((ILockable)this.FirstChild).Locked)
{
((ILockable)this.FirstChild).Lock();
}
if (this.SecondChild is ILockable && !((ILockable)this.SecondChild).Locked)
{
((ILockable)this.SecondChild).Lock();
}
if (this.Cars is ILockable && !((ILockable)this.Cars).Locked)
{
((ILockable)this.Cars).Lock();
}
}
}
/// <summary>
/// Proxy Implementation of <see cref = "TestPerson"/>.
/// </summary>
[ExcludeFromCodeCoverage]
public partial class ProxyTestPerson : TestPerson, IDraft<TestPerson>
{
/// <summary>
/// Gets or sets FirstName.
/// </summary>
public override string FirstName
{
get
{
if (this.draftState == null)
{
return base.FirstName;
}
return this.draftState.Get<string>(nameof(FirstName), () => base.FirstName, () => this.Original.FirstName, value => base.FirstName = value);
}
set
{
if (this.draftState == null)
{
base.FirstName = value;
}
this.draftState.Set<string>(nameof(FirstName), () => base.FirstName = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// Gets or sets LastName.
/// </summary>
public override string LastName
{
get
{
if (this.draftState == null)
{
return base.LastName;
}
return this.draftState.Get<string>(nameof(LastName), () => base.LastName, () => this.Original.LastName, value => base.LastName = value);
}
set
{
if (this.draftState == null)
{
base.LastName = value;
}
this.draftState.Set<string>(nameof(LastName), () => base.LastName = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// Gets or sets a value indicating whether IsAdult is true.
/// </summary>
public override bool IsAdult
{
get
{
if (this.draftState == null)
{
return base.IsAdult;
}
return this.draftState.Get<bool>(nameof(IsAdult), () => base.IsAdult, () => this.Original.IsAdult, value => base.IsAdult = value);
}
set
{
if (this.draftState == null)
{
base.IsAdult = value;
}
this.draftState.Set<bool>(nameof(IsAdult), () => base.IsAdult = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// Gets or sets FirstChild.
/// </summary>
public override RedCow.Test.TestPerson FirstChild
{
get
{
if (this.draftState == null)
{
return base.FirstChild;
}
return this.draftState.Get<RedCow.Test.TestPerson>(nameof(FirstChild), () => base.FirstChild, () => this.Original.FirstChild, value => base.FirstChild = value);
}
set
{
if (this.draftState == null)
{
base.FirstChild = value;
}
this.draftState.Set<RedCow.Test.TestPerson>(nameof(FirstChild), () => base.FirstChild = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// Gets or sets SecondChild.
/// </summary>
public override RedCow.Test.TestPerson SecondChild
{
get
{
if (this.draftState == null)
{
return base.SecondChild;
}
return this.draftState.Get<RedCow.Test.TestPerson>(nameof(SecondChild), () => base.SecondChild, () => this.Original.SecondChild, value => base.SecondChild = value);
}
set
{
if (this.draftState == null)
{
base.SecondChild = value;
}
this.draftState.Set<RedCow.Test.TestPerson>(nameof(SecondChild), () => base.SecondChild = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// Gets or sets Cars.
/// </summary>
public override System.Collections.Generic.IList<RedCow.Test.Car> Cars
{
get
{
if (this.draftState == null)
{
return base.Cars;
}
return this.draftState.Get<System.Collections.Generic.IList<RedCow.Test.Car>>(nameof(Cars), () => base.Cars, () => this.Original.Cars, value => base.Cars = value);
}
set
{
if (this.draftState == null)
{
base.Cars = value;
}
this.draftState.Set<System.Collections.Generic.IList<RedCow.Test.Car>>(nameof(Cars), () => base.Cars = value, ((IDraft)this).Clone);
}
}
/// <summary>
/// the draftState field.
/// </summary>
private ObjectDraftState draftState;
/// <summary>
/// Gets or sets the DraftState.
/// </summary>
DraftState IDraft.DraftState
{
get => this.draftState;
set
{
if (this.draftState != null && value != null)
{
throw new DraftException(this, "Draft state already set.");
}
if (((ILockable)this).Locked && value != null)
{
throw new ImmutableException(this, "This instance is immutable and cannot be assigned a new Draft state.");
}
this.draftState = (ObjectDraftState)value;
}
}
/// <summary>
/// Gets the original.
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public TestPerson Original => this.draftState.GetOriginal<TestPerson>();
/// <summary>
/// Gets the original as Immutable.
/// </summary>
[System.Text.Json.Serialization.JsonIgnore]
[Newtonsoft.Json.JsonIgnore]
public ITestPerson ImmutableOriginal => this.draftState.GetOriginal<ITestPerson>();
/// <summary>
/// Clones the object from the original.
/// </summary>
void IDraft.Clone()
{
if (this.draftState == null)
{
throw new DraftException(this, "Draft state not set.");
}
if (((ILockable)this).Locked)
{
throw new ImmutableException(this, "This instance is immutable and cannot be the destination of a clone operation.");
}
this.FirstName = this.Original.FirstName;
this.LastName = this.Original.LastName;
this.IsAdult = this.Original.IsAdult;
if (!this.FirstChild.IsDraft())
{
this.FirstChild = this.Original.FirstChild;
}
if (!this.SecondChild.IsDraft())
{
this.SecondChild = this.Original.SecondChild;
}
if (!this.Cars.IsDraft())
{
this.Cars = this.Original.Cars;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment