-
Notifications
You must be signed in to change notification settings - Fork 1
/
Mapper.cs
57 lines (50 loc) · 1.8 KB
/
Mapper.cs
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
namespace JSuite.Mapping.Parser
{
using System.Collections.Generic;
using System.Linq;
using JSuite.Mapping.Parser.Executing;
using JSuite.Mapping.Parser.Parsing;
using JSuite.Mapping.Parser.Parsing.Generic;
using JSuite.Mapping.Parser.Tokenizing;
using JSuite.Mapping.Parser.Tokenizing.Generic;
using Newtonsoft.Json.Linq;
public class Mapper
{
private readonly IList<MappingExecutor> mappings;
public Mapper(string script)
{
this.mappings = ParseScript(script).Select(o => new MappingExecutor(o)).ToList();
}
public void Map(JObject target, JObject source)
{
foreach (var mapping in this.mappings)
mapping.Execute(target, source);
}
private static IEnumerable<IParseTree<TokenType, ParserRuleType>> ParseScript(string script)
{
var translator = new TextIndexHelper(script);
return MappingTokenizer
.Tokenize(script)
.ApplyModifications()
.ToStatements()
.ApplyPartials()
.Parse(translator)
.Validate(translator);
}
private class MappingExecutor
{
private readonly ExtractFromSource extract;
private readonly UpdateTarget update;
public MappingExecutor(IParseTree<TokenType, ParserRuleType> mappingDefinition)
{
this.extract = mappingDefinition.SourceExtractor();
this.update = mappingDefinition.TargetUpdater();
}
public void Execute(JObject target, JObject source)
{
foreach (var extracted in this.extract(source))
this.update(target, extracted);
}
}
}
}