Skip to content

Commit 7cafc36

Browse files
authored
Merge pull request #29 from nitrictech/feature/document-service
Feature/document service
2 parents 9a5d0d1 + 63e6c55 commit 7cafc36

File tree

16 files changed

+1427
-13
lines changed

16 files changed

+1427
-13
lines changed

.github/workflows/publish-on-release.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ jobs:
99
runs-on: ubuntu-latest
1010

1111
steps:
12-
- uses: actions/checkout@v2
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
with:
15+
token: ${{secrets.SUB_MODULE_PAT}}
16+
submodules: recursive
1317
- name: Setup .NET Core
1418
uses: actions/setup-dotnet@v1
1519
with:
@@ -33,6 +37,6 @@ jobs:
3337
- name: Publish
3438
run: |
3539
mkdir __out
36-
dotnet pack -c Release -o __out --no-restore
40+
dotnet pack -c Release -o __out
3741
dotnet nuget push "./__out/*.nupkg" --skip-duplicate --no-symbols true --api-key ${{secrets.NUGET_TOKEN}}
3842
rm -rf __out

Nitric.Sdk/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Proto/

Nitric.Sdk/Common/Util.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public static Struct ObjToStruct(object obj)
2828
string json = ObjToJson(obj);
2929
return JsonParser.Default.Parse<Struct>(json);
3030
}
31-
3231
public static string GetEnvVar(string variable, string defaultValue = "")
3332
{
3433
var envVar = Environment.GetEnvironmentVariable(variable);
@@ -52,7 +51,7 @@ public static object JsonToObj(byte[] json)
5251

5352
public static Dictionary<string, object> ObjToDict(object obj)
5453
{
55-
return (Dictionary<string, object>) JsonToObj(ObjToJson(obj));
54+
return (Dictionary<string, object>)JsonToObj(ObjToJson(obj));
5655
}
5756

5857
public static Dictionary<string, List<string>> NameValueCollecToDict(NameValueCollection col)
@@ -73,5 +72,14 @@ public static Dictionary<string, string> CollectionToDict(IDictionary<string, st
7372
}
7473
return newDict;
7574
}
75+
public static IDictionary<string, object> DictToCollection<T>(Dictionary<string, object> dictionary) where T : IDictionary<string, object>, new()
76+
{
77+
IDictionary<string, object> dict = new T();
78+
foreach (KeyValuePair<string, object> kv in dictionary)
79+
{
80+
dict.Add(kv);
81+
}
82+
return dict;
83+
}
7684
}
7785
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using DocumentServiceClient = Nitric.Proto.Document.v1.DocumentService.DocumentServiceClient;
16+
using Collection = Nitric.Proto.Document.v1.Collection;
17+
using GrpcKey = Nitric.Proto.Document.v1.Key;
18+
using System.Collections.Generic;
19+
namespace Nitric.Api.Document
20+
{
21+
public class CollectionRef<T> where T : IDictionary<string, object>, new()
22+
{
23+
private string name;
24+
public readonly Key<T> ParentKey;
25+
private DocumentServiceClient documentClient;
26+
27+
internal CollectionRef(DocumentServiceClient documentClient, string name, Key<T> parentKey = null)
28+
{
29+
this.documentClient = documentClient;
30+
this.name = name;
31+
this.ParentKey = parentKey;
32+
}
33+
public DocumentRef<T> Doc(string documentId)
34+
{
35+
if (string.IsNullOrEmpty(documentId))
36+
{
37+
throw new ArgumentNullException(documentId);
38+
}
39+
return new DocumentRef<T>(
40+
this.documentClient,
41+
this,
42+
documentId);
43+
}
44+
public Query<T> Query()
45+
{
46+
return new Query<T>(this.documentClient, this);
47+
}
48+
internal Collection ToGrpcCollection()
49+
{
50+
var collection = new Collection()
51+
{
52+
Name = this.name,
53+
};
54+
if (this.ParentKey != null)
55+
{
56+
collection.Parent = this.ParentKey.ToKey();
57+
}
58+
return collection;
59+
}
60+
}
61+
}
62+

Nitric.Sdk/Document/Document.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System.Collections.Generic;
15+
namespace Nitric.Api.Document
16+
{
17+
public class Document<T> where T : IDictionary<string, object>, new()
18+
{
19+
public DocumentRef<T> Ref { get; private set; }
20+
public T Content { get; private set; }
21+
22+
internal Document(DocumentRef<T> documentRef, T content)
23+
{
24+
this.Ref = documentRef;
25+
this.Content = content;
26+
}
27+
}
28+
}

Nitric.Sdk/Document/DocumentRef.cs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using System.Collections.Generic;
16+
using DocumentServiceClient = Nitric.Proto.Document.v1.DocumentService.DocumentServiceClient;
17+
using GrpcKey = Nitric.Proto.Document.v1.Key;
18+
using Nitric.Proto.Document.v1;
19+
using Util = Nitric.Api.Common.Util;
20+
using Google.Protobuf.WellKnownTypes;
21+
namespace Nitric.Api.Document
22+
{
23+
public class DocumentRef<T> where T : IDictionary<string, object>, new()
24+
{
25+
const int DEPTH_LIMIT = 1;
26+
27+
private readonly DocumentServiceClient documentClient;
28+
public readonly Key<T> Key;
29+
private readonly CollectionRef<T> collection;
30+
31+
protected DocumentRef() { }
32+
internal DocumentRef(
33+
DocumentServiceClient documentClient,
34+
CollectionRef<T> collection,
35+
string documentId)
36+
{
37+
this.documentClient = documentClient;
38+
this.Key = new Key<T>(collection, documentId);
39+
this.collection = collection;
40+
}
41+
42+
public Document<T> Get()
43+
{
44+
DocumentGetRequest request = new DocumentGetRequest();
45+
request.Key = this.Key.ToKey();
46+
47+
var response = this.documentClient.Get(request);
48+
49+
return new Document<T>(
50+
this,
51+
DocumentToGeneric(response.Document.Content)
52+
);
53+
}
54+
public void Set(T value)
55+
{
56+
if (value == null)
57+
{
58+
throw new ArgumentNullException("Provide non-null value");
59+
}
60+
var request = new DocumentSetRequest
61+
{
62+
Key = this.Key.ToKey(),
63+
Content = Util.ObjToStruct(value),
64+
};
65+
this.documentClient.Set(request);
66+
}
67+
68+
public void Delete()
69+
{
70+
var request = new DocumentDeleteRequest
71+
{
72+
Key = this.Key.ToKey(),
73+
};
74+
this.documentClient.Delete(request);
75+
}
76+
77+
private int Depth(int depth, Collection collection)
78+
{
79+
return (collection.Parent != null) ?
80+
Depth(depth + 1, collection.Parent.Collection) : depth;
81+
}
82+
83+
public CollectionRef<T> Collection(string name)
84+
{
85+
if (string.IsNullOrEmpty(name))
86+
{
87+
throw new ArgumentNullException(name);
88+
}
89+
if (Depth(0, this.collection.ToGrpcCollection()) >= DEPTH_LIMIT)
90+
{
91+
throw new NotSupportedException("Currently subcollection are only able to be nested " + DEPTH_LIMIT + "deep");
92+
}
93+
return new CollectionRef<T>(this.documentClient, name, this.Key);
94+
}
95+
96+
//Utility function to convert a struct document to its generic counterpart
97+
protected T DocumentToGeneric(Struct content)
98+
{
99+
T doc = new T();
100+
foreach (var kv in content.Fields)
101+
{
102+
doc.Add(kv.Key, UnwrapValue(kv.Value));
103+
}
104+
return doc;
105+
}
106+
private object UnwrapValue(Value value)
107+
{
108+
switch (value.KindCase)
109+
{
110+
case Value.KindOneofCase.StringValue:
111+
return value.StringValue;
112+
case Value.KindOneofCase.BoolValue:
113+
return value.BoolValue;
114+
case Value.KindOneofCase.NumberValue:
115+
return value.NumberValue;
116+
case Value.KindOneofCase.NullValue:
117+
return null;
118+
case Value.KindOneofCase.StructValue:
119+
Dictionary<string, object> unwrappedStruct = new Dictionary<string, object>();
120+
foreach (var kv in value.StructValue.Fields)
121+
{
122+
unwrappedStruct.Add(kv.Key, UnwrapValue(kv.Value));
123+
}
124+
return unwrappedStruct;
125+
case Value.KindOneofCase.ListValue:
126+
List<object> unwrappedList = new List<object>();
127+
foreach (Value v in value.ListValue.Values)
128+
{
129+
unwrappedList.Add(UnwrapValue(v));
130+
}
131+
return unwrappedList;
132+
default:
133+
throw new ArgumentException("Provide proto-value");
134+
}
135+
}
136+
}
137+
}

Nitric.Sdk/Document/Documents.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using System.Collections.Generic;
16+
using DocumentServiceClient = Nitric.Proto.Document.v1.DocumentService.DocumentServiceClient;
17+
using Nitric.Api.Common;
18+
namespace Nitric.Api.Document
19+
{
20+
public class Documents : AbstractClient
21+
{
22+
private DocumentServiceClient documentClient;
23+
public Documents(DocumentServiceClient client = null)
24+
{
25+
this.documentClient = (client != null) ? client : new DocumentServiceClient(this.GetChannel());
26+
}
27+
28+
public CollectionRef<T> Collection<T>(string name) where T : IDictionary<string, object>, new()
29+
{
30+
if (string.IsNullOrEmpty(name))
31+
{
32+
throw new ArgumentNullException(name);
33+
}
34+
return new CollectionRef<T>(this.documentClient, name);
35+
}
36+
}
37+
}

Nitric.Sdk/Document/Key.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2021, Nitric Technologies Pty Ltd.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
using System;
15+
using System.Collections.Generic;
16+
using Collection = Nitric.Proto.Document.v1.Collection;
17+
using GrpcKey = Nitric.Proto.Document.v1.Key;
18+
19+
namespace Nitric.Api.Document
20+
{
21+
public class Key<T> where T : IDictionary<string, object>, new()
22+
{
23+
public readonly CollectionRef<T> collection;
24+
public readonly string id;
25+
26+
public Key(CollectionRef<T> collection, string id = null)
27+
{
28+
if (collection == null)
29+
{
30+
throw new ArgumentNullException("Provide non-null collection");
31+
}
32+
this.collection = collection;
33+
this.id = id != null ? id : "";
34+
}
35+
public GrpcKey ToKey()
36+
{
37+
return new GrpcKey
38+
{
39+
Collection = this.collection.ToGrpcCollection(),
40+
Id = this.id,
41+
};
42+
}
43+
public override string ToString()
44+
{
45+
return this.GetType().Name + "[collection=" + collection + ", id=" + id + "]";
46+
}
47+
}
48+
}

0 commit comments

Comments
 (0)