From 6e57b606bff9e9e2f829566831685bf887b3328c Mon Sep 17 00:00:00 2001 From: Ethan Celletti Date: Tue, 14 Feb 2023 12:53:58 -0800 Subject: [PATCH] Adding empty constructors --- src/ClientGenerator/RoslynTypeResolver.cs | 24 +- .../SyntaxRewriters/NamespacePrefixRemover.cs | 11 - .../Generators/__snapshots__/Dex.snap | 8 + .../Generators/__snapshots__/Governance.snap | 300 ++++++++++++++++++ 4 files changed, 321 insertions(+), 22 deletions(-) diff --git a/src/ClientGenerator/RoslynTypeResolver.cs b/src/ClientGenerator/RoslynTypeResolver.cs index 2489c9d7..d616b8ad 100644 --- a/src/ClientGenerator/RoslynTypeResolver.cs +++ b/src/ClientGenerator/RoslynTypeResolver.cs @@ -238,7 +238,7 @@ public ClassDeclarationSyntax GenerateClient(TypeName clientName, ServiceSourceC properties: properties, methods: methods, attributes: attributes, - emptyReflectionContructor: true, + emptyConstructorAccess: AccessType.Protected, subTypes: resolvedOptions .SelectMany(o => o.Type?.GeneratedSyntax ?? Array.Empty()) .ToList() @@ -526,7 +526,8 @@ internal ClassDeclarationSyntax GenerateRecord(TypeName recordTypeName, RecordSo return GenerateClass( name: recordTypeName, properties: properties, - subTypes: subItems + subTypes: subItems, + emptyConstructorAccess: AccessType.Public ); } @@ -1193,7 +1194,7 @@ private static ClassDeclarationSyntax GenerateClass( List? methods = null, List? implementTypes = null, List? attributes = null, - bool emptyReflectionContructor = false, + AccessType? emptyConstructorAccess = null, List? subTypes = null) { if (properties.Any(p => p.Type == null)) @@ -1201,20 +1202,21 @@ private static ClassDeclarationSyntax GenerateClass( // TODO throw new NotImplementedException("Null, empty or reserved properties are not yet supported"); } - IEnumerable properySyntxList = properties + List constructors = new(); + IEnumerable properySyntaxList = properties .Select(GenerateProperty) .Where(p => p != null)!; - - List constructors = new() + if (properties.Any()) { - GenerateConstructor(name, AccessType.Public, properties) - }; - if (emptyReflectionContructor) + // Only create constructor if there are properties + constructors.Add(GenerateConstructor(name, AccessType.Public, properties)); + } + if (emptyConstructorAccess != null) { // Empty Constrcutor for reflection constructors.Add(GenerateConstructor( name: name, - access: AccessType.Protected, + access: emptyConstructorAccess.Value, properties: new List() )); } @@ -1226,7 +1228,7 @@ private static ClassDeclarationSyntax GenerateClass( )) .WithMembers(SyntaxFactory.List( // Properties - properySyntxList.Cast() + properySyntaxList.Cast() // Constructors .Concat(constructors.Cast()) // Methods diff --git a/src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs b/src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs index 400683d4..014e8b90 100644 --- a/src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs +++ b/src/ClientGenerator/SyntaxRewriters/NamespacePrefixRemover.cs @@ -22,17 +22,6 @@ public NamespacePrefixRemover(string modelNamespace) this.ModelNamespace = modelNamespace; } - [return: NotNullIfNotNull("node")] - public override SyntaxNode? Visit(SyntaxNode? node) - { - string? a = node?.ToString(); - if (a != null && a.StartsWith("EdjCase.ICP.Agent.Agents")) - { - int b = 2; - } - return base.Visit(node); - } - public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) { return this.VisitInternal(node, base.VisitIdentifierName); diff --git a/test/Common.Tests/Generators/__snapshots__/Dex.snap b/test/Common.Tests/Generators/__snapshots__/Dex.snap index c8d7510a..0d5fbee4 100644 --- a/test/Common.Tests/Generators/__snapshots__/Dex.snap +++ b/test/Common.Tests/Generators/__snapshots__/Dex.snap @@ -404,6 +404,10 @@ namespace Test.Models this.To = to; this.ToAmount = toAmount; } + + public Order() + { + } } } @@ -674,5 +678,9 @@ namespace Test.Models this.Owner = owner; this.Token = token; } + + public Balance() + { + } } } diff --git a/test/Common.Tests/Generators/__snapshots__/Governance.snap b/test/Common.Tests/Generators/__snapshots__/Governance.snap index 9eddc747..f41ae2fb 100644 --- a/test/Common.Tests/Generators/__snapshots__/Governance.snap +++ b/test/Common.Tests/Generators/__snapshots__/Governance.snap @@ -192,6 +192,10 @@ namespace Test.Models { this.Hash = hash; } + + public AccountIdentifier() + { + } } } @@ -392,6 +396,10 @@ namespace Test.Models { this.NewHotKey = newHotKey; } + + public AddHotKey() + { + } } } @@ -412,6 +420,10 @@ namespace Test.Models { this.Change = change; } + + public AddOrRemoveNodeProvider() + { + } } } @@ -431,6 +443,10 @@ namespace Test.Models { this.E8s = e8s; } + + public Amount() + { + } } } @@ -451,6 +467,10 @@ namespace Test.Models { this.Principals = principals; } + + public ApproveGenesisKyc() + { + } } } @@ -474,6 +494,10 @@ namespace Test.Models this.Vote = vote; this.VotingPower = votingPower; } + + public Ballot() + { + } } } @@ -499,6 +523,10 @@ namespace Test.Models this.Vote = vote; this.ProposalId = proposalId; } + + public BallotInfo() + { + } } } @@ -678,6 +706,10 @@ namespace Test.Models { this.By = by; } + + public ClaimOrRefresh() + { + } } } @@ -702,6 +734,10 @@ namespace Test.Models this.Controller = controller; this.Memo = memo; } + + public ClaimOrRefreshNeuronFromAccount() + { + } } } @@ -722,6 +758,10 @@ namespace Test.Models { this.Result = result; } + + public ClaimOrRefreshNeuronFromAccountResponse() + { + } } } @@ -742,6 +782,10 @@ namespace Test.Models { this.RefreshedNeuronId = refreshedNeuronId; } + + public ClaimOrRefreshResponse() + { + } } } @@ -1347,6 +1391,10 @@ namespace Test.Models { this.Operation = operation; } + + public Configure() + { + } } } @@ -1371,6 +1419,10 @@ namespace Test.Models this.ToAccount = toAccount; this.Amount = amount; } + + public Disburse() + { + } } } @@ -1390,6 +1442,10 @@ namespace Test.Models { this.TransferBlockHeight = transferBlockHeight; } + + public DisburseResponse() + { + } } } @@ -1426,6 +1482,10 @@ namespace Test.Models this.NewController = newController; this.Nonce = nonce; } + + public DisburseToNeuron() + { + } } } @@ -1519,6 +1579,10 @@ namespace Test.Models this.NnsFunction = nnsFunction; this.Payload = payload; } + + public ExecuteNnsFunction() + { + } } } @@ -1544,6 +1608,10 @@ namespace Test.Models this.Topic = topic; this.Followees = followees; } + + public Follow() + { + } } } @@ -1564,6 +1632,10 @@ namespace Test.Models { this.Followees_ = followees; } + + public Followees() + { + } } } @@ -1631,6 +1703,10 @@ namespace Test.Models this.GenesisTimestampSeconds = genesisTimestampSeconds; } + public Governance() + { + } + public class GovernanceR0V0 { [CandidName("0")] @@ -1644,6 +1720,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceR0V0() + { + } } public class GovernanceR8V0 @@ -1659,6 +1739,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceR8V0() + { + } } public class GovernanceR9V0 @@ -1674,6 +1758,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceR9V0() + { + } } public class GovernanceR10V0 @@ -1689,6 +1777,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceR10V0() + { + } } } } @@ -1772,6 +1864,10 @@ namespace Test.Models this.TimestampSeconds = timestampSeconds; } + public GovernanceCachedMetrics() + { + } + public class GovernanceCachedMetricsR0V0 { [CandidName("0")] @@ -1785,6 +1881,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceCachedMetricsR0V0() + { + } } public class GovernanceCachedMetricsR3V0 @@ -1800,6 +1900,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceCachedMetricsR3V0() + { + } } public class GovernanceCachedMetricsR11V0 @@ -1815,6 +1919,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceCachedMetricsR11V0() + { + } } public class GovernanceCachedMetricsR13V0 @@ -1830,6 +1938,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public GovernanceCachedMetricsR13V0() + { + } } } } @@ -1854,6 +1966,10 @@ namespace Test.Models this.ErrorMessage = errorMessage; this.ErrorType = errorType; } + + public GovernanceError() + { + } } } @@ -1873,6 +1989,10 @@ namespace Test.Models { this.AdditionalDissolveDelaySeconds = additionalDissolveDelaySeconds; } + + public IncreaseDissolveDelay() + { + } } } @@ -1897,6 +2017,10 @@ namespace Test.Models this.Id = id; this.KnownNeuronData = knownNeuronData; } + + public KnownNeuron() + { + } } } @@ -1921,6 +2045,10 @@ namespace Test.Models this.Name = name; this.Description = description; } + + public KnownNeuronData() + { + } } } @@ -1941,6 +2069,10 @@ namespace Test.Models { this.KnownNeurons = knownNeurons; } + + public ListKnownNeuronsResponse() + { + } } } @@ -1965,6 +2097,10 @@ namespace Test.Models this.NeuronIds = neuronIds; this.IncludeNeuronsReadableByCaller = includeNeuronsReadableByCaller; } + + public ListNeurons() + { + } } } @@ -1991,6 +2127,10 @@ namespace Test.Models this.FullNeurons = fullNeurons; } + public ListNeuronsResponse() + { + } + public class ListNeuronsResponseR0V0 { [CandidName("0")] @@ -2004,6 +2144,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public ListNeuronsResponseR0V0() + { + } } } } @@ -2025,6 +2169,10 @@ namespace Test.Models { this.NodeProviders = nodeProviders; } + + public ListNodeProvidersResponse() + { + } } } @@ -2063,6 +2211,10 @@ namespace Test.Models this.ExcludeTopic = excludeTopic; this.IncludeStatus = includeStatus; } + + public ListProposalInfo() + { + } } } @@ -2083,6 +2235,10 @@ namespace Test.Models { this.ProposalInfo = proposalInfo; } + + public ListProposalInfoResponse() + { + } } } @@ -2103,6 +2259,10 @@ namespace Test.Models { this.ProposalId = proposalId; } + + public MakeProposalResponse() + { + } } } @@ -2131,6 +2291,10 @@ namespace Test.Models this.Command = command; this.NeuronIdOrSubaccount = neuronIdOrSubaccount; } + + public ManageNeuron() + { + } } } @@ -2151,6 +2315,10 @@ namespace Test.Models { this.Command = command; } + + public ManageNeuronResponse() + { + } } } @@ -2171,6 +2339,10 @@ namespace Test.Models { this.SourceNeuronId = sourceNeuronId; } + + public Merge() + { + } } } @@ -2190,6 +2362,10 @@ namespace Test.Models { this.PercentageToMerge = percentageToMerge; } + + public MergeMaturity() + { + } } } @@ -2213,6 +2389,10 @@ namespace Test.Models this.MergedMaturityE8s = mergedMaturityE8s; this.NewStakeE8s = newStakeE8s; } + + public MergeMaturityResponse() + { + } } } @@ -2232,6 +2412,10 @@ namespace Test.Models { this.MotionText = motionText; } + + public Motion() + { + } } } @@ -2279,6 +2463,10 @@ namespace Test.Models this.MinimumIcpXdrRate = minimumIcpXdrRate; this.MaximumNodeProviderRewardsE8s = maximumNodeProviderRewardsE8s; } + + public NetworkEconomics() + { + } } } @@ -2366,6 +2554,10 @@ namespace Test.Models this.KnownNeuronData = knownNeuronData; } + public Neuron() + { + } + public class NeuronR13V0 { [CandidName("0")] @@ -2379,6 +2571,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public NeuronR13V0() + { + } } } } @@ -2399,6 +2595,10 @@ namespace Test.Models { this.Id = id; } + + public NeuronId() + { + } } } @@ -2494,6 +2694,10 @@ namespace Test.Models this.Command = command; this.Timestamp = timestamp; } + + public NeuronInFlightCommand() + { + } } } @@ -2552,6 +2756,10 @@ namespace Test.Models this.VotingPower = votingPower; this.AgeSeconds = ageSeconds; } + + public NeuronInfo() + { + } } } @@ -2597,6 +2805,10 @@ namespace Test.Models this.TransferTimestamp = transferTimestamp; this.BlockHeight = blockHeight; } + + public NeuronStakeTransfer() + { + } } } @@ -2621,6 +2833,10 @@ namespace Test.Models this.Id = id; this.RewardAccount = rewardAccount; } + + public NodeProvider() + { + } } } @@ -2814,6 +3030,10 @@ namespace Test.Models this.Action = action; this.Summary = summary; } + + public Proposal() + { + } } } @@ -2885,6 +3105,10 @@ namespace Test.Models this.ExecutedTimestampSeconds = executedTimestampSeconds; } + public ProposalData() + { + } + public class ProposalDataR2V0 { [CandidName("0")] @@ -2898,6 +3122,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public ProposalDataR2V0() + { + } } } } @@ -2982,6 +3210,10 @@ namespace Test.Models this.ExecutedTimestampSeconds = executedTimestampSeconds; } + public ProposalInfo() + { + } + public class ProposalInfoR4V0 { [CandidName("0")] @@ -2995,6 +3227,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public ProposalInfoR4V0() + { + } } } } @@ -3021,6 +3257,10 @@ namespace Test.Models this.Vote = vote; this.Proposal = proposal; } + + public RegisterVote() + { + } } } @@ -3040,6 +3280,10 @@ namespace Test.Models { this.HotKeyToRemove = hotKeyToRemove; } + + public RemoveHotKey() + { + } } } @@ -3480,6 +3724,10 @@ namespace Test.Models this.DistributedE8sEquivalent = distributedE8sEquivalent; this.SettledProposals = settledProposals; } + + public RewardEvent() + { + } } } @@ -3578,6 +3826,10 @@ namespace Test.Models this.RewardMode = rewardMode; this.AmountE8s = amountE8s; } + + public RewardNodeProvider() + { + } } } @@ -3604,6 +3856,10 @@ namespace Test.Models this.UseRegistryDerivedRewards = useRegistryDerivedRewards; this.Rewards = rewards; } + + public RewardNodeProviders() + { + } } } @@ -3624,6 +3880,10 @@ namespace Test.Models { this.ToAccount = toAccount; } + + public RewardToAccount() + { + } } } @@ -3643,6 +3903,10 @@ namespace Test.Models { this.DissolveDelaySeconds = dissolveDelaySeconds; } + + public RewardToNeuron() + { + } } } @@ -3665,6 +3929,10 @@ namespace Test.Models this.DefaultFollowees = defaultFollowees; } + public SetDefaultFollowees() + { + } + public class SetDefaultFolloweesR0V0 { [CandidName("0")] @@ -3678,6 +3946,10 @@ namespace Test.Models this.F0 = f0; this.F1 = f1; } + + public SetDefaultFolloweesR0V0() + { + } } } } @@ -3698,6 +3970,10 @@ namespace Test.Models { this.DissolveTimestampSeconds = dissolveTimestampSeconds; } + + public SetDissolveTimestamp() + { + } } } @@ -3726,6 +4002,10 @@ namespace Test.Models this.NewController = newController; this.Nonce = nonce; } + + public Spawn() + { + } } } @@ -3746,6 +4026,10 @@ namespace Test.Models { this.CreatedNeuronId = createdNeuronId; } + + public SpawnResponse() + { + } } } @@ -3765,6 +4049,10 @@ namespace Test.Models { this.AmountE8s = amountE8s; } + + public Split() + { + } } } @@ -3796,6 +4084,10 @@ namespace Test.Models this.Total = total; this.TimestampSeconds = timestampSeconds; } + + public Tally() + { + } } } @@ -3816,6 +4108,10 @@ namespace Test.Models { this.RewardAccount = rewardAccount; } + + public UpdateNodeProvider() + { + } } } @@ -3835,5 +4131,9 @@ namespace Test.Models { this.CurrentDeadlineTimestampSeconds = currentDeadlineTimestampSeconds; } + + public WaitForQuietState() + { + } } }