Skip to content

Commit

Permalink
Variants with properties (#98)
Browse files Browse the repository at this point in the history
* Fixing tests

* Fixing tests

* Making custom config less tedious

* Adding SimpleVariant

* Making Variants not have typed attributes

* Revising Variants

z

* Adding getter/setter for variants

* Fixing tests

* Adding !
  • Loading branch information
Gekctek authored Sep 25, 2023
1 parent cece225 commit 8f45388
Show file tree
Hide file tree
Showing 141 changed files with 28,430 additions and 1,852 deletions.
39 changes: 5 additions & 34 deletions samples/Sample.Shared/Dex/Models/CancelOrderReceipt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using EdjCase.ICP.Candid.Mapping;
using Sample.Shared.Dex.Models;
using System;
using OrderId = System.UInt32;

namespace Sample.Shared.Dex.Models
{
[Variant(typeof(CancelOrderReceiptTag))]
[Variant()]
public class CancelOrderReceipt
{
[VariantTagProperty()]
Expand All @@ -14,6 +13,10 @@ public class CancelOrderReceipt
[VariantValueProperty()]
public object? Value { get; set; }

public CancelOrderErr? Err { get => this.Tag == CancelOrderReceiptTag.Err ? (CancelOrderErr)this.Value! : default; set => (this.Tag, this.Value) = (CancelOrderReceiptTag.Err, value); }

public OrderId? Ok { get => this.Tag == CancelOrderReceiptTag.Ok ? (OrderId)this.Value! : default; set => (this.Tag, this.Value) = (CancelOrderReceiptTag.Ok, value); }

public CancelOrderReceipt(CancelOrderReceiptTag tag, object? value)
{
this.Tag = tag;
Expand All @@ -23,43 +26,11 @@ public CancelOrderReceipt(CancelOrderReceiptTag tag, object? value)
protected CancelOrderReceipt()
{
}

public static CancelOrderReceipt Err(CancelOrderErr info)
{
return new CancelOrderReceipt(CancelOrderReceiptTag.Err, info);
}

public static CancelOrderReceipt Ok(OrderId info)
{
return new CancelOrderReceipt(CancelOrderReceiptTag.Ok, info);
}

public CancelOrderErr AsErr()
{
this.ValidateTag(CancelOrderReceiptTag.Err);
return (CancelOrderErr)this.Value!;
}

public OrderId AsOk()
{
this.ValidateTag(CancelOrderReceiptTag.Ok);
return (OrderId)this.Value!;
}

private void ValidateTag(CancelOrderReceiptTag tag)
{
if (!this.Tag.Equals(tag))
{
throw new InvalidOperationException($"Cannot cast '{this.Tag}' to type '{tag}'");
}
}
}

public enum CancelOrderReceiptTag
{
[VariantOptionType(typeof(CancelOrderErr))]
Err,
[VariantOptionType(typeof(OrderId))]
Ok
}
}
39 changes: 5 additions & 34 deletions samples/Sample.Shared/Dex/Models/DepositReceipt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using EdjCase.ICP.Candid.Mapping;
using Sample.Shared.Dex.Models;
using EdjCase.ICP.Candid.Models;
using System;

namespace Sample.Shared.Dex.Models
{
[Variant(typeof(DepositReceiptTag))]
[Variant()]
public class DepositReceipt
{
[VariantTagProperty()]
Expand All @@ -14,6 +13,10 @@ public class DepositReceipt
[VariantValueProperty()]
public object? Value { get; set; }

public DepositErr? Err { get => this.Tag == DepositReceiptTag.Err ? (DepositErr)this.Value! : default; set => (this.Tag, this.Value) = (DepositReceiptTag.Err, value); }

public UnboundedUInt? Ok { get => this.Tag == DepositReceiptTag.Ok ? (UnboundedUInt)this.Value! : default; set => (this.Tag, this.Value) = (DepositReceiptTag.Ok, value); }

public DepositReceipt(DepositReceiptTag tag, object? value)
{
this.Tag = tag;
Expand All @@ -23,43 +26,11 @@ public DepositReceipt(DepositReceiptTag tag, object? value)
protected DepositReceipt()
{
}

public static DepositReceipt Err(DepositErr info)
{
return new DepositReceipt(DepositReceiptTag.Err, info);
}

public static DepositReceipt Ok(UnboundedUInt info)
{
return new DepositReceipt(DepositReceiptTag.Ok, info);
}

public DepositErr AsErr()
{
this.ValidateTag(DepositReceiptTag.Err);
return (DepositErr)this.Value!;
}

public UnboundedUInt AsOk()
{
this.ValidateTag(DepositReceiptTag.Ok);
return (UnboundedUInt)this.Value!;
}

private void ValidateTag(DepositReceiptTag tag)
{
if (!this.Tag.Equals(tag))
{
throw new InvalidOperationException($"Cannot cast '{this.Tag}' to type '{tag}'");
}
}
}

public enum DepositReceiptTag
{
[VariantOptionType(typeof(DepositErr))]
Err,
[VariantOptionType(typeof(UnboundedUInt))]
Ok
}
}
39 changes: 5 additions & 34 deletions samples/Sample.Shared/Dex/Models/OrderPlacementReceipt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using EdjCase.ICP.Candid.Mapping;
using Sample.Shared.Dex.Models;
using EdjCase.ICP.Candid.Models;
using System;

namespace Sample.Shared.Dex.Models
{
[Variant(typeof(OrderPlacementReceiptTag))]
[Variant()]
public class OrderPlacementReceipt
{
[VariantTagProperty()]
Expand All @@ -14,6 +13,10 @@ public class OrderPlacementReceipt
[VariantValueProperty()]
public object? Value { get; set; }

public OrderPlacementErr? Err { get => this.Tag == OrderPlacementReceiptTag.Err ? (OrderPlacementErr)this.Value! : default; set => (this.Tag, this.Value) = (OrderPlacementReceiptTag.Err, value); }

public OptionalValue<Order>? Ok { get => this.Tag == OrderPlacementReceiptTag.Ok ? (OptionalValue<Order>)this.Value! : default; set => (this.Tag, this.Value) = (OrderPlacementReceiptTag.Ok, value); }

public OrderPlacementReceipt(OrderPlacementReceiptTag tag, object? value)
{
this.Tag = tag;
Expand All @@ -23,43 +26,11 @@ public OrderPlacementReceipt(OrderPlacementReceiptTag tag, object? value)
protected OrderPlacementReceipt()
{
}

public static OrderPlacementReceipt Err(OrderPlacementErr info)
{
return new OrderPlacementReceipt(OrderPlacementReceiptTag.Err, info);
}

public static OrderPlacementReceipt Ok(OptionalValue<Order> info)
{
return new OrderPlacementReceipt(OrderPlacementReceiptTag.Ok, info);
}

public OrderPlacementErr AsErr()
{
this.ValidateTag(OrderPlacementReceiptTag.Err);
return (OrderPlacementErr)this.Value!;
}

public OptionalValue<Order> AsOk()
{
this.ValidateTag(OrderPlacementReceiptTag.Ok);
return (OptionalValue<Order>)this.Value!;
}

private void ValidateTag(OrderPlacementReceiptTag tag)
{
if (!this.Tag.Equals(tag))
{
throw new InvalidOperationException($"Cannot cast '{this.Tag}' to type '{tag}'");
}
}
}

public enum OrderPlacementReceiptTag
{
[VariantOptionType(typeof(OrderPlacementErr))]
Err,
[VariantOptionType(typeof(OptionalValue<Order>))]
Ok
}
}
39 changes: 5 additions & 34 deletions samples/Sample.Shared/Dex/Models/WithdrawReceipt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using EdjCase.ICP.Candid.Mapping;
using Sample.Shared.Dex.Models;
using EdjCase.ICP.Candid.Models;
using System;

namespace Sample.Shared.Dex.Models
{
[Variant(typeof(WithdrawReceiptTag))]
[Variant()]
public class WithdrawReceipt
{
[VariantTagProperty()]
Expand All @@ -14,6 +13,10 @@ public class WithdrawReceipt
[VariantValueProperty()]
public object? Value { get; set; }

public WithdrawErr? Err { get => this.Tag == WithdrawReceiptTag.Err ? (WithdrawErr)this.Value! : default; set => (this.Tag, this.Value) = (WithdrawReceiptTag.Err, value); }

public UnboundedUInt? Ok { get => this.Tag == WithdrawReceiptTag.Ok ? (UnboundedUInt)this.Value! : default; set => (this.Tag, this.Value) = (WithdrawReceiptTag.Ok, value); }

public WithdrawReceipt(WithdrawReceiptTag tag, object? value)
{
this.Tag = tag;
Expand All @@ -23,43 +26,11 @@ public WithdrawReceipt(WithdrawReceiptTag tag, object? value)
protected WithdrawReceipt()
{
}

public static WithdrawReceipt Err(WithdrawErr info)
{
return new WithdrawReceipt(WithdrawReceiptTag.Err, info);
}

public static WithdrawReceipt Ok(UnboundedUInt info)
{
return new WithdrawReceipt(WithdrawReceiptTag.Ok, info);
}

public WithdrawErr AsErr()
{
this.ValidateTag(WithdrawReceiptTag.Err);
return (WithdrawErr)this.Value!;
}

public UnboundedUInt AsOk()
{
this.ValidateTag(WithdrawReceiptTag.Ok);
return (UnboundedUInt)this.Value!;
}

private void ValidateTag(WithdrawReceiptTag tag)
{
if (!this.Tag.Equals(tag))
{
throw new InvalidOperationException($"Cannot cast '{this.Tag}' to type '{tag}'");
}
}
}

public enum WithdrawReceiptTag
{
[VariantOptionType(typeof(WithdrawErr))]
Err,
[VariantOptionType(typeof(UnboundedUInt))]
Ok
}
}
6 changes: 3 additions & 3 deletions samples/Sample.Shared/Governance/Models/AccountId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Sample.Shared.Governance.Models
public class AccountId
{
[CandidName("hash")]
public byte[] Hazh { get; set; }
public byte[] Hash { get; set; }

public AccountId(byte[] hazh)
public AccountId(byte[] hash)
{
this.Hazh = hazh;
this.Hash = hash;
}

public AccountId()
Expand Down
26 changes: 7 additions & 19 deletions samples/Sample.Shared/Governance/Models/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sample.Shared.Governance.Models
{
[Variant(typeof(ActionTag))]
[Variant()]
public class Action
{
[VariantTagProperty()]
Expand All @@ -23,9 +23,9 @@ protected Action()
{
}

public static Action RegisterKnownNeuron(KnownNeuron info)
public static Action RegisterNeuron(KnownNeuron info)
{
return new Action(ActionTag.RegisterKnownNeuron, info);
return new Action(ActionTag.RegisterNeuron, info);
}

public static Action ManageNeuron(ManageNeuron info)
Expand Down Expand Up @@ -88,9 +88,9 @@ public static Action Motion(Motion info)
return new Action(ActionTag.Motion, info);
}

public KnownNeuron AsRegisterKnownNeuron()
public KnownNeuron AsRegisterNeuron()
{
this.ValidateTag(ActionTag.RegisterKnownNeuron);
this.ValidateTag(ActionTag.RegisterNeuron);
return (KnownNeuron)this.Value!;
}

Expand Down Expand Up @@ -177,31 +177,19 @@ private void ValidateTag(ActionTag tag)

public enum ActionTag
{
[VariantOptionType(typeof(KnownNeuron))]
RegisterKnownNeuron,
[VariantOptionType(typeof(ManageNeuron))]
[CandidName("RegisterKnownNeuron")]
RegisterNeuron,
ManageNeuron,
[VariantOptionType(typeof(CreateServiceNervousSystem))]
CreateServiceNervousSystem,
[VariantOptionType(typeof(ExecuteNnsFunction))]
ExecuteNnsFunction,
[VariantOptionType(typeof(RewardNodeProvider))]
RewardNodeProvider,
[VariantOptionType(typeof(OpenSnsTokenSwap))]
OpenSnsTokenSwap,
[VariantOptionType(typeof(SetSnsTokenSwapOpenTimeWindow))]
SetSnsTokenSwapOpenTimeWindow,
[VariantOptionType(typeof(SetDefaultFollowees))]
SetDefaultFollowees,
[VariantOptionType(typeof(RewardNodeProviders))]
RewardNodeProviders,
[VariantOptionType(typeof(NetworkEconomics))]
ManageNetworkEconomics,
[VariantOptionType(typeof(ApproveGenesisKyc))]
ApproveGenesisKyc,
[VariantOptionType(typeof(AddOrRemoveNodeProvider))]
AddOrRemoveNodeProvider,
[VariantOptionType(typeof(Motion))]
Motion
}
}
5 changes: 1 addition & 4 deletions samples/Sample.Shared/Governance/Models/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sample.Shared.Governance.Models
{
[Variant(typeof(ByTag))]
[Variant()]
public class By
{
[VariantTagProperty()]
Expand Down Expand Up @@ -74,11 +74,8 @@ public NeuronIdOrSubaccountInfo()

public enum ByTag
{
[VariantOptionType(typeof(By.NeuronIdOrSubaccountInfo))]
NeuronIdOrSubaccount,
[VariantOptionType(typeof(ClaimOrRefreshNeuronFromAccount))]
MemoAndController,
[VariantOptionType(typeof(ulong))]
Memo
}
}
Loading

0 comments on commit 8f45388

Please sign in to comment.