Repository navigation
Expand file tree
/
Copy pathMapNode.cs
More file actions
141 lines (118 loc) · 5.3 KB
/
Copy pathMapNode.cs
File metadata and controls
141 lines (118 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
namespace Microsoft.OpenApi.Readers
{
using SharpYaml.Schemas;
using SharpYaml.Serialization;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
/// <summary>
/// Abstraction of a Map to isolate semantic parsing from details of
/// </summary>
public class MapNode : ParseNode, IEnumerable<PropertyNode>
{
YamlMappingNode node;
private List<PropertyNode> nodes;
public static MapNode Create(string yaml)
{
var parsingContent = new ParsingContext();
return new MapNode(parsingContent, (YamlMappingNode)YamlHelper.ParseYaml(yaml));
}
public MapNode(ParsingContext ctx, YamlMappingNode node) : base(ctx)
{
if (node == null) throw new OpenApiException($"Expected map");
this.node = node;
nodes = this.node.Children.Select(kvp => new PropertyNode(Context, kvp.Key.GetScalarValue(), kvp.Value)).Cast<PropertyNode>().ToList();
}
public IEnumerator<PropertyNode> GetEnumerator()
{
return this.nodes.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.nodes.GetEnumerator();
}
public PropertyNode this[string key]
{
get
{
YamlNode node = null;
if (this.node.Children.TryGetValue(new YamlScalarNode(key), out node))
{
return new PropertyNode(this.Context, key, this.node.Children[new YamlScalarNode(key)]);
} else
{
return null;
}
}
}
public override string GetRaw()
{
var x = new Serializer(new SerializerSettings(new JsonSchema()) { EmitJsonComptible = true } );
return x.Serialize(this.node);
}
public string GetScalarValue(ValueNode key)
{
var scalarNode = this.node.Children[new YamlScalarNode(key.GetScalarValue())] as YamlScalarNode;
if (scalarNode == null) throw new OpenApiException($"Expected scalar at line {this.node.Start.Line} for key {key.GetScalarValue()}");
return scalarNode.Value;
}
public T GetReferencedObject<T>(string refPointer) where T : IReference
{
return (T)this.Context.GetReferencedObject(refPointer);
}
public T CreateOrReferenceDomainObject<T>(Func<T> factory) where T: IReference
{
T domainObject;
var refPointer = GetReferencePointer(); // What should the DOM of a reference look like?
// Duplicated object - poor perf/more memory/unsynchronized changes
// Intermediate object - require common base class/client code has to explicitly code for it.
// Delegating object - lot of setup work/maintenance/ require full interfaces
// **current favourite***Shared object - marker to indicate its a reference/serialization code must serialize as reference everywhere except components.
if (refPointer != null)
{
domainObject = (T)this.Context.GetReferencedObject(refPointer);
}
else
{
domainObject = factory();
}
return domainObject;
}
public override Dictionary<string, T> CreateMap<T>(Func<MapNode, T> map)
{
var yamlMap = this.node;
if (yamlMap == null) throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
var nodes = yamlMap.Select(n => new { key = n.Key.GetScalarValue(), value = map(new MapNode(this.Context,n.Value as YamlMappingNode)) });
return nodes.ToDictionary(k => k.key, v => v.value);
}
public override Dictionary<string, T> CreateMapWithReference<T>(string refpointerbase, Func<MapNode, T> map)
{
var yamlMap = this.node;
if (yamlMap == null) throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
var nodes = yamlMap.Select(n => new { key = n.Key.GetScalarValue(),
value = this.GetReferencedObject<T>(refpointerbase + n.Key.GetScalarValue())
?? map(new MapNode(this.Context, (YamlMappingNode)n.Value))
});
return nodes.ToDictionary(k => k.key, v => v.value);
}
public override Dictionary<string, T> CreateSimpleMap<T>(Func<ValueNode, T> map)
{
var yamlMap = this.node;
if (yamlMap == null) throw new OpenApiException($"Expected map at line {yamlMap.Start.Line} while parsing {typeof(T).Name}");
var nodes = yamlMap.Select(n => new { key = n.Key.GetScalarValue(), value = map(new ValueNode(this.Context, (YamlScalarNode)n.Value)) });
return nodes.ToDictionary(k => k.key, v => v.value);
}
public string GetReferencePointer()
{
YamlNode refNode;
if (!this.node.Children.TryGetValue(new YamlScalarNode("$ref"), out refNode))
{
return null;
}
return refNode.GetScalarValue();
}
}
}