Skip to content

Commit 50eae3c

Browse files
author
Zhen Li
committed
Added entity[x] which is the same as entity.Properties[x]
1 parent 7c39a58 commit 50eae3c

File tree

3 files changed

+104
-26
lines changed

3 files changed

+104
-26
lines changed

Neo4j.Driver/Neo4j.Driver/IEntity.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ namespace Neo4j.Driver
2323
/// </summary>
2424
public interface IEntity
2525
{
26+
/// <summary>
27+
/// Gets the value that has the specified key in <see cref="Properties"/>
28+
/// </summary>
29+
/// <param name="key">the key</param>
30+
/// <returns>the value specified by the given key in <see cref="Properties"/></returns>
31+
object this[string key] { get; }
32+
2633
/// <summary>
2734
/// Gets the unique <see cref="IIdentity"/> of the <c>Entity</c>.
2835
/// </summary>

Neo4j.Driver/Neo4j.Driver/Internal/Entity.cs

Lines changed: 96 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace Neo4j.Driver.Internal
2323
{
2424
public class Node : INode, IEquatable<INode>
2525
{
26+
public IIdentity Identity { get; }
27+
public IReadOnlyList<string> Labels { get; }
28+
public IReadOnlyDictionary<string, object> Properties { get; }
29+
public object this[string key] => Properties[key];
30+
2631
public Node(long id, IReadOnlyList<string> lables, IReadOnlyDictionary<string, object> prop)
2732
{
2833
Identity = new Identity(id);
@@ -38,10 +43,6 @@ public bool Equals(INode other)
3843
return x && y && z;
3944
}
4045

41-
public IIdentity Identity { get; }
42-
public IReadOnlyList<string> Labels { get; }
43-
public IReadOnlyDictionary<string, object> Properties { get; }
44-
4546
public override bool Equals(object obj)
4647
{
4748
if (ReferenceEquals(null, obj)) return false;
@@ -64,6 +65,13 @@ public override int GetHashCode()
6465

6566
public class Relationship : IRelationship, IEquatable<IRelationship>
6667
{
68+
public IIdentity Identity { get; }
69+
public string Type { get; }
70+
public IIdentity Start { get; internal set; }
71+
public IIdentity End { get; internal set; }
72+
public IReadOnlyDictionary<string, object> Properties { get; }
73+
public object this[string key] => Properties[key];
74+
6775
public Relationship(long id, long startId, long endId, string relType,
6876
IReadOnlyDictionary<string, object> props)
6977
{
@@ -94,16 +102,6 @@ public bool Equals(IRelationship other)
94102
return Properties.ContentEqual(other.Properties);
95103
}
96104

97-
public IIdentity Identity { get; }
98-
99-
public string Type { get; }
100-
101-
public IIdentity Start { get; internal set; }
102-
103-
public IIdentity End { get; internal set; }
104-
105-
public IReadOnlyDictionary<string, object> Properties { get; }
106-
107105
public override bool Equals(object obj)
108106
{
109107
if (ReferenceEquals(null, obj)) return false;
@@ -134,6 +132,8 @@ internal void SetStartAndEnd(IIdentity start, IIdentity end)
134132

135133
public class Identity : IIdentity, IEquatable<IIdentity>
136134
{
135+
public long Id { get; }
136+
137137
public Identity(long id)
138138
{
139139
Id = id;
@@ -144,8 +144,6 @@ public bool Equals(IIdentity other)
144144
return Id == other.Id;
145145
}
146146

147-
public long Id { get; }
148-
149147
public override bool Equals(object obj)
150148
{
151149
if (ReferenceEquals(null, obj)) return false;
@@ -160,15 +158,14 @@ public override int GetHashCode()
160158
}
161159
}
162160

163-
164161
public interface ISegment
165162
{
166163
INode Start { get; }
167164
INode End { get; }
168165
IRelationship Relationship { get; }
169166
}
170-
171-
public class Segment : ISegment
167+
168+
public class Segment : ISegment, IEquatable<ISegment>
172169
{
173170
public Segment(INode start, IRelationship rel, INode end)
174171
{
@@ -180,11 +177,40 @@ public Segment(INode start, IRelationship rel, INode end)
180177
public INode Start { get; }
181178
public INode End { get; }
182179
public IRelationship Relationship { get; }
180+
181+
public bool Equals(ISegment other)
182+
{
183+
return Equals(Start, other.Start) && Equals(End, other.End) && Equals(Relationship, other.Relationship);
184+
}
185+
186+
public override bool Equals(object obj)
187+
{
188+
if (ReferenceEquals(null, obj)) return false;
189+
if (ReferenceEquals(this, obj)) return true;
190+
if (obj.GetType() != GetType()) return false;
191+
return Equals((ISegment)obj);
192+
}
193+
194+
public override int GetHashCode()
195+
{
196+
unchecked
197+
{
198+
var hashCode = Start?.GetHashCode() ?? 0;
199+
hashCode = (hashCode * 397) ^ (End?.GetHashCode() ?? 0);
200+
hashCode = (hashCode * 397) ^ (Relationship?.GetHashCode() ?? 0);
201+
return hashCode;
202+
}
203+
}
183204
}
184205

185-
public class Path : IPath
206+
public class Path : IPath, IEquatable<IPath>
186207
{
187-
private readonly IReadOnlyList<ISegment> _segments;
208+
private readonly IReadOnlyList<ISegment> _segments; // TODO: do I need to expose this or not
209+
210+
public INode Start => Nodes.First();
211+
public INode End => Nodes.Last();
212+
public IReadOnlyList<INode> Nodes { get; }
213+
public IReadOnlyList<IRelationship> Relationships { get; }
188214

189215
public Path(IReadOnlyList<ISegment> segments, IReadOnlyList<INode> nodes,
190216
IReadOnlyList<IRelationship> relationships)
@@ -194,9 +220,54 @@ public Path(IReadOnlyList<ISegment> segments, IReadOnlyList<INode> nodes,
194220
Relationships = relationships;
195221
}
196222

197-
public INode Start => Nodes.First();
198-
public INode End => Nodes.Last();
199-
public IReadOnlyList<INode> Nodes { get; }
200-
public IReadOnlyList<IRelationship> Relationships { get; }
223+
public bool Equals(IPath other)
224+
{
225+
return Equals(Nodes, other.Nodes) && Equals(Relationships, other.Relationships);
226+
}
227+
228+
public override bool Equals(object obj)
229+
{
230+
if (ReferenceEquals(null, obj)) return false;
231+
if (ReferenceEquals(this, obj)) return true;
232+
if (obj.GetType() != GetType()) return false;
233+
return Equals((IPath)obj);
234+
}
235+
236+
public override int GetHashCode()
237+
{
238+
unchecked
239+
{
240+
var hashCode = Nodes?.GetHashCode() ?? 0;
241+
hashCode = (hashCode * 397) ^ (Relationships?.GetHashCode() ?? 0);
242+
hashCode = (hashCode * 397) ^ (_segments?.GetHashCode() ?? 0);
243+
return hashCode;
244+
}
245+
}
246+
247+
public override string ToString()
248+
{
249+
string str = "<";
250+
INode start, end = null;
251+
IRelationship rel;
252+
int i = 0;
253+
foreach (var segment in _segments)
254+
{
255+
start = Nodes[i];
256+
end = Nodes[i + 1];
257+
rel = Relationships[i];
258+
259+
if (segment.Start.Equals(start))
260+
{
261+
str += start + "-" + rel + "->";
262+
}
263+
else
264+
{
265+
str += start + "<-" + rel + "-";
266+
}
267+
}
268+
269+
str += end + ">";
270+
return str;
271+
}
201272
}
202273
}

Neo4j.Driver/Neo4j.Driver/Internal/Result/Record.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Record : IRecord
2929
public IReadOnlyDictionary<string, dynamic> Values { get; }
3030
public IReadOnlyList<string> Keys { get; }
3131

32-
public Record(string[] keys, dynamic[] values )
32+
public Record(string[] keys, dynamic[] values)
3333
{
3434
Throw.ArgumentException.IfNotEqual(keys.Length, values.Length, nameof(keys), nameof(values));
3535

0 commit comments

Comments
 (0)