-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathRuntimeExpression.cs
More file actions
112 lines (97 loc) · 3.53 KB
/
RuntimeExpression.cs
File metadata and controls
112 lines (97 loc) · 3.53 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
namespace Microsoft.OpenApi.Expressions
{
/// <summary>
/// Base class for the Open API runtime expression.
/// </summary>
public abstract class RuntimeExpression : IEquatable<RuntimeExpression>
{
/// <summary>
/// The dollar sign prefix for a runtime expression.
/// </summary>
public const string Prefix = "$";
/// <summary>
/// The expression string.
/// </summary>
public abstract string Expression { get; }
/// <summary>
/// Build the runtime expression from input string.
/// </summary>
/// <param name="expression">The runtime expression.</param>
/// <returns>The built runtime expression object.</returns>
public static RuntimeExpression Build(string expression)
{
Utils.CheckArgumentNullOrEmpty(expression);
if (!expression.StartsWith(Prefix, StringComparison.OrdinalIgnoreCase))
{
return new CompositeExpression(expression);
}
// $url
if (expression.Equals(UrlExpression.Url, StringComparison.Ordinal))
{
return new UrlExpression();
}
// $method
if (expression.Equals(MethodExpression.Method, StringComparison.Ordinal))
{
return new MethodExpression();
}
// $statusCode
if (expression.Equals(StatusCodeExpression.StatusCode, StringComparison.Ordinal))
{
return new StatusCodeExpression();
}
// $request.
if (expression.StartsWith(RequestExpression.Request, StringComparison.Ordinal))
{
var subString = expression.Substring(RequestExpression.Request.Length);
var source = SourceExpression.Build(subString);
return new RequestExpression(source);
}
// $response.
if (expression.StartsWith(ResponseExpression.Response, StringComparison.Ordinal))
{
var subString = expression.Substring(ResponseExpression.Response.Length);
var source = SourceExpression.Build(subString);
return new ResponseExpression(source);
}
throw new OpenApiException(string.Format(SRResource.RuntimeExpressionHasInvalidFormat, expression));
}
/// <summary>
/// GetHashCode implementation for IEquatable.
/// </summary>
public override int GetHashCode()
{
return StringComparer.Ordinal.GetHashCode(Expression);
}
/// <summary>
/// Equals implementation for IEquatable.
/// </summary>
public override bool Equals(object? obj)
{
if (obj == null)
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
return obj.GetType() == GetType() && Equals((RuntimeExpression)obj);
}
/// <inheritdoc />
public bool Equals(RuntimeExpression? other)
{
return other is not null && StringComparer.Ordinal.Equals(Expression, other.Expression);
}
/// <inheritdoc />
public override string ToString()
{
return Expression;
}
}
}