1
- using System . Collections . Immutable ;
2
1
using AutoSDK . Extensions ;
3
2
using AutoSDK . Models ;
4
3
@@ -11,82 +10,69 @@ public static string GenerateAnyOf(
11
10
CancellationToken cancellationToken = default )
12
11
{
13
12
var types = $ "<{ string . Join ( ", " , Enumerable . Range ( 1 , anyOfData . Count ) . Select ( x => $ "T{ x } ") ) } >";
14
- var classNameWithoutTypes = string . IsNullOrWhiteSpace ( anyOfData . Name )
13
+ var classNameWithoutTypes = ! anyOfData . IsNamed
15
14
? $ "{ anyOfData . SubType } "
16
15
: anyOfData . Name ;
17
- var className = string . IsNullOrWhiteSpace ( anyOfData . Name )
16
+ var className = ! anyOfData . IsNamed
18
17
? $ "{ anyOfData . SubType } { types } "
19
18
: anyOfData . Name ;
20
- var allTypes = anyOfData . Properties . IsEmpty
21
- ? Enumerable
22
- . Range ( 1 , anyOfData . Count )
23
- . Select ( i => PropertyData . Default with
24
- {
25
- Name = $ "Value{ i } ",
26
- Type = TypeData . Default with
27
- {
28
- CSharpTypeRaw = $ "T{ i } ",
29
- } ,
30
- } )
31
- . ToImmutableArray ( ) . AsEquatableArray ( )
32
- : anyOfData . Properties ;
33
19
var validation = anyOfData . SubType switch
34
20
{
35
- "AnyOf" => string . Join ( " || " , allTypes . Select ( x => $ "Is{ x . Name } ") ) ,
36
- "OneOf" => string . Join ( " || " , allTypes . Select ( ( x , xi ) =>
37
- string . Join ( " && " , allTypes . Select ( ( y , yi ) => $ "{ ( yi == xi ? "" : "!" ) } Is{ y . Name } ") ) ) ) ,
38
- "AllOf" => string . Join ( " && " , allTypes . Select ( x => $ "Is{ x . Name } ") ) ,
21
+ "AnyOf" => string . Join ( " || " , anyOfData . Properties . Select ( x => $ "Is{ x . Name } ") ) ,
22
+ "OneOf" => string . Join ( " || " , anyOfData . Properties . Select ( ( x , xi ) =>
23
+ string . Join ( " && " , anyOfData . Properties . Select ( ( y , yi ) => $ "{ ( yi == xi ? "" : "!" ) } Is{ y . Name } ") ) ) ) ,
24
+ "AllOf" => string . Join ( " && " , anyOfData . Properties . Select ( x => $ "Is{ x . Name } ") ) ,
39
25
_ => throw new NotImplementedException ( ) ,
40
26
} ;
41
27
var constructorWithAllValues =
42
28
anyOfData . Count > 1 ||
43
- ( ! string . IsNullOrWhiteSpace ( anyOfData . Name ) &&
29
+ ( anyOfData . IsNamed &&
44
30
anyOfData . DiscriminatorType != null &&
45
31
anyOfData . DiscriminatorPropertyName != null &&
46
- allTypes . All ( x => ! string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ) ? $@ "
32
+ anyOfData . Properties . All ( x => ! string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ) ? $@ "
47
33
{ string . Empty . ToXmlDocumentationSummary ( level : 8 ) }
48
34
public { classNameWithoutTypes } (
49
- { ( string . IsNullOrWhiteSpace ( anyOfData . Name ) ||
35
+ { ( ! anyOfData . IsNamed ||
50
36
anyOfData . DiscriminatorType == null ||
51
37
anyOfData . DiscriminatorPropertyName == null ||
52
- allTypes . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
38
+ anyOfData . Properties . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
53
39
{ anyOfData . DiscriminatorType . Value . CSharpTypeWithoutNullability } { anyOfData . DiscriminatorPropertyName } ? { anyOfData . DiscriminatorPropertyName . ToParameterName ( ) } ,
54
40
" ) }
55
- { allTypes . Select ( x => $@ "
41
+ { anyOfData . Properties . Select ( x => $@ "
56
42
{ x . Type . CSharpTypeWithNullability } { x . ParameterName } ,
57
43
" ) . Inject ( ) . TrimEnd ( ',' , '\n ' ) }
58
44
)
59
45
{{
60
- { ( string . IsNullOrWhiteSpace ( anyOfData . Name ) ||
46
+ { ( ! anyOfData . IsNamed ||
61
47
anyOfData . DiscriminatorType == null ||
62
48
anyOfData . DiscriminatorPropertyName == null ||
63
- allTypes . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
49
+ anyOfData . Properties . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
64
50
{ anyOfData . DiscriminatorPropertyName } = { anyOfData . DiscriminatorPropertyName . ToParameterName ( ) } ;
65
51
" ) }
66
- { allTypes . Select ( x => $@ "
52
+ { anyOfData . Properties . Select ( x => $@ "
67
53
{ x . Name } = { x . ParameterName } ;
68
54
" ) . Inject ( ) }
69
55
}}" : " " ;
70
56
var json = GenerateFromToJsonMethods ( anyOfData . Namespace , className , anyOfData . Settings , isValueType : true , cancellationToken ) ;
71
57
72
58
return $@ "using System.Linq;
73
- { ( anyOfData . Properties . IsEmpty ? "" : @"#pragma warning disable CS0618 // Type or member is obsolete
74
- " ) }
59
+ { ( anyOfData . IsNamed ? @"#pragma warning disable CS0618 // Type or member is obsolete
60
+ " : "" ) }
75
61
#nullable enable
76
62
77
63
namespace { anyOfData . Namespace }
78
64
{{
79
65
{ anyOfData . Summary . ToXmlDocumentationSummary ( level : 4 ) }
80
66
public readonly partial struct { className } : global::System.IEquatable<{ className } >
81
67
{{
82
- { ( string . IsNullOrWhiteSpace ( anyOfData . Name ) ||
68
+ { ( ! anyOfData . IsNamed ||
83
69
anyOfData . DiscriminatorType == null ||
84
70
anyOfData . DiscriminatorPropertyName == null ||
85
- allTypes . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
71
+ anyOfData . Properties . Any ( x => string . IsNullOrWhiteSpace ( x . DiscriminatorValue ) ) ? " " : $@ "
86
72
{ string . Empty . ToXmlDocumentationSummary ( level : 8 ) }
87
73
public { anyOfData . DiscriminatorType . Value . CSharpTypeWithoutNullability } { anyOfData . DiscriminatorPropertyName } ? { anyOfData . DiscriminatorPropertyName } {{ get; }}
88
74
" ) }
89
- { allTypes . Select ( x => $@ "
75
+ { anyOfData . Properties . Select ( x => $@ "
90
76
{ x . Summary . ToXmlDocumentationSummary ( level : 8 ) }
91
77
#if NET6_0_OR_GREATER
92
78
public { x . Type . CSharpTypeWithNullability } { x . Name } {{ get; init; }}
@@ -117,7 +103,7 @@ namespace {anyOfData.Namespace}
117
103
118
104
{ string . Empty . ToXmlDocumentationSummary ( level : 8 ) }
119
105
public object? Object =>
120
- { allTypes . Reverse ( ) . Select ( x => $@ "
106
+ { anyOfData . Properties . Reverse ( ) . Select ( x => $@ "
121
107
{ x . Name } as object ??
122
108
" ) . Inject ( ) . TrimEnd ( '?' , '\n ' ) }
123
109
;
@@ -130,7 +116,7 @@ public bool Validate()
130
116
131
117
{ string . Empty . ToXmlDocumentationSummary ( level : 8 ) }
132
118
public TResult? Match<TResult>(
133
- { allTypes . Select ( x => $@ "
119
+ { anyOfData . Properties . Select ( x => $@ "
134
120
global::System.Func<{ x . Type . CSharpType } , TResult>? { x . ParameterName } = null,
135
121
" ) . Inject ( ) }
136
122
bool validate = true)
@@ -140,7 +126,7 @@ public bool Validate()
140
126
Validate();
141
127
}}
142
128
143
- { allTypes . Select ( ( x , i ) => $@ "
129
+ { anyOfData . Properties . Select ( ( x , i ) => $@ "
144
130
{ ( i > 0 ? "else " : "" ) } if (Is{ x . Name } && { x . ParameterName } != null)
145
131
{{
146
132
return { x . ParameterName } ({ x . Name } !);
@@ -151,7 +137,7 @@ public bool Validate()
151
137
152
138
{ string . Empty . ToXmlDocumentationSummary ( level : 8 ) }
153
139
public void Match(
154
- { allTypes . Select ( x => $@ "
140
+ { anyOfData . Properties . Select ( x => $@ "
155
141
global::System.Action<{ x . Type . CSharpType } >? { x . ParameterName } = null,
156
142
" ) . Inject ( ) }
157
143
bool validate = true)
@@ -161,7 +147,7 @@ public void Match(
161
147
Validate();
162
148
}}
163
149
164
- { allTypes . Select ( ( x , i ) => $@ "
150
+ { anyOfData . Properties . Select ( ( x , i ) => $@ "
165
151
{ ( i > 0 ? "else " : "" ) } if (Is{ x . Name } )
166
152
{{
167
153
{ x . ParameterName } ?.Invoke({ x . Name } !);
@@ -173,7 +159,7 @@ public override int GetHashCode()
173
159
{{
174
160
var fields = new object?[]
175
161
{{
176
- { allTypes . Select ( x => $@ "
162
+ { anyOfData . Properties . Select ( x => $@ "
177
163
{ x . Name } ,
178
164
typeof({ x . Type . CSharpTypeWithoutNullability } ),
179
165
" ) . Inject ( ) }
@@ -193,7 +179,7 @@ static int HashCodeAggregator(int hashCode, object? value) => value == null
193
179
public bool Equals({ className } other)
194
180
{{
195
181
return
196
- { allTypes . Select ( x => $@ "
182
+ { anyOfData . Properties . Select ( x => $@ "
197
183
global::System.Collections.Generic.EqualityComparer<{ x . Type . CSharpTypeWithNullability } >.Default.Equals({ x . Name } , other.{ x . Name } ) &&
198
184
" ) . Inject ( ) . TrimEnd ( '&' , '\n ' ) }
199
185
;
0 commit comments