Skip to content

Commit 70979ce

Browse files
author
Eduardo V. Bruno
authored
Merge pull request #116 from fauna/drv-228-tostring
DRV-228 update Fauna types ToString
2 parents b4d796b + 6f575ba commit 70979ce

File tree

10 files changed

+104
-15
lines changed

10 files changed

+104
-15
lines changed

FaunaDB.Client.Test/EncoderTest.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using FaunaDB.Query;
34
using FaunaDB.Types;
45
using NUnit.Framework;
56

@@ -397,5 +398,49 @@ public void TestStringOverride()
397398
Encode(new StringOverride(testUri, testGuid))
398399
);
399400
}
401+
402+
[Test]
403+
public void TestToStringTypes()
404+
{
405+
Assert.AreEqual(
406+
"SetRef({\"match\": Ref(id=\"spells_by_element\", collection=Ref(id=\"indexes\")), \"terms\": \"water\"})",
407+
new SetRefV(new Dictionary<string, Value>() {
408+
{ "terms", StringV.Of("water") },
409+
{ "match", new RefV("spells_by_element", new RefV("indexes")) }
410+
}).ToString()
411+
);
412+
413+
Assert.AreEqual("true", BooleanV.Of(true).ToString());
414+
Assert.AreEqual("3.14", DoubleV.Of(3.14).ToString());
415+
Assert.AreEqual("42", LongV.Of(42).ToString());
416+
Assert.AreEqual("null", NullV.Instance.ToString());
417+
Assert.AreEqual("Date(\"2001-01-01\")", new DateV("2001-01-01").ToString());
418+
Assert.AreEqual("Time(\"2000-01-01T01:10:30.123Z\")", new TimeV("2000-01-01T01:10:30.123Z").ToString());
419+
Assert.AreEqual(@"[1, 3.14, true, ""foo bar"", ""my \""quote""]", ArrayV.Of(1, 3.14, true, "foo bar", "my \"quote").ToString());
420+
Assert.AreEqual("Bytes(0x01, 0x02, 0x03)", BytesV.Of(0x1, 0x2, 0x3).ToString());
421+
422+
Assert.AreEqual("{\"answer\": 42, \"question\": \"meaning\"}",
423+
ObjectV.With("answer", 42, "question", "meaning").ToString());
424+
Assert.AreEqual("{\"answer\": 42, \"question\": \"meaning\"}",
425+
ObjectV.With("question", "meaning", "answer", 42).ToString());
426+
427+
Assert.AreEqual(
428+
"Query({\"api_version\": \"3\", \"expr\": {\"add\": [{\"var\": \"x\"}, 1]}, \"lambda\": \"x\"})",
429+
new QueryV(new Dictionary<string, Expr>() {
430+
{"lambda", StringV.Of("x")},
431+
{"expr", ObjectV.With("add", ArrayV.Of(ObjectV.With("var", "x"), 1))},
432+
{ "api_version", StringV.Of("3") }
433+
}).ToString()
434+
);
435+
436+
Assert.AreEqual(
437+
"Query({\"api_version\": \"3\", \"expr\": {\"add\": [{\"var\": \"x\"}, 1, 3.14]}, \"lambda\": \"x\"})",
438+
new QueryV(new Dictionary<string, Expr>() {
439+
{"lambda", "x"},
440+
{"expr", Add(Var("x"), 1, 3.14)},
441+
{ "api_version", "3" }
442+
}).ToString()
443+
);
444+
}
400445
}
401446
}

FaunaDB.Client/Collections/DictionaryExtension.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Linq;
1+
using System;
2+
using System.Linq;
23
using System.Collections.Generic;
34

45
namespace FaunaDB.Collections
@@ -25,6 +26,24 @@ public static bool DictEquals<TKey, TValue>(this IReadOnlyDictionary<TKey, TValu
2526

2627
public static Dictionary<TKey, TValue> FilterNulls<TKey, TValue>(this Dictionary<TKey, TValue> dict) =>
2728
dict.Where(kv => kv.Value != null).ToDictionary(kv => kv.Key, kv => kv.Value);
29+
30+
public static string Debug<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> source)
31+
where TKey: IComparable
32+
{
33+
List<TKey> elements = source.Keys.ToList();
34+
elements.Sort();
35+
36+
string[] result = new string[elements.Count];
37+
38+
for (int i = 0; i < result.Length; i++)
39+
{
40+
var key = elements[i];
41+
var value = source[key].ToString();
42+
result[i] = $"\"{key}\": {value}";
43+
}
44+
45+
return string.Join(", ", result);
46+
}
2847
}
2948
}
3049

FaunaDB.Client/Query/Unescaped.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ protected internal override void WriteJson(JsonWriter writer) =>
3636

3737
public override string ToString()
3838
{
39-
var props = string.Join(",", from kv in Values select $"{kv.Key}: {kv.Value}");
40-
return $"UObject({props})";
39+
var props = Values.Debug();
40+
return $"{{{props}}}";
4141
}
4242

4343
public static UnescapedObject With(Dictionary<string, Expr> exprs) =>
@@ -101,6 +101,6 @@ protected internal override void WriteJson(JsonWriter writer) =>
101101
writer.WriteArray(Value);
102102

103103
public override string ToString() =>
104-
$"UArr({string.Join(", ", Value)})";
104+
$"[{string.Join(", ", Value)}]";
105105
}
106106
}

FaunaDB.Client/Types/ArrayV.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected override int HashCode() =>
8181
HashUtil.Hash(Value);
8282

8383
public override string ToString() =>
84-
$"Arr({string.Join(", ", Value)})";
84+
$"[{string.Join(", ", Value)}]";
8585
#endregion
8686
}
8787
}

FaunaDB.Client/Types/BytesV.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected internal override void WriteJson(JsonWriter writer)
4848
public override string ToString()
4949
{
5050
var str = Value.Select(b => string.Format("0x{0:x2}", b));
51-
return "BytesV(" + string.Join(", ", str) + ")";
51+
return "Bytes(" + string.Join(", ", str) + ")";
5252
}
5353

5454
/// <summary>

FaunaDB.Client/Types/NullV.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override bool Equals(Expr v) =>
2222
object.ReferenceEquals(this, v);
2323

2424
public override string ToString() =>
25-
"NullV";
25+
"null";
2626

2727
protected override int HashCode() =>
2828
0;

FaunaDB.Client/Types/ObjectV.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ protected override int HashCode() =>
7878

7979
public override string ToString()
8080
{
81-
var props = string.Join(",", from kv in Value select $"{kv.Key}: {kv.Value}");
82-
return $"ObjectV({props})";
81+
var props = Value.Debug();
82+
return $"{{{props}}}";
8383
}
8484
#endregion
8585

FaunaDB.Client/Types/QueryV.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@ public override bool Equals(Expr v)
3030
var w = v as QueryV;
3131
return w != null && Value.DictEquals(w.Value);
3232
}
33+
34+
public override string ToString()
35+
{
36+
var content = Value.Debug();
37+
return $"Query({{{content}}})";
38+
}
3339
}
3440
}

FaunaDB.Client/Types/RefV.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ protected override int HashCode() =>
6262

6363
public override string ToString()
6464
{
65-
var cls = Collection != null ? $", collection = {Collection}" : string.Empty;
66-
var db = Database != null ? $", database = {Database}" : string.Empty;
65+
var cls = Collection != null ? $", collection={Collection}" : string.Empty;
66+
var db = Database != null ? $", database={Database}" : string.Empty;
6767

68-
return $"RefV(id = \"{Id}\"{cls}{db})";
68+
return $"Ref(id=\"{Id}\"{cls}{db})";
6969
}
7070
}
7171

FaunaDB.Client/Types/ScalarValue.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected override int HashCode() =>
3535
Value.GetHashCode();
3636

3737
public override string ToString() =>
38-
$"{GetType().Name}({Value})";
38+
$"{Value}";
3939
#endregion
4040
}
4141

@@ -51,6 +51,9 @@ public static BooleanV Of(bool b) =>
5151

5252
public static readonly BooleanV True = new BooleanV(true);
5353
public static readonly BooleanV False = new BooleanV(false);
54+
55+
public override string ToString() =>
56+
Value.ToString().ToLower();
5457
}
5558

5659
/// <summary>
@@ -88,6 +91,14 @@ internal StringV(string value) : base(value)
8891

8992
public static StringV Of(string v) =>
9093
new StringV(v);
94+
95+
public override string ToString()
96+
{
97+
var s = Value.ToString().Replace("\"", "\\\"");
98+
return $"\"{s}\"";
99+
}
100+
101+
91102
}
92103

93104
/// <summary>
@@ -113,6 +124,7 @@ protected internal override void WriteJson(JsonWriter writer)
113124
writer.WriteEndObject();
114125
}
115126

127+
#region boilerplate
116128
public override bool Equals(Expr v)
117129
{
118130
var other = v as SetRefV;
@@ -121,6 +133,13 @@ public override bool Equals(Expr v)
121133

122134
public override int GetHashCode() =>
123135
Value.GetHashCode();
136+
137+
public override string ToString()
138+
{
139+
var content = Value.Debug();
140+
return $"SetRef({{{content}}})";
141+
}
142+
#endregion
124143
}
125144

126145
/// <summary>
@@ -206,7 +225,7 @@ protected override int HashCode() =>
206225
Value.GetHashCode();
207226

208227
override public string ToString() =>
209-
$"FaunaTime({Value.ToIso(TimeFormat)})";
228+
$"Time(\"{Value.ToIso(TimeFormat)}\")";
210229
#endregion
211230
}
212231

@@ -270,7 +289,7 @@ protected override int HashCode() =>
270289
Value.GetHashCode();
271290

272291
override public string ToString() =>
273-
$"FaunaDate({Value})";
292+
$"Date(\"{Value.ToIso(DateFormat)}\")";
274293
#endregion
275294
}
276295

0 commit comments

Comments
 (0)