openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
codegen/generator/test/common/InputFactory.cs
762lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.Input; |
| 2 | using Microsoft.TypeSpec.Generator.Input.Extensions; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Linq; |
| 5 | |
| 6 | namespace OpenAILibraryPlugin.Tests.Common |
| 7 | { |
| 8 | /// <summary> |
| 9 | /// Provider methods to construct intput test data |
| 10 | /// </summary> |
| 11 | public static class InputFactory |
| 12 | { |
| 13 | /// <summary> |
| 14 | /// Primitive input data types |
| 15 | /// </summary> |
| 16 | public static class Primitive |
| 17 | { |
| 18 | /// <summary> |
| 19 | /// Construct string input data |
| 20 | /// </summary> |
| 21 | /// <param name="name"></param> |
| 22 | /// <param name="crossLanguageDefinitionId"></param> |
| 23 | /// <param name="encode"></param> |
| 24 | /// <returns></returns> |
| 25 | public static InputPrimitiveType String(string? name = null, string? crossLanguageDefinitionId = null, string? encode = null) |
| 26 | { |
| 27 | return new InputPrimitiveType(InputPrimitiveTypeKind.String, name ?? string.Empty, crossLanguageDefinitionId ?? string.Empty, encode); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /// <summary> |
| 32 | /// Construct input enum types |
| 33 | /// </summary> |
| 34 | public static class EnumMember |
| 35 | { |
| 36 | /// <summary> |
| 37 | /// Construct input enum type value for int32 |
| 38 | /// </summary> |
| 39 | /// <param name="name"></param> |
| 40 | /// <param name="value"></param> |
| 41 | /// <param name="enumType"></param> |
| 42 | /// <returns></returns> |
| 43 | public static InputEnumTypeValue Int32(string name, int value, InputEnumType enumType) |
| 44 | { |
| 45 | return new InputEnumTypeValue(name, value, InputPrimitiveType.Int32, "", $"{name} description", enumType); |
| 46 | } |
| 47 | |
| 48 | /// <summary> |
| 49 | /// Construct input enum type value for float32 |
| 50 | /// </summary> |
| 51 | /// <param name="name"></param> |
| 52 | /// <param name="value"></param> |
| 53 | /// <param name="enumType"></param> |
| 54 | /// <returns></returns> |
| 55 | public static InputEnumTypeValue Float32(string name, float value, InputEnumType enumType) |
| 56 | { |
| 57 | return new InputEnumTypeValue(name, value, InputPrimitiveType.Float32, "", $"{name} description", enumType); |
| 58 | } |
| 59 | |
| 60 | /// <summary> |
| 61 | /// Construct input enum type value for string |
| 62 | /// </summary> |
| 63 | /// <param name="name"></param> |
| 64 | /// <param name="value"></param> |
| 65 | /// <param name="enumType"></param> |
| 66 | /// <returns></returns> |
| 67 | public static InputEnumTypeValue String(string name, string value, InputEnumType enumType) |
| 68 | { |
| 69 | return new InputEnumTypeValue(name, value, InputPrimitiveType.String, "", $"{name} description", enumType); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /// <summary> |
| 74 | /// Construct input literal types |
| 75 | /// </summary> |
| 76 | public static class Literal |
| 77 | { |
| 78 | /// <summary> |
| 79 | /// Construct input literal type value for string |
| 80 | /// </summary> |
| 81 | /// <param name="value"></param> |
| 82 | /// <param name="name"></param> |
| 83 | /// <param name="namespace"></param> |
| 84 | /// <returns></returns> |
| 85 | public static InputLiteralType String(string value, string? name = null, string? @namespace = null) |
| 86 | { |
| 87 | return new InputLiteralType(name ?? string.Empty, @namespace ?? string.Empty, InputPrimitiveType.String, value); |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Construct input enum type value for any |
| 92 | /// </summary> |
| 93 | /// <param name="value"></param> |
| 94 | /// <param name="name"></param> |
| 95 | /// <param name="namespace"></param> |
| 96 | /// <returns></returns> |
| 97 | public static InputLiteralType Int32(int value, string? name = null, string? @namespace = null) |
| 98 | { |
| 99 | return new InputLiteralType(name ?? string.Empty, @namespace ?? string.Empty, InputPrimitiveType.Int32, value); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /// <summary> |
| 104 | /// Construct input constants |
| 105 | /// </summary> |
| 106 | public static class Constant |
| 107 | { |
| 108 | /// <summary> |
| 109 | /// Construct input constnat for string |
| 110 | /// </summary> |
| 111 | /// <param name="value"></param> |
| 112 | /// <returns></returns> |
| 113 | public static InputConstant String(string value) |
| 114 | { |
| 115 | return new InputConstant(value, InputPrimitiveType.String); |
| 116 | } |
| 117 | |
| 118 | /// <summary> |
| 119 | /// Construct input constnat for int64 |
| 120 | /// </summary> |
| 121 | /// <param name="value"></param> |
| 122 | /// <returns></returns> |
| 123 | public static InputConstant Int64(long value) |
| 124 | { |
| 125 | return new InputConstant(value, InputPrimitiveType.Int64); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// <summary> |
| 130 | /// Construct input parameter with content type |
| 131 | /// </summary> |
| 132 | /// <param name="contentType"></param> |
| 133 | /// <returns></returns> |
| 134 | public static InputHeaderParameter ContentTypeParameter(string contentType) |
| 135 | => HeaderParameter( |
| 136 | "contentType", |
| 137 | Literal.String(contentType), |
| 138 | isRequired: true, |
| 139 | defaultValue: Constant.String(contentType), |
| 140 | serializedName: "Content-Type", |
| 141 | isContentType: true, |
| 142 | scope: InputParameterScope.Constant); |
| 143 | |
| 144 | /// <summary> |
| 145 | /// Construct input model property |
| 146 | /// </summary> |
| 147 | /// <param name="name"></param> |
| 148 | /// <param name="type"></param> |
| 149 | /// <param name="isRequired"></param> |
| 150 | /// <param name="isReadOnly"></param> |
| 151 | /// <param name="isDiscriminator"></param> |
| 152 | /// <param name="isHttpMetadata"></param> |
| 153 | /// <param name="isApiVersion"></param> |
| 154 | /// <param name="defaultValue"></param> |
| 155 | /// <param name="wireName"></param> |
| 156 | /// <param name="summary"></param> |
| 157 | /// <param name="serializedName"></param> |
| 158 | /// <param name="doc"></param> |
| 159 | /// <returns></returns> |
| 160 | public static InputModelProperty Property( |
| 161 | string name, |
| 162 | InputType type, |
| 163 | bool isRequired = false, |
| 164 | bool isReadOnly = false, |
| 165 | bool isDiscriminator = false, |
| 166 | bool isHttpMetadata = false, |
| 167 | bool isApiVersion = false, |
| 168 | InputConstant? defaultValue = null, |
| 169 | string? wireName = null, |
| 170 | string? summary = null, |
| 171 | string? serializedName = null, |
| 172 | string? doc = null) |
| 173 | { |
| 174 | return new InputModelProperty( |
| 175 | name: name, |
| 176 | summary: summary, |
| 177 | doc: doc ?? $"Description for {name}", |
| 178 | type: type, |
| 179 | isRequired: isRequired, |
| 180 | isReadOnly: isReadOnly, |
| 181 | isApiVersion: isApiVersion, |
| 182 | defaultValue: defaultValue, |
| 183 | isHttpMetadata: isHttpMetadata, |
| 184 | access: null, |
| 185 | isDiscriminator: isDiscriminator, |
| 186 | serializedName: serializedName ?? wireName ?? name.ToVariableName(), |
| 187 | serializationOptions: new(json: new(wireName ?? name.ToVariableName()))); |
| 188 | } |
| 189 | |
| 190 | public static InputHeaderParameter HeaderParameter( |
| 191 | string name, |
| 192 | InputType type, |
| 193 | bool isRequired = false, |
| 194 | bool isReadOnly = false, |
| 195 | bool isApiVersion = false, |
| 196 | bool isContentType = false, |
| 197 | string? summary = null, |
| 198 | string? doc = null, |
| 199 | string? collectionFormat = null, |
| 200 | string? serializedName = null, |
| 201 | InputConstant? defaultValue = null, |
| 202 | InputParameterScope scope = InputParameterScope.Method) |
| 203 | { |
| 204 | return new InputHeaderParameter( |
| 205 | name: name, |
| 206 | summary: summary, |
| 207 | doc: doc ?? $"{name} description", |
| 208 | type: type, |
| 209 | isRequired: isRequired, |
| 210 | isReadOnly: isReadOnly, |
| 211 | isApiVersion: isApiVersion, |
| 212 | isContentType: isContentType, |
| 213 | access: null, |
| 214 | defaultValue: defaultValue, |
| 215 | collectionFormat: collectionFormat, |
| 216 | scope: scope, |
| 217 | arraySerializationDelimiter: null, |
| 218 | serializedName: serializedName ?? name); |
| 219 | } |
| 220 | |
| 221 | public static InputQueryParameter QueryParameter( |
| 222 | string name, |
| 223 | InputType type, |
| 224 | bool isRequired = false, |
| 225 | bool isReadOnly = false, |
| 226 | bool isApiVersion = false, |
| 227 | InputConstant? defaultValue = null, |
| 228 | string? summary = null, |
| 229 | string? doc = null, |
| 230 | string? collectionFormat = null, |
| 231 | string? serializedName = null, |
| 232 | bool explode = false, |
| 233 | InputParameterScope scope = InputParameterScope.Method, |
| 234 | string? delimiter = null) |
| 235 | { |
| 236 | return new InputQueryParameter( |
| 237 | name: name, |
| 238 | summary: summary, |
| 239 | doc: doc ?? $"{name} description", |
| 240 | type: type, |
| 241 | isRequired: isRequired, |
| 242 | isReadOnly: isReadOnly, |
| 243 | isApiVersion: isApiVersion, |
| 244 | defaultValue: defaultValue, |
| 245 | scope: scope, |
| 246 | arraySerializationDelimiter: delimiter, |
| 247 | access: null, |
| 248 | serializedName: serializedName ?? name, |
| 249 | collectionFormat: collectionFormat, |
| 250 | explode: explode); |
| 251 | } |
| 252 | |
| 253 | public static InputPathParameter PathParameter( |
| 254 | string name, |
| 255 | InputType type, |
| 256 | bool isRequired = false, |
| 257 | bool isReadOnly = false, |
| 258 | bool isApiVersion = false, |
| 259 | InputConstant? defaultValue = null, |
| 260 | string? summary = null, |
| 261 | string? doc = null, |
| 262 | string? serializedName = null, |
| 263 | bool allowReserved = false, |
| 264 | bool explode = false, |
| 265 | bool skipUrlEncoding = false, |
| 266 | string? serverUrlTemplate = null, |
| 267 | InputParameterScope scope = InputParameterScope.Method) |
| 268 | { |
| 269 | return new InputPathParameter( |
| 270 | name: name, |
| 271 | summary: summary, |
| 272 | doc: doc ?? $"{name} description", |
| 273 | type: type, |
| 274 | isRequired: isRequired, |
| 275 | isReadOnly: isReadOnly, |
| 276 | isApiVersion: isApiVersion, |
| 277 | explode: explode, |
| 278 | defaultValue: defaultValue, |
| 279 | scope: scope, |
| 280 | skipUrlEncoding: skipUrlEncoding, |
| 281 | serverUrlTemplate: serverUrlTemplate, |
| 282 | access: null, |
| 283 | serializedName: serializedName ?? name, |
| 284 | allowReserved: allowReserved); |
| 285 | } |
| 286 | |
| 287 | public static InputEndpointParameter EndpointParameter( |
| 288 | string name, |
| 289 | InputType type, |
| 290 | bool isRequired = false, |
| 291 | bool isReadOnly = false, |
| 292 | bool isApiVersion = false, |
| 293 | InputConstant? defaultValue = null, |
| 294 | string? summary = null, |
| 295 | string? doc = null, |
| 296 | string? serializedName = null, |
| 297 | bool skipUrlEncoding = false, |
| 298 | bool isEndpoint = true, |
| 299 | string? serverUrlTemplate = null, |
| 300 | InputParameterScope scope = InputParameterScope.Client) |
| 301 | { |
| 302 | return new InputEndpointParameter( |
| 303 | name: name, |
| 304 | summary: summary, |
| 305 | doc: doc ?? $"{name} description", |
| 306 | type: type, |
| 307 | isRequired: isRequired, |
| 308 | isReadOnly: isReadOnly, |
| 309 | isApiVersion: isApiVersion, |
| 310 | defaultValue: defaultValue, |
| 311 | scope: scope, |
| 312 | skipUrlEncoding: skipUrlEncoding, |
| 313 | serverUrlTemplate: serverUrlTemplate, |
| 314 | isEndpoint: isEndpoint, |
| 315 | access: null, |
| 316 | serializedName: serializedName ?? name); |
| 317 | } |
| 318 | |
| 319 | public static InputBodyParameter BodyParameter( |
| 320 | string name, |
| 321 | InputType type, |
| 322 | bool isRequired = false, |
| 323 | bool isReadOnly = false, |
| 324 | bool isApiVersion = false, |
| 325 | InputConstant? defaultValue = null, |
| 326 | string? summary = null, |
| 327 | string? doc = null, |
| 328 | string? serializedName = null, |
| 329 | string[]? contentTypes = null, |
| 330 | string? defaultContentType = null, |
| 331 | InputParameterScope scope = InputParameterScope.Method) |
| 332 | { |
| 333 | return new InputBodyParameter( |
| 334 | name: name, |
| 335 | summary: summary, |
| 336 | doc: doc ?? $"{name} description", |
| 337 | type: type, |
| 338 | isRequired: isRequired, |
| 339 | isReadOnly: isReadOnly, |
| 340 | isApiVersion: isApiVersion, |
| 341 | defaultValue: defaultValue, |
| 342 | defaultContentType: defaultContentType ?? "application/json", |
| 343 | contentTypes: contentTypes ?? ["application/json"], |
| 344 | scope: scope, |
| 345 | access: null, |
| 346 | serializedName: serializedName ?? name); |
| 347 | } |
| 348 | |
| 349 | public static InputMethodParameter MethodParameter( |
| 350 | string name, |
| 351 | InputType type, |
| 352 | bool isRequired = false, |
| 353 | bool isReadOnly = false, |
| 354 | bool isApiVersion = false, |
| 355 | InputConstant? defaultValue = null, |
| 356 | string? summary = null, |
| 357 | string? doc = null, |
| 358 | string? serializedName = null, |
| 359 | InputRequestLocation location = InputRequestLocation.Body, |
| 360 | InputParameterScope scope = InputParameterScope.Method) |
| 361 | { |
| 362 | return new InputMethodParameter( |
| 363 | name: name, |
| 364 | summary: summary, |
| 365 | doc: doc ?? $"{name} description", |
| 366 | type: type, |
| 367 | isRequired: isRequired, |
| 368 | isReadOnly: isReadOnly, |
| 369 | isApiVersion: isApiVersion, |
| 370 | defaultValue: defaultValue, |
| 371 | scope: scope, |
| 372 | access: null, |
| 373 | location: location, |
| 374 | serializedName: serializedName ?? name); |
| 375 | } |
| 376 | |
| 377 | /// <summary> |
| 378 | /// Construct input model type |
| 379 | /// </summary> |
| 380 | /// <param name="name"></param> |
| 381 | /// <param name="clientNamespace"></param> |
| 382 | /// <param name="access"></param> |
| 383 | /// <param name="usage"></param> |
| 384 | /// <param name="properties"></param> |
| 385 | /// <param name="baseModel"></param> |
| 386 | /// <param name="modelAsStruct"></param> |
| 387 | /// <param name="discriminatedKind"></param> |
| 388 | /// <param name="additionalProperties"></param> |
| 389 | /// <param name="discriminatedModels"></param> |
| 390 | /// <param name="derivedModels"></param> |
| 391 | /// <param name="decorators"></param> |
| 392 | /// <param name="isDynamicModel"></param> |
| 393 | /// <returns></returns> |
| 394 | public static InputModelType Model( |
| 395 | string name, |
| 396 | string clientNamespace = "Samples.Models", |
| 397 | string access = "public", |
| 398 | InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input | InputModelTypeUsage.Json, |
| 399 | IEnumerable<InputModelProperty>? properties = null, |
| 400 | InputModelType? baseModel = null, |
| 401 | bool modelAsStruct = false, |
| 402 | string? discriminatedKind = null, |
| 403 | InputType? additionalProperties = null, |
| 404 | IDictionary<string, InputModelType>? discriminatedModels = null, |
| 405 | IEnumerable<InputModelType>? derivedModels = null, |
| 406 | IReadOnlyList<InputDecoratorInfo>? decorators = null, |
| 407 | bool isDynamicModel = false) |
| 408 | { |
| 409 | IEnumerable<InputModelProperty> propertiesList = properties ?? [Property("StringProperty", InputPrimitiveType.String)]; |
| 410 | var model = new InputModelType( |
| 411 | name, |
| 412 | clientNamespace, |
| 413 | name, |
| 414 | access, |
| 415 | null, |
| 416 | null, |
| 417 | $"{name} description", |
| 418 | usage, |
| 419 | [.. propertiesList], |
| 420 | baseModel, |
| 421 | derivedModels is null ? [] : [.. derivedModels], |
| 422 | discriminatedKind, |
| 423 | propertiesList.FirstOrDefault(p => p.IsDiscriminator), |
| 424 | discriminatedModels is null ? new Dictionary<string, InputModelType>() : discriminatedModels.AsReadOnly(), |
| 425 | additionalProperties, |
| 426 | modelAsStruct, |
| 427 | new(), |
| 428 | isDynamicModel); |
| 429 | if (decorators is not null) |
| 430 | { |
| 431 | var decoratorProperty = typeof(InputModelType).GetProperty(nameof(InputModelType.Decorators)); |
| 432 | var setDecoratorMethod = decoratorProperty?.GetSetMethod(true); |
| 433 | setDecoratorMethod!.Invoke(model, [decorators]); |
| 434 | } |
| 435 | return model; |
| 436 | } |
| 437 | |
| 438 | /// <summary> |
| 439 | /// Construct basic service method |
| 440 | /// </summary> |
| 441 | /// <param name="name"></param> |
| 442 | /// <param name="operation"></param> |
| 443 | /// <param name="access"></param> |
| 444 | /// <param name="parameters"></param> |
| 445 | /// <param name="response"></param> |
| 446 | /// <param name="exception"></param> |
| 447 | /// <param name="crossLanguageDefinitionId"></param> |
| 448 | /// <returns></returns> |
| 449 | public static InputBasicServiceMethod BasicServiceMethod( |
| 450 | string name, |
| 451 | InputOperation operation, |
| 452 | string access = "public", |
| 453 | IReadOnlyList<InputMethodParameter>? parameters = null, |
| 454 | InputServiceMethodResponse? response = null, |
| 455 | InputServiceMethodResponse? exception = null, |
| 456 | string? crossLanguageDefinitionId = null) |
| 457 | { |
| 458 | return new InputBasicServiceMethod( |
| 459 | name, |
| 460 | access, |
| 461 | [], |
| 462 | null, |
| 463 | null, |
| 464 | operation, |
| 465 | parameters ?? [], |
| 466 | response ?? ServiceMethodResponse(null, null), |
| 467 | exception, |
| 468 | false, |
| 469 | true, |
| 470 | true, |
| 471 | crossLanguageDefinitionId ?? string.Empty); |
| 472 | } |
| 473 | |
| 474 | /// <summary> |
| 475 | /// Construct service method response |
| 476 | /// </summary> |
| 477 | /// <param name="type"></param> |
| 478 | /// <param name="resultSegments"></param> |
| 479 | /// <returns></returns> |
| 480 | public static InputServiceMethodResponse ServiceMethodResponse(InputType? type, IReadOnlyList<string>? resultSegments) |
| 481 | { |
| 482 | return new InputServiceMethodResponse(type, resultSegments); |
| 483 | } |
| 484 | |
| 485 | /// <summary> |
| 486 | /// Construct paging service method |
| 487 | /// </summary> |
| 488 | /// <param name="name"></param> |
| 489 | /// <param name="operation"></param> |
| 490 | /// <param name="access"></param> |
| 491 | /// <param name="parameters"></param> |
| 492 | /// <param name="response"></param> |
| 493 | /// <param name="exception"></param> |
| 494 | /// <param name="pagingMetadata"></param> |
| 495 | /// <returns></returns> |
| 496 | public static InputPagingServiceMethod PagingServiceMethod( |
| 497 | string name, |
| 498 | InputOperation operation, |
| 499 | string access = "public", |
| 500 | IReadOnlyList<InputMethodParameter>? parameters = null, |
| 501 | InputServiceMethodResponse? response = null, |
| 502 | InputServiceMethodResponse? exception = null, |
| 503 | InputPagingServiceMetadata? pagingMetadata = null) |
| 504 | { |
| 505 | return new InputPagingServiceMethod( |
| 506 | name, |
| 507 | access, |
| 508 | [], |
| 509 | null, |
| 510 | null, |
| 511 | operation, |
| 512 | parameters ?? [], |
| 513 | response ?? ServiceMethodResponse(null, null), |
| 514 | exception, |
| 515 | false, |
| 516 | true, |
| 517 | true, |
| 518 | string.Empty, |
| 519 | pagingMetadata ?? PagingMetadata([], null, null)); |
| 520 | } |
| 521 | |
| 522 | /// <summary> |
| 523 | /// Construct paging metadata |
| 524 | /// </summary> |
| 525 | /// <param name="itemPropertySegments"></param> |
| 526 | /// <param name="nextLink"></param> |
| 527 | /// <param name="continuationToken"></param> |
| 528 | /// <returns></returns> |
| 529 | public static InputPagingServiceMetadata PagingMetadata(IReadOnlyList<string> itemPropertySegments, InputNextLink? nextLink, InputContinuationToken? continuationToken) |
| 530 | { |
| 531 | return new InputPagingServiceMetadata(itemPropertySegments, nextLink, continuationToken); |
| 532 | } |
| 533 | |
| 534 | /// <summary> |
| 535 | /// Construct paging service method |
| 536 | /// </summary> |
| 537 | /// <param name="name"></param> |
| 538 | /// <param name="operation"></param> |
| 539 | /// <param name="access"></param> |
| 540 | /// <param name="parameters"></param> |
| 541 | /// <param name="response"></param> |
| 542 | /// <param name="exception"></param> |
| 543 | /// <param name="longRunningServiceMetadata"></param> |
| 544 | /// <returns></returns> |
| 545 | public static InputLongRunningServiceMethod LongRunningServiceMethod( |
| 546 | string name, |
| 547 | InputOperation operation, |
| 548 | string access = "public", |
| 549 | IReadOnlyList<InputMethodParameter>? parameters = null, |
| 550 | InputServiceMethodResponse? response = null, |
| 551 | InputServiceMethodResponse? exception = null, |
| 552 | InputLongRunningServiceMetadata? longRunningServiceMetadata = null) |
| 553 | { |
| 554 | return new InputLongRunningServiceMethod( |
| 555 | name, |
| 556 | access, |
| 557 | [], |
| 558 | null, |
| 559 | null, |
| 560 | operation, |
| 561 | parameters ?? [], |
| 562 | response ?? ServiceMethodResponse(null, null), |
| 563 | exception, |
| 564 | false, |
| 565 | true, |
| 566 | true, |
| 567 | string.Empty, |
| 568 | longRunningServiceMetadata ?? LongRunningServiceMetadata(1, OperationResponse(), null)); |
| 569 | } |
| 570 | |
| 571 | /// <summary> |
| 572 | /// Construct paging metadata |
| 573 | /// </summary> |
| 574 | /// <param name="finalState"></param> |
| 575 | /// <param name="finalResponse"></param> |
| 576 | /// <param name="resultPath"></param> |
| 577 | /// <returns></returns> |
| 578 | public static InputLongRunningServiceMetadata LongRunningServiceMetadata(int finalState, InputOperationResponse finalResponse, string? resultPath) |
| 579 | { |
| 580 | return new InputLongRunningServiceMetadata(finalState, finalResponse, resultPath); |
| 581 | } |
| 582 | |
| 583 | /// <summary> |
| 584 | /// Construct input operation |
| 585 | /// </summary> |
| 586 | /// <param name="name"></param> |
| 587 | /// <param name="access"></param> |
| 588 | /// <param name="parameters"></param> |
| 589 | /// <param name="responses"></param> |
| 590 | /// <param name="requestMediaTypes"></param> |
| 591 | /// <param name="path"></param> |
| 592 | /// <param name="decorators"></param> |
| 593 | /// <returns></returns> |
| 594 | public static InputOperation Operation( |
| 595 | string name, |
| 596 | string access = "public", |
| 597 | IEnumerable<InputParameter>? parameters = null, |
| 598 | IEnumerable<InputOperationResponse>? responses = null, |
| 599 | IEnumerable<string>? requestMediaTypes = null, |
| 600 | string? path = null, |
| 601 | IReadOnlyList<InputDecoratorInfo>? decorators = null, |
| 602 | string? ns = null) |
| 603 | { |
| 604 | var operation = new InputOperation( |
| 605 | name, |
| 606 | null, |
| 607 | "", |
| 608 | $"{name} description", |
| 609 | null, |
| 610 | access, |
| 611 | parameters is null ? [] : [.. parameters], |
| 612 | responses is null ? [OperationResponse()] : [.. responses], |
| 613 | "GET", |
| 614 | string.Empty, |
| 615 | path ?? string.Empty, |
| 616 | null, |
| 617 | requestMediaTypes is null ? null : [.. requestMediaTypes], |
| 618 | false, |
| 619 | true, |
| 620 | true, |
| 621 | name, |
| 622 | ns); |
| 623 | if (decorators is not null) |
| 624 | { |
| 625 | var decoratorProperty = typeof(InputOperation).GetProperty(nameof(InputOperation.Decorators)); |
| 626 | var setDecoratorMethod = decoratorProperty?.GetSetMethod(true); |
| 627 | setDecoratorMethod!.Invoke(operation, [decorators]); |
| 628 | } |
| 629 | return operation; |
| 630 | } |
| 631 | |
| 632 | /// <summary> |
| 633 | /// Construct input operation response |
| 634 | /// </summary> |
| 635 | /// <param name="statusCodes"></param> |
| 636 | /// <param name="bodytype"></param> |
| 637 | /// <returns></returns> |
| 638 | public static InputOperationResponse OperationResponse(IEnumerable<int>? statusCodes = null, InputType? bodytype = null) |
| 639 | { |
| 640 | return new InputOperationResponse( |
| 641 | statusCodes is null ? [200] : [.. statusCodes], |
| 642 | bodytype, |
| 643 | [], |
| 644 | false, |
| 645 | ["application/json"]); |
| 646 | } |
| 647 | |
| 648 | private static readonly Dictionary<InputClient, IList<InputClient>> _childClientsCache = new(); |
| 649 | /// <summary> |
| 650 | /// Construct input client |
| 651 | /// </summary> |
| 652 | /// <param name="name"></param> |
| 653 | /// <param name="clientNamespace"></param> |
| 654 | /// <param name="doc"></param> |
| 655 | /// <param name="methods"></param> |
| 656 | /// <param name="parameters"></param> |
| 657 | /// <param name="parent"></param> |
| 658 | /// <param name="decorators"></param> |
| 659 | /// <param name="crossLanguageDefinitionId"></param> |
| 660 | /// <returns></returns> |
| 661 | public static InputClient Client(string name, string clientNamespace = "Samples", string? doc = null, IEnumerable<InputServiceMethod>? methods = null, IEnumerable<InputParameter>? parameters = null, InputClient? parent = null, IReadOnlyList<InputDecoratorInfo>? decorators = null, string? crossLanguageDefinitionId = null, bool? isMultiServiceClient = false) |
| 662 | { |
| 663 | // when this client has parent, we add the constructed client into the `children` list of the parent |
| 664 | var clientChildren = new List<InputClient>(); |
| 665 | var client = new InputClient( |
| 666 | name, |
| 667 | clientNamespace, |
| 668 | crossLanguageDefinitionId ?? $"{clientNamespace}.{name}", |
| 669 | string.Empty, |
| 670 | doc ?? $"{name} description", |
| 671 | isMultiServiceClient ?? false, |
| 672 | methods is null ? [] : [.. methods], |
| 673 | parameters is null ? [] : [.. parameters], |
| 674 | parent, |
| 675 | clientChildren, |
| 676 | [] |
| 677 | ); |
| 678 | _childClientsCache[client] = clientChildren; |
| 679 | // when we have a parent, we need to find the children list of this parent client and update accordingly. |
| 680 | if (parent != null && _childClientsCache.TryGetValue(parent, out var children)) |
| 681 | { |
| 682 | children.Add(client); |
| 683 | } |
| 684 | |
| 685 | if (decorators is not null) |
| 686 | { |
| 687 | var decoratorProperty = typeof(InputClient).GetProperty(nameof(InputClient.Decorators)); |
| 688 | var setDecoratorMethod = decoratorProperty?.GetSetMethod(true); |
| 689 | setDecoratorMethod!.Invoke(client, [decorators]); |
| 690 | } |
| 691 | return client; |
| 692 | } |
| 693 | |
| 694 | public static InputPagingServiceMetadata ContinuationTokenPagingMetadata(InputParameter parameter, string itemPropertyName, string continuationTokenName, InputResponseLocation continuationTokenLocation) |
| 695 | { |
| 696 | return new InputPagingServiceMetadata( |
| 697 | [itemPropertyName], |
| 698 | null, |
| 699 | continuationToken: new InputContinuationToken(parameter, [continuationTokenName], continuationTokenLocation)); |
| 700 | } |
| 701 | |
| 702 | public static InputType Array(InputType elementType) |
| 703 | { |
| 704 | return new InputArrayType("list", "list", elementType); |
| 705 | } |
| 706 | |
| 707 | public static InputPagingServiceMetadata NextLinkPagingMetadata(string itemPropertyName, string nextLinkName, InputResponseLocation nextLinkLocation, IReadOnlyList<InputParameter>? reinjectedParameters = null) |
| 708 | { |
| 709 | return PagingMetadata( |
| 710 | [itemPropertyName], |
| 711 | new InputNextLink(null, [nextLinkName], nextLinkLocation, reinjectedParameters), |
| 712 | null); |
| 713 | } |
| 714 | |
| 715 | public static InputEnumType StringEnum( |
| 716 | string name, |
| 717 | IEnumerable<(string Name, string Value)> values, |
| 718 | string access = "public", |
| 719 | InputModelTypeUsage usage = InputModelTypeUsage.Input | InputModelTypeUsage.Output, |
| 720 | bool isExtensible = false, |
| 721 | string clientNamespace = "Sample.Models") |
| 722 | { |
| 723 | var enumValues = new List<InputEnumTypeValue>(); |
| 724 | var enumType = Enum( |
| 725 | name, |
| 726 | InputPrimitiveType.String, |
| 727 | enumValues, |
| 728 | access: access, |
| 729 | usage: usage, |
| 730 | isExtensible: isExtensible, |
| 731 | clientNamespace: clientNamespace); |
| 732 | |
| 733 | foreach (var (valueName, value) in values) |
| 734 | { |
| 735 | enumValues.Add(EnumMember.String(valueName, value, enumType)); |
| 736 | } |
| 737 | |
| 738 | return enumType; |
| 739 | } |
| 740 | |
| 741 | private static InputEnumType Enum( |
| 742 | string name, |
| 743 | InputPrimitiveType underlyingType, |
| 744 | IReadOnlyList<InputEnumTypeValue> values, |
| 745 | string access = "public", |
| 746 | InputModelTypeUsage usage = InputModelTypeUsage.Output | InputModelTypeUsage.Input, |
| 747 | bool isExtensible = false, |
| 748 | string clientNamespace = "Sample.Models") |
| 749 | => new InputEnumType( |
| 750 | name, |
| 751 | clientNamespace, |
| 752 | name, |
| 753 | access, |
| 754 | null, |
| 755 | "", |
| 756 | $"{name} description", |
| 757 | usage, |
| 758 | underlyingType, |
| 759 | values, |
| 760 | isExtensible); |
| 761 | } |
| 762 | } |
| 763 | |