-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathOpenApiPrimitive.cs
142 lines (122 loc) · 4.86 KB
/
OpenApiPrimitive.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Text;
using Microsoft.OpenApi.Exceptions;
using Microsoft.OpenApi.Properties;
using Microsoft.OpenApi.Writers;
namespace Microsoft.OpenApi.Any
{
/// <summary>
/// Open API primitive class.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class OpenApiPrimitive<T> : IOpenApiPrimitive
{
/// <summary>
/// Initializes the <see cref="IOpenApiPrimitive"/> class with the given value.
/// </summary>
/// <param name="value"></param>
public OpenApiPrimitive(T value)
{
Value = value;
}
/// <summary>
/// Initializes a copy of an <see cref="IOpenApiPrimitive"/> object
/// </summary>
/// <param name="openApiPrimitive"></param>
public OpenApiPrimitive(OpenApiPrimitive<T> openApiPrimitive)
{
Value = openApiPrimitive.Value;
}
/// <summary>
/// The kind of <see cref="IOpenApiAny"/>.
/// </summary>
public AnyType AnyType => AnyType.Primitive;
/// <summary>
/// The primitive class this object represents.
/// </summary>
public abstract PrimitiveType PrimitiveType { get; }
/// <summary>
/// Value of this <see cref="IOpenApiPrimitive"/>
/// </summary>
public T Value { get; }
/// <summary>
/// Write out content of primitive element
/// </summary>
/// <param name="writer"></param>
/// <param name="specVersion"></param>
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
switch (this.PrimitiveType)
{
case PrimitiveType.Integer:
var intValue = (OpenApiInteger)(IOpenApiPrimitive)this;
writer.WriteValue(intValue.Value);
break;
case PrimitiveType.Long:
var longValue = (OpenApiLong)(IOpenApiPrimitive)this;
writer.WriteValue(longValue.Value);
break;
case PrimitiveType.Float:
var floatValue = (OpenApiFloat)(IOpenApiPrimitive)this;
writer.WriteValue(floatValue.Value);
break;
case PrimitiveType.Double:
var doubleValue = (OpenApiDouble)(IOpenApiPrimitive)this;
writer.WriteValue(doubleValue.Value);
break;
case PrimitiveType.String:
var stringValue = (OpenApiString)(IOpenApiPrimitive)this;
if (stringValue.IsRawString())
writer.WriteRaw(stringValue.Value);
else
writer.WriteValue(stringValue.Value);
break;
case PrimitiveType.Byte:
var byteValue = (OpenApiByte)(IOpenApiPrimitive)this;
if (byteValue.Value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(Convert.ToBase64String(byteValue.Value));
}
break;
case PrimitiveType.Binary:
var binaryValue = (OpenApiBinary)(IOpenApiPrimitive)this;
if (binaryValue.Value == null)
{
writer.WriteNull();
}
else
{
writer.WriteValue(Encoding.UTF8.GetString(binaryValue.Value));
}
break;
case PrimitiveType.Boolean:
var boolValue = (OpenApiBoolean)(IOpenApiPrimitive)this;
writer.WriteValue(boolValue.Value);
break;
case PrimitiveType.Date:
var dateValue = (OpenApiDate)(IOpenApiPrimitive)this;
writer.WriteValue(dateValue.Value.ToString("o").Substring(0, 10));
break;
case PrimitiveType.DateTime:
var dateTimeValue = (OpenApiDateTime)(IOpenApiPrimitive)this;
writer.WriteValue(dateTimeValue.Value);
break;
case PrimitiveType.Password:
var passwordValue = (OpenApiPassword)(IOpenApiPrimitive)this;
writer.WriteValue(passwordValue.Value);
break;
default:
throw new OpenApiWriterException(
string.Format(
SRResource.PrimitiveTypeNotSupported,
this.PrimitiveType));
}
}
}
}