1
- using System . Text . Json ;
1
+ using System . Text ;
2
2
using System . Text . Json . Nodes ;
3
+ using Humanizer ;
3
4
using Json . More ;
4
5
5
6
namespace LearnJsonEverything . Services ;
@@ -66,14 +67,46 @@ private static string BuildApiLink(LessonData lesson) =>
66
67
67
68
private static string BuildTestList ( JsonArray testData )
68
69
{
69
- var tests = testData . Deserialize ( SerializerContext . Default . SchemaTestArray ) ! ;
70
- string [ ] lines =
71
- [
72
- "| Instance | Is Valid |" ,
73
- "|:-|:-:|" ,
74
- .. tests . Select ( test => $ "|`{ test . Instance . AsJsonString ( ) } `|{ test . IsValid } |")
75
- ] ;
76
-
77
- return string . Join ( Environment . NewLine , lines ) ;
70
+ var lines = new List < string > ( ) ;
71
+
72
+ var sample = testData [ 0 ] ! . AsObject ( ) ;
73
+ lines . Add ( $ "|{ string . Join ( "|" , sample . Select ( x => x . Key . Pascalize ( ) . Humanize ( ) ) ) } |") ;
74
+ lines . Add ( $ "|{ string . Join ( "|" , sample . Select ( _ => ":-" ) ) } |") ;
75
+
76
+ foreach ( var test in testData )
77
+ {
78
+ var lineContent = new StringBuilder ( "|" ) ;
79
+ foreach ( var kvp in sample )
80
+ {
81
+ lineContent . Append ( MaybeCode ( test ! [ kvp . Key ] , kvp . Key ) ) ;
82
+ lineContent . Append ( '|' ) ;
83
+ }
84
+
85
+ lines . Add ( lineContent . ToString ( ) ) ;
86
+ }
87
+
88
+ return string . Join ( Environment . NewLine , lines ) ;
89
+
90
+ //string[] lines =
91
+ // [
92
+ // "| Instance | Is Valid |",
93
+ // "|:-|:-:|",
94
+ // .. testData.Select(test => $"|`{test!["instance"]!.Print()}`|{test["isValid"]!.Print()}|")
95
+ // ];
96
+
97
+ // return string.Join(Environment.NewLine, lines);
78
98
}
99
+
100
+ private static readonly string [ ] KeysToFormat =
101
+ [
102
+ "instance" ,
103
+ "format"
104
+ ] ;
105
+
106
+ private static string MaybeCode ( JsonNode ? node , string key )
107
+ {
108
+ if ( KeysToFormat . Contains ( key ) ) return $ "`{ node . Print ( ) } `";
109
+
110
+ return node ? . Print ( ) ;
111
+ }
79
112
}
0 commit comments