-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOptionsExtensions.cs
More file actions
134 lines (116 loc) · 5.05 KB
/
OptionsExtensions.cs
File metadata and controls
134 lines (116 loc) · 5.05 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
using Couchbase.AnalyticsClient.Options;
using Couchbase.AnalyticsClient.Query;
using Couchbase.Core.Json;
using Couchbase.Grpc.Protocol.Columnar;
using Google.Protobuf.Collections;
using Google.Protobuf.WellKnownTypes;
using ArgumentOutOfRangeException = System.ArgumentOutOfRangeException;
namespace Couchbase.Analytics.Performer.Internal.Utils;
internal static class OptionsExtensions
{
public static QueryOptions ToQueryOptions(this ExecuteQueryRequest.Types.Options? protoOptions)
{
var queryOptions = new QueryOptions();
if (protoOptions is null) return queryOptions;
if (protoOptions.HasReadonly) queryOptions = queryOptions.WithReadOnly(protoOptions.Readonly);
if (protoOptions.HasScanConsistency) queryOptions = queryOptions.WithScanConsistency(protoOptions.ScanConsistency.ToCore());
if (protoOptions.Deserializer is not null) queryOptions = queryOptions.WithDeserializer(protoOptions.Deserializer.ToCore());
if (protoOptions.Timeout is not null) queryOptions = queryOptions.WithTimeout(protoOptions.Timeout.ToTimeSpan());
if (protoOptions.HasMaxRetries) queryOptions = queryOptions.WithMaxRetries((uint)protoOptions.MaxRetries);
if (protoOptions.ParametersPositional is not null)
{
queryOptions = queryOptions.WithPositionalParameters(protoOptions.ParametersPositional.Values.ToCore());
}
if (protoOptions.ParametersNamed is not null)
{
queryOptions = queryOptions.WithNamedParameters(protoOptions.ParametersNamed.Fields.ToCore());
}
if (protoOptions.Raw is not null)
{
queryOptions = queryOptions.WithRawParameters(protoOptions.Raw.Fields.ToCore());
}
return queryOptions;
}
private static IDeserializer ToCore(this Deserializer protoDeserializer)
{
return protoDeserializer.TypeCase switch
{
Deserializer.TypeOneofCase.Json => new StjJsonDeserializer(),
Deserializer.TypeOneofCase.Passthrough => new StjJsonDeserializer(),
Deserializer.TypeOneofCase.Custom => throw new NotSupportedException("Custom deserializer is not supported in .NET"),
_ => throw new ArgumentOutOfRangeException(nameof(protoDeserializer.TypeCase), "Could not parse Deserializer")
};
}
private static QueryScanConsistency ToCore(
this ExecuteQueryRequest.Types.Options.Types.ScanConsistency protoScanConsistency)
{
switch (protoScanConsistency)
{
case ExecuteQueryRequest.Types.Options.Types.ScanConsistency.NotBounded:
return QueryScanConsistency.NotBounded;
case ExecuteQueryRequest.Types.Options.Types.ScanConsistency.RequestPlus:
return QueryScanConsistency.RequestPlus;
default:
throw new ArgumentOutOfRangeException("Could not parse ScanConsistency");
}
}
private static object? ToClrObject(Value value)
{
switch (value.KindCase)
{
case Value.KindOneofCase.NullValue:
return null;
case Value.KindOneofCase.NumberValue:
// Prefer integer for integral numbers within Int64 range
var number = value.NumberValue;
if (number % 1 == 0 && number <= long.MaxValue && number >= long.MinValue)
{
return (long)number;
}
return number; // double
case Value.KindOneofCase.StringValue:
return value.StringValue;
case Value.KindOneofCase.BoolValue:
return value.BoolValue;
case Value.KindOneofCase.StructValue:
return ToClrDictionary(value.StructValue);
case Value.KindOneofCase.ListValue:
return ToClrList(value.ListValue);
case Value.KindOneofCase.None:
default:
throw new ArgumentOutOfRangeException($"Unsupported value type: {value.KindCase}");
}
}
private static List<object> ToClrList(ListValue listValue)
{
return listValue.Values.Select(ToClrObject).Select(obj => obj!).ToList();
}
private static Dictionary<string, object> ToClrDictionary(Struct structValue)
{
var dict = new Dictionary<string, object>();
foreach (var kv in structValue.Fields)
{
var obj = ToClrObject(kv.Value);
dict[kv.Key] = obj!;
}
return dict;
}
private static List<object> ToCore(this RepeatedField<Value> values)
{
return values.Select(ToClrObject).Select(obj => obj!).ToList();
}
internal static List<object> ToList(ListValue listValue)
{
return ToClrList(listValue);
}
private static Dictionary<string, object> ToCore(this MapField<string, Value> fields)
{
var dictionary = new Dictionary<string, object>();
foreach (var paramValue in fields)
{
var obj = ToClrObject(paramValue.Value);
dictionary[paramValue.Key] = obj!;
}
return dictionary;
}
}