@@ -18,6 +18,81 @@ namespace k8s
18
18
/// </summary>
19
19
20
20
public class Yaml {
21
+ /// <summary>
22
+ /// Load a collection of objects from a stream asynchronously
23
+ /// </summary>
24
+ /// <param name="stream">
25
+ /// The stream to load the objects from.
26
+ /// </param>
27
+ /// <param name="typeMap">
28
+ /// A map from <apiVersion>/<kind> to Type. For example "v1/Pod" -> typeof(V1Pod)
29
+ /// </param>
30
+ public static async Task < List < object > > LoadAllFromStreamAsync ( Stream stream , Dictionary < String , Type > typeMap ) {
31
+ var reader = new StreamReader ( stream ) ;
32
+ var content = await reader . ReadToEndAsync ( ) ;
33
+ return LoadAllFromString ( content , typeMap ) ;
34
+ }
35
+
36
+ /// <summary>
37
+ /// Load a collection of objects from a file asynchronously
38
+ /// </summary>
39
+ /// <param name="fileName">
40
+ /// The name of the file to load from.
41
+ /// </param>
42
+ /// <param name="typeMap">
43
+ /// A map from <apiVersion>/<kind> to Type. For example "v1/Pod" -> typeof(V1Pod)
44
+ /// </param>
45
+
46
+ public static Task < List < object > > LoadAllFromFileAsync ( String fileName , Dictionary < String , Type > typeMap )
47
+ {
48
+ var reader = File . Open ( fileName , FileMode . Open ) ;
49
+ return LoadAllFromStreamAsync ( reader , typeMap ) ;
50
+ }
51
+
52
+ /// <summary>
53
+ /// Load a collection of objects from a string
54
+ /// </summary>
55
+ /// <param name="content">
56
+ /// The string to load the objects from.
57
+ /// </param>
58
+ /// <param name="typeMap">
59
+ /// A map from <apiVersion>/<kind> to Type. For example "v1/Pod" -> typeof(V1Pod)
60
+ /// </param>
61
+
62
+ public static List < object > LoadAllFromString ( String content , Dictionary < String , Type > typeMap ) {
63
+ var deserializer =
64
+ new DeserializerBuilder ( )
65
+ . WithNamingConvention ( new CamelCaseNamingConvention ( ) )
66
+ . WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
67
+ . WithTypeConverter ( new IntOrStringYamlConverter ( ) )
68
+ . IgnoreUnmatchedProperties ( )
69
+ . Build ( ) ;
70
+ var types = new List < Type > ( ) ;
71
+ var parser = new Parser ( new StringReader ( content ) ) ;
72
+ parser . Expect < StreamStart > ( ) ;
73
+ while ( parser . Accept < DocumentStart > ( ) ) {
74
+ var obj = deserializer . Deserialize < KubernetesObject > ( parser ) ;
75
+ types . Add ( typeMap [ obj . ApiVersion + "/" + obj . Kind ] ) ;
76
+ }
77
+
78
+ deserializer =
79
+ new DeserializerBuilder ( )
80
+ . WithNamingConvention ( new CamelCaseNamingConvention ( ) )
81
+ . WithTypeInspector ( ti => new AutoRestTypeInspector ( ti ) )
82
+ . WithTypeConverter ( new IntOrStringYamlConverter ( ) )
83
+ . Build ( ) ;
84
+ parser = new Parser ( new StringReader ( content ) ) ;
85
+ parser . Expect < StreamStart > ( ) ;
86
+ var ix = 0 ;
87
+ var results = new List < object > ( ) ;
88
+ while ( parser . Accept < DocumentStart > ( ) ) {
89
+ var objType = types [ ix ++ ] ;
90
+ var obj = deserializer . Deserialize ( parser , objType ) ;
91
+ results . Add ( obj ) ;
92
+ }
93
+ return results ;
94
+ }
95
+
21
96
public static async Task < T > LoadFromStreamAsync < T > ( Stream stream ) {
22
97
var reader = new StreamReader ( stream ) ;
23
98
var content = await reader . ReadToEndAsync ( ) ;
0 commit comments