Skip to content

Commit

Permalink
Tuple serialization (#53)
Browse files Browse the repository at this point in the history
* Fixing tuples

Fixes #52

* Update candid-client.toml
  • Loading branch information
Gekctek authored Mar 23, 2023
1 parent b8c380c commit 167bf90
Show file tree
Hide file tree
Showing 30 changed files with 481 additions and 311 deletions.
16 changes: 8 additions & 8 deletions samples/Sample.Shared/Governance/Models/Governance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public Governance()

public class DefaultFolloweesItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public int F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Followees F1 { get; set; }

public DefaultFolloweesItemRecord(int f0, Followees f1)
Expand All @@ -100,10 +100,10 @@ public DefaultFolloweesItemRecord()

public class ProposalsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public ProposalData F1 { get; set; }

public ProposalsItemRecord(ulong f0, ProposalData f1)
Expand All @@ -119,10 +119,10 @@ public ProposalsItemRecord()

public class InFlightCommandsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public NeuronInFlightCommand F1 { get; set; }

public InFlightCommandsItemRecord(ulong f0, NeuronInFlightCommand f1)
Expand All @@ -138,10 +138,10 @@ public InFlightCommandsItemRecord()

public class NeuronsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Neuron F1 { get; set; }

public NeuronsItemRecord(ulong f0, Neuron f1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public GovernanceCachedMetrics()

public class NotDissolvingNeuronsE8sBucketsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public double F1 { get; set; }

public NotDissolvingNeuronsE8sBucketsItemRecord(ulong f0, double f1)
Expand All @@ -107,10 +107,10 @@ public NotDissolvingNeuronsE8sBucketsItemRecord()

public class NotDissolvingNeuronsCountBucketsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public ulong F1 { get; set; }

public NotDissolvingNeuronsCountBucketsItemRecord(ulong f0, ulong f1)
Expand All @@ -126,10 +126,10 @@ public NotDissolvingNeuronsCountBucketsItemRecord()

public class DissolvingNeuronsCountBucketsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public ulong F1 { get; set; }

public DissolvingNeuronsCountBucketsItemRecord(ulong f0, ulong f1)
Expand All @@ -145,10 +145,10 @@ public DissolvingNeuronsCountBucketsItemRecord()

public class DissolvingNeuronsE8sBucketsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public double F1 { get; set; }

public DissolvingNeuronsE8sBucketsItemRecord(ulong f0, double f1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public ListNeuronsResponse()

public class NeuronInfosItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public NeuronInfo F1 { get; set; }

public NeuronInfosItemRecord(ulong f0, NeuronInfo f1)
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample.Shared/Governance/Models/Neuron.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public Neuron()

public class FolloweesItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public int F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Followees F1 { get; set; }

public FolloweesItemRecord(int f0, Followees f1)
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample.Shared/Governance/Models/ProposalData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public ProposalData()

public class BallotsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Ballot F1 { get; set; }

public BallotsItemRecord(ulong f0, Ballot f1)
Expand Down
4 changes: 2 additions & 2 deletions samples/Sample.Shared/Governance/Models/ProposalInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ public ProposalInfo()

public class BallotsItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public ulong F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Ballot F1 { get; set; }

public BallotsItemRecord(ulong f0, Ballot f1)
Expand Down
6 changes: 5 additions & 1 deletion samples/Sample.Shared/Governance/Models/RewardEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ public class RewardEvent
[CandidName("actual_timestamp_seconds")]
public ulong ActualTimestampSeconds { get; set; }

[CandidName("total_available_e8s_equivalent")]
public ulong TotalAvailableE8sEquivalent { get; set; }

[CandidName("distributed_e8s_equivalent")]
public ulong DistributedE8sEquivalent { get; set; }

[CandidName("settled_proposals")]
public List<NeuronId> SettledProposals { get; set; }

public RewardEvent(ulong dayAfterGenesis, ulong actualTimestampSeconds, ulong distributedE8sEquivalent, List<NeuronId> settledProposals)
public RewardEvent(ulong dayAfterGenesis, ulong actualTimestampSeconds, ulong totalAvailableE8sEquivalent, ulong distributedE8sEquivalent, List<NeuronId> settledProposals)
{
this.DayAfterGenesis = dayAfterGenesis;
this.ActualTimestampSeconds = actualTimestampSeconds;
this.TotalAvailableE8sEquivalent = totalAvailableE8sEquivalent;
this.DistributedE8sEquivalent = distributedE8sEquivalent;
this.SettledProposals = settledProposals;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public SetDefaultFollowees()

public class DefaultFolloweesItemRecord
{
[CandidName("0")]
[CandidTag(0U)]
public int F0 { get; set; }

[CandidName("1")]
[CandidTag(1U)]
public Followees F1 { get; set; }

public DefaultFolloweesItemRecord(int f0, Followees f1)
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions samples/Sample.Shared/candid-client.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ feature-nullable = true
[[clients]]
name = "Dex"
type = "file"
file-path = "../ServiceDefinitionFiles/Dex.did"
file-path = "ServiceDefinitions/Dex.did"

[[clients]]
name = "Governance"
Expand All @@ -16,7 +16,7 @@ canister-id = "rrkah-fqaaa-aaaaa-aaaaq-cai"
[[clients]]
name = "AddressBook"
type = "file"
file-path = "../ServiceDefinitionFiles/AddressBook.did"
file-path = "ServiceDefinitions/AddressBook.did"
output-directory = "C:/Git/ICP.NET/samples/Sample.Shared/Address" # override
no-folders = true
keep-candid-case = true
59 changes: 51 additions & 8 deletions src/Candid/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
- [Type](#P-EdjCase-ICP-Candid-Models-Types-CandidKnownType-Type 'EdjCase.ICP.Candid.Models.Types.CandidKnownType.Type')
- [CandidNameAttribute](#T-EdjCase-ICP-Candid-Mapping-CandidNameAttribute 'EdjCase.ICP.Candid.Mapping.CandidNameAttribute')
- [#ctor(name)](#M-EdjCase-ICP-Candid-Mapping-CandidNameAttribute-#ctor-System-String- 'EdjCase.ICP.Candid.Mapping.CandidNameAttribute.#ctor(System.String)')
- [Name](#P-EdjCase-ICP-Candid-Mapping-CandidNameAttribute-Name 'EdjCase.ICP.Candid.Mapping.CandidNameAttribute.Name')
- [CandidOptional](#T-EdjCase-ICP-Candid-Models-Values-CandidOptional 'EdjCase.ICP.Candid.Models.Values.CandidOptional')
- [#ctor(value)](#M-EdjCase-ICP-Candid-Models-Values-CandidOptional-#ctor-EdjCase-ICP-Candid-Models-Values-CandidValue- 'EdjCase.ICP.Candid.Models.Values.CandidOptional.#ctor(EdjCase.ICP.Candid.Models.Values.CandidValue)')
- [Type](#P-EdjCase-ICP-Candid-Models-Values-CandidOptional-Type 'EdjCase.ICP.Candid.Models.Values.CandidOptional.Type')
Expand Down Expand Up @@ -205,6 +204,11 @@
- [op_Implicit(id)](#M-EdjCase-ICP-Candid-Models-CandidTag-op_Implicit-System-UInt32-~EdjCase-ICP-Candid-Models-CandidTag 'EdjCase.ICP.Candid.Models.CandidTag.op_Implicit(System.UInt32)~EdjCase.ICP.Candid.Models.CandidTag')
- [op_Implicit(tag)](#M-EdjCase-ICP-Candid-Models-CandidTag-op_Implicit-EdjCase-ICP-Candid-Models-CandidTag-~System-UInt32 'EdjCase.ICP.Candid.Models.CandidTag.op_Implicit(EdjCase.ICP.Candid.Models.CandidTag)~System.UInt32')
- [op_Inequality()](#M-EdjCase-ICP-Candid-Models-CandidTag-op_Inequality-EdjCase-ICP-Candid-Models-CandidTag,EdjCase-ICP-Candid-Models-CandidTag- 'EdjCase.ICP.Candid.Models.CandidTag.op_Inequality(EdjCase.ICP.Candid.Models.CandidTag,EdjCase.ICP.Candid.Models.CandidTag)')
- [CandidTagAttribute](#T-EdjCase-ICP-Candid-Mapping-CandidTagAttribute 'EdjCase.ICP.Candid.Mapping.CandidTagAttribute')
- [#ctor(tag)](#M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-EdjCase-ICP-Candid-Models-CandidTag- 'EdjCase.ICP.Candid.Mapping.CandidTagAttribute.#ctor(EdjCase.ICP.Candid.Models.CandidTag)')
- [#ctor(id)](#M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-System-UInt32- 'EdjCase.ICP.Candid.Mapping.CandidTagAttribute.#ctor(System.UInt32)')
- [#ctor(name)](#M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-System-String- 'EdjCase.ICP.Candid.Mapping.CandidTagAttribute.#ctor(System.String)')
- [Tag](#P-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-Tag 'EdjCase.ICP.Candid.Mapping.CandidTagAttribute.Tag')
- [CandidTextParseException](#T-EdjCase-ICP-Candid-Exceptions-CandidTextParseException 'EdjCase.ICP.Candid.Exceptions.CandidTextParseException')
- [Message](#P-EdjCase-ICP-Candid-Exceptions-CandidTextParseException-Message 'EdjCase.ICP.Candid.Exceptions.CandidTextParseException.Message')
- [CandidType](#T-EdjCase-ICP-Candid-Models-Types-CandidType 'EdjCase.ICP.Candid.Models.Types.CandidType')
Expand Down Expand Up @@ -1820,13 +1824,6 @@ the serializers will use the property names
| ---- | ---- | ----------- |
| name | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The name to use for serialization of candid values |

<a name='P-EdjCase-ICP-Candid-Mapping-CandidNameAttribute-Name'></a>
### Name `property`

##### Summary

The name to use for serialization of candid values

<a name='T-EdjCase-ICP-Candid-Models-Values-CandidOptional'></a>
## CandidOptional `type`

Expand Down Expand Up @@ -3137,6 +3134,52 @@ Converts a candid tag value to a uint by using the id of the tag

This method has no parameters.

<a name='T-EdjCase-ICP-Candid-Mapping-CandidTagAttribute'></a>
## CandidTagAttribute `type`

##### Namespace

EdjCase.ICP.Candid.Mapping

##### Summary

An attribute to specify a candid tag to use for serialization. If unspecified
the serializers will use the property names

<a name='M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-EdjCase-ICP-Candid-Models-CandidTag-'></a>
### #ctor(tag) `constructor`

##### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| tag | [EdjCase.ICP.Candid.Models.CandidTag](#T-EdjCase-ICP-Candid-Models-CandidTag 'EdjCase.ICP.Candid.Models.CandidTag') | The tag to use for serialization of candid values |

<a name='M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-System-UInt32-'></a>
### #ctor(id) `constructor`

##### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| id | [System.UInt32](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.UInt32 'System.UInt32') | The tag id (name hash) to use for serialization of candid values |

<a name='M-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-#ctor-System-String-'></a>
### #ctor(name) `constructor`

##### Parameters

| Name | Type | Description |
| ---- | ---- | ----------- |
| name | [System.String](http://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k:System.String 'System.String') | The tag name to use for serialization of candid values |

<a name='P-EdjCase-ICP-Candid-Mapping-CandidTagAttribute-Tag'></a>
### Tag `property`

##### Summary

The tag to use for serialization of candid values

<a name='T-EdjCase-ICP-Candid-Exceptions-CandidTextParseException'></a>
## CandidTextParseException `type`

Expand Down
23 changes: 19 additions & 4 deletions src/Candid/API.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 11 additions & 12 deletions src/Candid/Mapping/IResolvableTypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,11 @@ private static IResolvableTypeInfo BuildEnumVariant(Type enumType)
{
string tagName = Enum.GetName(enumType, e);
MemberInfo field = enumType.GetMember(tagName).First();
var nameAttribute = field.GetCustomAttribute<CandidNameAttribute>();
string name = nameAttribute?.Name ?? tagName;
return (Name: CandidTag.FromName(name), Value: e);
var tagAttribute = field.GetCustomAttribute<CandidTagAttribute>();
CandidTag tag = tagAttribute?.Tag ?? CandidTag.FromName(tagName);
return (Tag: tag, Value: e);
})
.ToDictionary(e => e.Name, e => e.Value);
.ToDictionary(e => e.Tag, e => e.Value);
var candidType = new CandidVariantType(options.ToDictionary(o => o.Key, o => (CandidType)CandidType.Null()));
var mapper = new VariantEnumMapper(candidType, enumType, options);
return new ResolvedTypeInfo(enumType, candidType, mapper);
Expand All @@ -409,9 +409,9 @@ private static IResolvableTypeInfo BuildVariant(Type objType, VariantAttribute a
MemberInfo field = attribute.TagType.GetMember(tagName).First();
var typeAttribute = field.GetCustomAttribute<VariantOptionTypeAttribute>();
Type? optionType = typeAttribute?.OptionType;
var nameAttribute = field.GetCustomAttribute<CandidNameAttribute>();
string name = nameAttribute?.Name ?? tagName;
return (CandidTag.FromName(name), new VariantMapper.Option(tagEnum, optionType));
var tagAttribute = field.GetCustomAttribute<CandidTagAttribute>();
CandidTag tag = tagAttribute?.Tag ?? CandidTag.FromName(tagName);
return (tag, new VariantMapper.Option(tagEnum, optionType));
})
.ToDictionary(k => k.Item1, k => k.Item2);

Expand Down Expand Up @@ -476,17 +476,16 @@ private static IResolvableTypeInfo BuildRecord(Type objType)
// Ignore property
continue;
}
CandidNameAttribute? propertyAttribute = property.GetCustomAttribute<CandidNameAttribute>();
string propertyName;
CandidTagAttribute? propertyAttribute = property.GetCustomAttribute<CandidTagAttribute>();
CandidTag tag;
if (propertyAttribute != null)
{
propertyName = propertyAttribute.Name;
tag = propertyAttribute.Tag;
}
else
{
propertyName = property.Name;
tag = CandidTag.FromName(property.Name);
}
CandidTag tag = CandidTag.FromName(propertyName);
CustomMapperAttribute? customMapperAttribute = property.GetCustomAttribute<CustomMapperAttribute>();

PropertyMetaData propertyMetaData = new(property, customMapperAttribute?.Mapper);
Expand Down
Loading

0 comments on commit 167bf90

Please sign in to comment.