Skip to content

Commit 3079530

Browse files
feat: Add Document object and change objects reference to collectionsRef
1 parent d6d3f3d commit 3079530

File tree

6 files changed

+88
-47
lines changed

6 files changed

+88
-47
lines changed

Nitric.Sdk/Document/CollectionRef.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,14 @@ namespace Nitric.Api.Document
2020
{
2121
public class CollectionRef<T> where T : IDictionary<string, object>, new()
2222
{
23-
private Collection collection;
23+
private string name;
24+
public readonly Key<T> ParentKey;
2425
private DocumentServiceClient documentClient;
2526

26-
internal CollectionRef(DocumentServiceClient documentClient, string name, GrpcKey parentKey = null)
27+
internal CollectionRef(DocumentServiceClient documentClient, string name, Key<T> parentKey = null)
2728
{
28-
this.documentClient = documentClient;
29-
30-
var collection = new Collection()
31-
{
32-
Name = name,
33-
};
34-
if (parentKey != null)
35-
{
36-
collection.Parent = parentKey;
37-
}
38-
this.collection = collection;
29+
this.name = name;
30+
this.ParentKey = parentKey;
3931
}
4032
public DocumentRef<T> Doc(string documentId)
4133
{
@@ -45,12 +37,24 @@ public DocumentRef<T> Doc(string documentId)
4537
}
4638
return new DocumentRef<T>(
4739
this.documentClient,
48-
this.collection,
40+
this,
4941
documentId);
5042
}
5143
public Query<T> Query()
5244
{
53-
return new Query<T>(this.documentClient, this.collection);
45+
return new Query<T>(this.documentClient, this);
46+
}
47+
internal Collection ToGrpcCollection()
48+
{
49+
var collection = new Collection()
50+
{
51+
Name = this.name,
52+
};
53+
if (this.ParentKey != null)
54+
{
55+
collection.Parent = this.ParentKey.ToKey();
56+
}
57+
return collection;
5458
}
5559
}
5660
}

Nitric.Sdk/Document/Document.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
namespace Nitric.Api.Document
4+
{
5+
public class Document<T> where T : IDictionary<string, object>, new()
6+
{
7+
public DocumentRef<T> Ref { get; private set; }
8+
public T Content { get; private set; }
9+
10+
internal Document(DocumentRef<T> documentRef, T content)
11+
{
12+
this.Ref = documentRef;
13+
this.Content = content;
14+
}
15+
}
16+
}

Nitric.Sdk/Document/DocumentRef.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,31 @@ namespace Nitric.Api.Document
2525
const int DEPTH_LIMIT = 1;
2626

2727
private readonly DocumentServiceClient documentClient;
28-
private readonly GrpcKey key;
29-
private readonly Collection collection;
28+
public readonly Key<T> Key;
29+
private readonly CollectionRef<T> collection;
3030

3131
internal DocumentRef(
3232
DocumentServiceClient documentClient,
33-
Collection collection,
33+
CollectionRef<T> collection,
3434
string documentId)
3535
{
3636
this.documentClient = documentClient;
37-
this.key = new Key(collection, documentId).ToKey();
37+
this.Key = new Key<T>(collection, documentId);
3838
this.collection = collection;
3939
}
4040

41-
public T Get()
41+
public Document<T> Get()
4242
{
4343
DocumentGetRequest request = new DocumentGetRequest();
44-
request.Key = this.key;
44+
request.Key = this.Key.ToKey();
4545

4646
DocumentGetResponse response = null;
4747
response = this.documentClient.Get(request);
4848

49-
return DocumentToGeneric(response.Document.Content);
49+
return new Document<T>(
50+
this,
51+
DocumentToGeneric(response.Document.Content)
52+
);
5053
}
5154
public void Set(T value)
5255
{
@@ -56,7 +59,7 @@ public void Set(T value)
5659
}
5760
var request = new DocumentSetRequest
5861
{
59-
Key = this.key,
62+
Key = this.Key.ToKey(),
6063
Content = Util.ObjToStruct(value),
6164
};
6265
this.documentClient.Set(request);
@@ -66,7 +69,7 @@ public void Delete()
6669
{
6770
var request = new DocumentDeleteRequest
6871
{
69-
Key = this.key,
72+
Key = this.Key.ToKey(),
7073
};
7174
this.documentClient.Delete(request);
7275
}
@@ -83,11 +86,11 @@ public CollectionRef<T> Collection(string name)
8386
{
8487
throw new ArgumentNullException(name);
8588
}
86-
if (Depth(0, this.collection) >= DEPTH_LIMIT)
89+
if (Depth(0, this.collection.ToGrpcCollection()) >= DEPTH_LIMIT)
8790
{
8891
throw new NotSupportedException("Currently subcollection are only able to be nested " + DEPTH_LIMIT + "deep");
8992
}
90-
return new CollectionRef<T>(this.documentClient, name, this.key);
93+
return new CollectionRef<T>(this.documentClient, name, this.Key);
9194
}
9295

9396
//Utility function to convert a struct document to its generic counterpart

Nitric.Sdk/Document/Key.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414
using System;
15+
using System.Collections.Generic;
1516
using Collection = Nitric.Proto.Document.v1.Collection;
1617
using GrpcKey = Nitric.Proto.Document.v1.Key;
1718

1819
namespace Nitric.Api.Document
1920
{
20-
public class Key
21+
public class Key<T> where T : IDictionary<string, object>, new()
2122
{
22-
Collection collection;
23-
string id;
23+
public readonly CollectionRef<T> collection;
24+
public readonly string id;
2425

25-
public Key(Collection collection, string id = null)
26+
public Key(CollectionRef<T> collection, string id = null)
2627
{
2728
if (collection == null)
2829
{
@@ -35,7 +36,7 @@ public GrpcKey ToKey()
3536
{
3637
return new GrpcKey
3738
{
38-
Collection = this.collection,
39+
Collection = this.collection.ToGrpcCollection(),
3940
Id = this.id,
4041
};
4142
}

Nitric.Sdk/Document/Query.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ namespace Nitric.Api.Document
2525
public class Query<T> where T : IDictionary<string, object>, new()
2626
{
2727
internal DocumentServiceClient documentClient;
28-
internal Collection collection;
28+
internal CollectionRef<T> collection;
2929
internal List<Expression> expressions;
3030
internal object pagingToken;
3131
internal int limit;
3232

33-
internal Query(DocumentServiceClient documentClient, Collection collection)
33+
internal Query(DocumentServiceClient documentClient, CollectionRef<T> collection)
3434
{
3535
this.documentClient = documentClient;
3636
this.collection = collection;
@@ -93,7 +93,7 @@ public override string ToString()
9393
internal readonly Query<T> query;
9494
internal readonly bool paginateAll;
9595
public object PagingToken { get; private set; }
96-
public List<T> QueryData { get; private set; }
96+
public List<Document<T>> QueryData { get; private set; }
9797

9898
internal QueryResult(Query<T> query, bool paginateAll)
9999
{
@@ -110,7 +110,7 @@ internal QueryResult(Query<T> query, bool paginateAll)
110110
internal DocumentQueryRequest BuildDocQueryRequest(List<Expression> expressions)
111111
{
112112
var request = new DocumentQueryRequest();
113-
request.Collection = this.query.collection;
113+
request.Collection = this.query.collection.ToGrpcCollection();
114114

115115
foreach (Expression expression in expressions)
116116
{
@@ -133,19 +133,33 @@ internal DocumentQueryRequest BuildDocQueryRequest(List<Expression> expressions)
133133

134134
internal void LoadPageData(DocumentQueryResponse response)
135135
{
136-
QueryData = new List<T>(response.Documents.Count);
137-
136+
QueryData = new List<Document<T>>(response.Documents.Count);
137+
var collection = this.query.collection.ToGrpcCollection();
138138
foreach (var doc in response.Documents)
139139
{
140140
var dict = Util.ObjToDict(doc.Content);
141141

142142
if (typeof(T).IsAssignableFrom(dict.GetType()))
143143
{
144-
QueryData.Add((T)Util.DictToCollection<T>(dict));
144+
QueryData.Add(new Document<T>(
145+
new DocumentRef<T>(
146+
this.query.documentClient,
147+
this.query.collection,
148+
this.query.collection.ParentKey.id
149+
),
150+
(T)Util.DictToCollection<T>(dict))
151+
);
145152
}
146153
else
147154
{
148-
QueryData.Add((T)Util.DictToCollection<T>(Util.ObjToDict(dict)));
155+
QueryData.Add(new Document<T>(
156+
new DocumentRef<T>(
157+
this.query.documentClient,
158+
this.query.collection,
159+
this.query.collection.ParentKey.id
160+
),
161+
(T)Util.DictToCollection<T>(Util.ObjToDict(dict)))
162+
);
149163
}
150164
}
151165
this.PagingToken = Util.CollectionToDict(response.PagingToken);

Nitric.Test/Api/Document/DocumentsTest.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ public void TestGetWithDictionary()
105105
.Collection<Dictionary<string, object>>("test-collection")
106106
.Doc("test-document");
107107
var response = documentRef.Get();
108-
Assert.AreEqual(response["test"], "document");
108+
109+
Assert.AreEqual(response.Content["test"], "document");
110+
Assert.AreEqual(response.Ref, documentRef);
109111

110112
dc.Verify(t => t.Get(It.IsAny<DocumentGetRequest>(), null, null, It.IsAny<System.Threading.CancellationToken>()), Times.Once);
111113
}
@@ -138,7 +140,8 @@ public void TestGetWithSortedDictionary()
138140
.Collection<SortedDictionary<string, object>>("test-collection")
139141
.Doc("test-document");
140142
var response = documentRef.Get();
141-
Assert.AreEqual(response["test"], "document");
143+
Assert.AreEqual(response.Content["test"], "document");
144+
Assert.AreEqual(response.Ref, documentRef);
142145

143146
dc.Verify(t => t.Get(It.IsAny<DocumentGetRequest>(), null, null, It.IsAny<System.Threading.CancellationToken>()), Times.Once);
144147
}
@@ -444,7 +447,7 @@ public void TestFetch()
444447
{
445448
var response = query.Fetch();
446449
Assert.AreEqual(response.QueryData.Count, 1);
447-
Assert.AreEqual(response.QueryData[0], "document");
450+
Assert.AreEqual(response.QueryData[0].Content["test"], "document");
448451
Assert.AreEqual(response.PagingToken, pagingToken);
449452
}
450453
catch (Exception)
@@ -488,7 +491,7 @@ public void TestFetchConformsToLimitOfOne()
488491
.Limit(1)
489492
.Fetch();
490493
Assert.AreEqual(response.QueryData.Count, 1);
491-
Assert.AreEqual(response.QueryData[0], "document");
494+
Assert.AreEqual(response.QueryData[0].Content["test"], "document");
492495
Assert.AreEqual(response.PagingToken, pagingToken);
493496
}
494497
catch (Exception)
@@ -533,7 +536,7 @@ public void TestFetchWithExpressions()
533536
.Where("last_name", "==", "smith")
534537
.Fetch();
535538
Assert.AreEqual(response.QueryData.Count, 1);
536-
Assert.AreEqual(response.QueryData[0], "document");
539+
Assert.AreEqual(response.QueryData[0].Content["test"], "document");
537540
Assert.AreEqual(response.PagingToken, pagingToken);
538541
}
539542
catch (Exception)
@@ -580,7 +583,7 @@ public void TestFetchWithPagingToken()
580583
.PagingFrom(pagingToken)
581584
.Fetch();
582585
Assert.AreEqual(response.QueryData.Count, 1);
583-
Assert.AreEqual(response.QueryData[0], "document");
586+
Assert.AreEqual(response.QueryData[0].Content["test"], "document");
584587
Assert.AreEqual(response.PagingToken, updatedPagingToken);
585588
}
586589
catch (Exception)
@@ -623,8 +626,8 @@ public void TestFetchAll()
623626
{
624627
var response = query.FetchAll();
625628
Assert.AreEqual(response.QueryData.Count, 1);
626-
Assert.AreEqual(response.QueryData[0], "john smith");
627-
Assert.AreEqual(response.QueryData[1], "jane doe");
629+
Assert.AreEqual(response.QueryData[0].Content["0"], "john smith");
630+
Assert.AreEqual(response.QueryData[1].Content["1"], "jane doe");
628631
Assert.AreEqual(response.PagingToken, pagingToken);
629632
}
630633
catch (Exception)

0 commit comments

Comments
 (0)