-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentAsExtensions.cs
More file actions
82 lines (77 loc) · 3.23 KB
/
ContentAsExtensions.cs
File metadata and controls
82 lines (77 loc) · 3.23 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
using System.Text.Json;
using Couchbase.AnalyticsClient.Results;
using Couchbase.Grpc.Protocol.Columnar;
using Google.Protobuf;
using Google.Protobuf.WellKnownTypes;
namespace Couchbase.Analytics.Performer.Internal.Utils;
public static class ContentAsExtensions
{
public static ContentWas ContentAsToAnalyticsRow(this AnalyticsRow analyticsRow, ContentAs contentAs)
{
switch (contentAs.AsCase)
{
case ContentAs.AsOneofCase.AsByteArray:
return new ContentWas { ContentWasBytes = ByteString.CopyFrom(analyticsRow.ContentAs<byte[]>()) };
case ContentAs.AsOneofCase.AsList:
var listJson = analyticsRow.ContentAs<JsonElement>();
var listValue = ConvertJsonArrayToList(listJson);
return new ContentWas { ContentWasList = listValue };
case ContentAs.AsOneofCase.AsMap:
var json = analyticsRow.ContentAs<JsonElement>();
var structValue = ConvertJsonObjectToStruct(json);
return new ContentWas { ContentWasMap = structValue };
case ContentAs.AsOneofCase.AsString:
return new ContentWas { ContentWasString = analyticsRow.ContentAs<string>() };
case ContentAs.AsOneofCase.None:
default:
throw new ArgumentException("No contentAs specified");
}
}
private static Struct ConvertJsonObjectToStruct(JsonElement jsonObject)
{
var result = new Struct();
foreach (var property in jsonObject.EnumerateObject())
{
result.Fields[property.Name] = ConvertJsonToProtoValue(property.Value);
}
return result;
}
private static ListValue ConvertJsonArrayToList(JsonElement jsonArray)
{
var list = new ListValue();
foreach (var item in jsonArray.EnumerateArray())
{
list.Values.Add(ConvertJsonToProtoValue(item));
}
return list;
}
private static Value ConvertJsonToProtoValue(JsonElement element)
{
switch (element.ValueKind)
{
case JsonValueKind.String:
return new Value { StringValue = element.GetString() };
case JsonValueKind.Number:
if (element.TryGetInt64(out var longValue))
{
return new Value { NumberValue = longValue };
}
if (element.TryGetDouble(out var doubleValue))
{
return new Value { NumberValue = doubleValue };
}
return new Value { NumberValue = double.Parse(element.GetRawText(), System.Globalization.CultureInfo.InvariantCulture) };
case JsonValueKind.True:
case JsonValueKind.False:
return new Value { BoolValue = element.GetBoolean() };
case JsonValueKind.Object:
return new Value { StructValue = ConvertJsonObjectToStruct(element) };
case JsonValueKind.Array:
return new Value { ListValue = ConvertJsonArrayToList(element) };
case JsonValueKind.Null:
case JsonValueKind.Undefined:
default:
return new Value { NullValue = NullValue.NullValue };
}
}
}