-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathDataSourceConverterFactory.cs
More file actions
208 lines (180 loc) · 9.8 KB
/
DataSourceConverterFactory.cs
File metadata and controls
208 lines (180 loc) · 9.8 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.DataApiBuilder.Config.ObjectModel;
namespace Azure.DataApiBuilder.Config.Converters;
internal class DataSourceConverterFactory : JsonConverterFactory
{
// Settings for variable replacement during deserialization.
private readonly DeserializationVariableReplacementSettings? _replacementSettings;
/// <inheritdoc/>
public override bool CanConvert(Type typeToConvert)
{
return typeToConvert.IsAssignableTo(typeof(DataSource));
}
/// <inheritdoc/>
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
return new DataSourceConverter(_replacementSettings);
}
/// <param name="replacementSettings">Settings for variable replacement during deserialization.
/// If null, no variable replacement will be performed.</param>
internal DataSourceConverterFactory(DeserializationVariableReplacementSettings? replacementSettings = null)
{
_replacementSettings = replacementSettings;
}
private class DataSourceConverter : JsonConverter<DataSource>
{
// Settings for variable replacement during deserialization.
private readonly DeserializationVariableReplacementSettings? _replacementSettings;
/// <param name="replacementSettings">Settings for variable replacement during deserialization.
/// If null, no variable replacement will be performed.</param>
public DataSourceConverter(DeserializationVariableReplacementSettings? replacementSettings)
{
_replacementSettings = replacementSettings;
}
public override DataSource? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType is JsonTokenType.StartObject)
{
DatabaseType databaseType = DatabaseType.MSSQL;
string connectionString = string.Empty;
DatasourceHealthCheckConfig? health = null;
Dictionary<string, object?>? datasourceOptions = null;
bool includeVectorFieldsByDefault = false;
bool userProvidedIncludeVectorFieldsByDefault = false;
while (reader.Read())
{
if (reader.TokenType is JsonTokenType.EndObject)
{
return new DataSource(databaseType, connectionString, datasourceOptions, health, includeVectorFieldsByDefault)
{
UserProvidedIncludeVectorFieldsByDefault = userProvidedIncludeVectorFieldsByDefault
};
}
if (reader.TokenType is JsonTokenType.PropertyName)
{
string propertyName = reader.GetString() ?? string.Empty;
reader.Read();
switch (propertyName)
{
case "database-type":
databaseType = EnumExtensions.Deserialize<DatabaseType>(reader.DeserializeString(_replacementSettings)!);
break;
case "connection-string":
connectionString = reader.DeserializeString(_replacementSettings)!;
break;
case "health":
if (reader.TokenType == JsonTokenType.Null)
{
health = new();
}
else
{
try
{
health = JsonSerializer.Deserialize<DatasourceHealthCheckConfig>(ref reader, options);
}
catch (Exception e)
{
throw new JsonException($"Error while deserializing DataSource health: {e.Message}");
}
}
break;
case "options":
if (reader.TokenType is not JsonTokenType.Null)
{
Dictionary<string, object?> optionsDict = new();
while (reader.Read() && reader.TokenType is not JsonTokenType.EndObject)
{
string optionsSubproperty = reader.GetString()!;
reader.Read();
object? optionsSubpropertyValue;
if (reader.TokenType is JsonTokenType.String)
{
// Determine whether to resolve the environment variable or keep as-is.
string stringValue = reader.DeserializeString(_replacementSettings)!;
if (bool.TryParse(stringValue, out bool boolValue))
{
// MsSqlOptions.SetSessionContext will contain a boolean value.
optionsSubpropertyValue = boolValue;
}
else
{
// CosmosDbNoSQLDataSourceOptions will contain string values.
optionsSubpropertyValue = stringValue;
}
}
else if (reader.TokenType is JsonTokenType.True or JsonTokenType.False)
{
optionsSubpropertyValue = reader.GetBoolean();
}
else if (reader.TokenType is JsonTokenType.Null)
{
optionsSubpropertyValue = null;
}
else
{
throw new JsonException($"Unexpected value for options {optionsSubproperty} while deserializing DataSource options.");
}
optionsDict.Add(optionsSubproperty, optionsSubpropertyValue);
}
datasourceOptions = optionsDict;
}
break;
case "include-vector-fields-by-default":
if (reader.TokenType is JsonTokenType.True or JsonTokenType.False)
{
includeVectorFieldsByDefault = reader.GetBoolean();
userProvidedIncludeVectorFieldsByDefault = true;
}
else if (reader.TokenType is JsonTokenType.String)
{
// Support environment variable replacement
string stringValue = reader.DeserializeString(_replacementSettings)!;
if (bool.TryParse(stringValue, out bool boolValue))
{
includeVectorFieldsByDefault = boolValue;
userProvidedIncludeVectorFieldsByDefault = true;
}
}
break;
default:
throw new JsonException($"Unexpected property {propertyName} while deserializing DataSource.");
}
}
}
}
throw new JsonException("data-source property has a missing }.");
}
public override void Write(Utf8JsonWriter writer, DataSource value, JsonSerializerOptions options)
{
writer.WriteStartObject();
// Always write required properties
writer.WritePropertyName("database-type");
JsonSerializer.Serialize(writer, value.DatabaseType, options);
writer.WritePropertyName("connection-string");
writer.WriteStringValue(value.ConnectionString);
// Write options if present
if (value.Options is not null && value.Options.Count > 0)
{
writer.WritePropertyName("options");
JsonSerializer.Serialize(writer, value.Options, options);
}
// Write health if present
if (value.Health is not null)
{
writer.WritePropertyName("health");
JsonSerializer.Serialize(writer, value.Health, options);
}
// Write include-vector-fields-by-default only if user provided it
if (value.UserProvidedIncludeVectorFieldsByDefault)
{
writer.WritePropertyName("include-vector-fields-by-default");
writer.WriteBooleanValue(value.IncludeVectorFieldsByDefault);
}
writer.WriteEndObject();
}
}
}