Last active
August 29, 2015 14:09
-
-
Save robfe/32ba60bc997deef9f74f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Trait("Category", "Spike")] | |
public class OomBuilder | |
{ | |
const string StaticConverter = @" | |
public static {{To}} To{{To}}({{From}} src) | |
{ | |
return new {{To}} | |
{ | |
{{#each Matches}} | |
{{this}} = src.{{this}},{{/each}} | |
{{#each TargetMissing}} | |
//{{this}} = ??,{{/each}} | |
{{#each SrcMissing}} | |
//?? = {{this}},{{/each}} | |
} | |
}"; | |
const string Constructor = @" | |
public {{To}}({{From}} src) | |
{ | |
{{#each Matches}} | |
{{this}} = src.{{this}};{{/each}} | |
{{#each TargetMissing}} | |
//{{this}} = ??;{{/each}} | |
{{#each SrcMissing}} | |
//?? = {{this}};{{/each}} | |
}"; | |
const string FromConverter = @" | |
public {{To}} To{{To}}() | |
{ | |
return new {{To}} | |
{ | |
{{#each Matches}} | |
{{this}} = {{this}},{{/each}} | |
{{#each TargetMissing}} | |
//{{this}} = ??,{{/each}} | |
{{#each SrcMissing}} | |
//?? = {{this}},{{/each}} | |
} | |
}"; | |
const string Cloner = @" | |
public {{To}} Clone() | |
{ | |
return new {{To}} | |
{ | |
{{#each Matches}} | |
{{this}} = {{this}},{{/each}} | |
{{#each TargetMissing}} | |
//{{this}} = ??,{{/each}} | |
{{#each SrcMissing}} | |
//?? = {{this}},{{/each}} | |
} | |
}"; | |
const string VmView = @" | |
public static {{To}} From{{From}}({{From}} source) | |
{ | |
return new {{To}} | |
{ | |
{{#each Matches}} | |
{{this}} = source.{{this}},{{/each}} | |
}; | |
} | |
public void ApplyTo{{From}}({{From}} target) | |
{ | |
{{#each Matches}} | |
target.{{this}} = {{this}};{{/each}} | |
}"; | |
[Fact] | |
public void PrintMapping() | |
{ | |
//tweak these to get what you want: | |
var from = typeof(X); | |
var to = typeof(Y); | |
const string template = VmView; | |
var toProps = to.GetProperties().Select(x => x.Name).ToList(); | |
var fromProps = from.GetProperties().Select(x => x.Name).ToList(); | |
var data = new | |
{ | |
From=from.Name, | |
To = to.Name, | |
Matches = toProps.Intersect(fromProps), | |
TargetMissing = toProps.Except(fromProps), | |
SrcMissing = fromProps.Except(toProps), | |
}; | |
Console.Out.WriteLine(Mustache(template, data)); | |
} | |
string Mustache(string template, object data) | |
{ | |
return new FormatCompiler().Compile(template).Render(data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment