-
Notifications
You must be signed in to change notification settings - Fork 160
/
DpgOutputLibrary.cs
160 lines (132 loc) · 7.44 KB
/
DpgOutputLibrary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using AutoRest.CSharp.Common.Input;
using AutoRest.CSharp.Common.Output.Models.Types;
using AutoRest.CSharp.Generation.Types;
using AutoRest.CSharp.Input;
using AutoRest.CSharp.Input.Source;
using AutoRest.CSharp.LowLevel.Output;
using AutoRest.CSharp.LowLevel.Output.Samples;
using AutoRest.CSharp.LowLevel.Output.Tests;
namespace AutoRest.CSharp.Output.Models.Types
{
internal class DpgOutputLibrary : OutputLibrary
{
private readonly string _libraryName;
private readonly IReadOnlyDictionary<InputEnumType, EnumType> _enums;
private readonly IReadOnlyDictionary<InputModelType, ModelTypeProvider> _models;
private readonly bool _isTspInput;
private readonly SourceInputModel? _sourceInputModel;
public TypeFactory TypeFactory { get; }
public IEnumerable<EnumType> Enums => _enums.Values;
public IEnumerable<ModelTypeProvider> Models
{
get
{
// Skip the replaced model, e.g. the replaced ErrorResponse.
foreach (var (key, model) in _models)
{
var type = TypeFactory.CreateType(key);
if (type is { IsFrameworkType: false, Implementation: ModelTypeProvider implementation} && model == implementation)
{
yield return model;
}
}
}
}
public IReadOnlyList<LowLevelClient> RestClients { get; }
public ClientOptionsTypeProvider ClientOptions { get; }
public IEnumerable<TypeProvider> AllModels => new List<TypeProvider>(_enums.Values).Concat(Models);
public DpgOutputLibrary(string libraryName, IReadOnlyDictionary<InputEnumType, EnumType> enums, IReadOnlyDictionary<InputModelType, ModelTypeProvider> models, IReadOnlyList<LowLevelClient> restClients, ClientOptionsTypeProvider clientOptions, bool isTspInput, SourceInputModel? sourceInputModel)
{
TypeFactory = new TypeFactory(this, typeof(BinaryData));
_libraryName = libraryName;
_enums = enums;
_models = models;
_isTspInput = isTspInput;
_sourceInputModel = sourceInputModel;
RestClients = restClients;
ClientOptions = clientOptions;
}
private IEnumerable<string>? _accessOverriddenModels;
public IEnumerable<string> AccessOverriddenModels => _accessOverriddenModels ??= Enums.Where(e => e.IsAccessibilityOverridden).Select(e => e.Declaration.Name)
.Concat(Models.Where(m => m.IsAccessibilityOverridden).Select(m => m.Declaration.Name));
private AspDotNetExtensionTypeProvider? _aspDotNetExtension;
public AspDotNetExtensionTypeProvider AspDotNetExtension => _aspDotNetExtension ??= new AspDotNetExtensionTypeProvider(RestClients, Configuration.Namespace, _sourceInputModel);
private ModelFactoryTypeProvider? _modelFactoryProvider;
public ModelFactoryTypeProvider? ModelFactory => _modelFactoryProvider ??= ModelFactoryTypeProvider.TryCreate(AllModels, TypeFactory, _sourceInputModel);
private DpgTestBaseProvider? _dpgTestBase;
public DpgTestBaseProvider DpgTestBase => _dpgTestBase ??= new DpgTestBaseProvider(Configuration.Namespace, RestClients, DpgTestEnvironment, _sourceInputModel);
private DpgTestEnvironmentProvider? _dpgTestEnvironment;
public DpgTestEnvironmentProvider DpgTestEnvironment => _dpgTestEnvironment ??= new DpgTestEnvironmentProvider(Configuration.Namespace, _sourceInputModel);
private Dictionary<LowLevelClient, DpgClientTestProvider>? _dpgClientTestProviders;
private Dictionary<LowLevelClient, DpgClientTestProvider> DpgClientTestProviders => _dpgClientTestProviders ??= EnsureDpgClientTestProviders();
private Dictionary<LowLevelClient, DpgClientTestProvider> EnsureDpgClientTestProviders()
{
var result = new Dictionary<LowLevelClient, DpgClientTestProvider>();
foreach (var client in RestClients)
{
DpgClientTestProvider testCaseProvider = Configuration.IsBranded ?
new DpgClientRecordedTestProvider(Configuration.Namespace, client, DpgTestBase, _sourceInputModel) :
new SmokeTestProvider(Configuration.Namespace, client, _sourceInputModel);
if (!testCaseProvider.IsEmpty)
{
result.Add(client, testCaseProvider);
}
}
return result;
}
public DpgClientTestProvider? GetTestForClient(LowLevelClient client) => DpgClientTestProviders.TryGetValue(client, out var test) ? test : null;
private Dictionary<LowLevelClient, DpgClientSampleProvider>? _dpgClientSampleProviders;
private Dictionary<LowLevelClient, DpgClientSampleProvider> DpgClientSampleProviders => _dpgClientSampleProviders ??= EnsureDpgSampleProviders();
private Dictionary<LowLevelClient, DpgClientSampleProvider> EnsureDpgSampleProviders()
{
var result = new Dictionary<LowLevelClient, DpgClientSampleProvider>();
// we do not write samples if the library is not branded, or samples are turned off
if (!Configuration.IsBranded || !Configuration.GenerateSampleProject)
return result;
foreach (var client in RestClients)
{
var sampleProvider = new DpgClientSampleProvider(Configuration.Namespace, client, _sourceInputModel);
if (!sampleProvider.IsEmpty)
result.Add(client, sampleProvider);
}
return result;
}
public DpgClientSampleProvider? GetSampleForClient(LowLevelClient client)
=> DpgClientSampleProviders.TryGetValue(client, out var sample) ? sample : null;
public override CSharpType ResolveEnum(InputEnumType enumType)
{
if (!_isTspInput || enumType.Usage == InputModelTypeUsage.None)
{
return TypeFactory.CreateType(enumType.EnumValueType);
}
if (_enums.TryGetValue(enumType, out var typeProvider))
{
return typeProvider.Type;
}
throw new InvalidOperationException($"No {nameof(EnumType)} has been created for `{enumType.Name}` {nameof(InputEnumType)}.");
}
public override CSharpType ResolveModel(InputModelType model)
=> _models.TryGetValue(model, out var modelTypeProvider) ? modelTypeProvider.Type : new CSharpType(typeof(object), model.IsNullable);
public override CSharpType? FindTypeByName(string originalName)
{
foreach (var model in Models)
{
if (model.Declaration.Name == originalName)
return model.Type;
}
foreach (var e in Enums)
{
if (e.Declaration.Name == originalName)
return e.Type;
}
return null;
}
public override CSharpType FindTypeForSchema(Schema schema) => throw new NotImplementedException($"{nameof(FindTypeForSchema)} shouldn't be called for DPG!");
public override TypeProvider FindTypeProviderForSchema(Schema schema) => throw new NotImplementedException($"{nameof(FindTypeForSchema)} shouldn't be called for DPG!");
}
}