Improve configData.merge, 'create' fix

This commit is contained in:
Joshua Granick
2014-10-20 10:48:39 -07:00
parent 8b56b03641
commit 5ee3082a39
3 changed files with 79 additions and 42 deletions

View File

@@ -20,44 +20,6 @@ import haxe.ds.StringMap;
}
public static function copyFieldsPreferObjectOverValue<T> (source:T, destination:T):T {
for (field in Reflect.fields (source)) {
var do_copy = true;
var exists = Reflect.hasField( destination, field );
if( exists ) {
var value_source = Reflect.field (source, field);
var value_dest = Reflect.field (destination, field);
var type_source = Type.typeof(value_source).getName();
var type_dest = Type.typeof(value_dest).getName();
// if(LogHelper.verbose) {
// LogHelper.println(field + " / existed in dest as " + type_dest + " / " + type_source );
// }
//if trying to copy a non object over an object, don't
if(type_source != "TObject" && type_dest == "TObject") {
do_copy = false;
if(LogHelper.verbose) {
LogHelper.println(field + " not merged by preference" );
}
}
}
if(do_copy) {
Reflect.setField (destination, field, Reflect.field (source, field));
}
}
return destination;
}
public static function copyMissingFields<T> (source:T, destination:T):T {

View File

@@ -268,7 +268,65 @@ abstract ConfigData(Dynamic) to Dynamic from Dynamic {
if (other != null) {
ObjectHelper.copyFieldsPreferObjectOverValue (other, this);
mergeValues (other, this);
}
}
private function mergeValues<T> (source:T, destination:T):Void {
for (field in Reflect.fields (source)) {
var doCopy = true;
var exists = Reflect.hasField (destination, field);
var typeDest = null;
if (exists) {
var valueSource = Reflect.field (source, field);
var valueDest = Reflect.field (destination, field);
var typeSource = Type.typeof(valueSource).getName ();
typeDest = Type.typeof (valueDest).getName ();
//if trying to copy a non object over an object, don't
if (typeSource != "TObject" && typeDest == "TObject") {
doCopy = false;
//if (LogHelper.verbose) {
//
//LogHelper.println (field + " not merged by preference");
//
//}
}
if (StringTools.endsWith (field, "___array")) {
doCopy = false;
var array:Array<Dynamic> = cast Reflect.field (destination, field);
array = array.concat (cast Reflect.field (source, field));
Reflect.setField (destination, field, array);
}
}
if (doCopy) {
if (typeDest == "TObject") {
mergeValues (Reflect.field (source, field), Reflect.field (destination, field));
} else {
Reflect.setField (destination, field, Reflect.field (source, field));
}
}
}

View File

@@ -83,10 +83,15 @@ class CreateTemplate {
var sampleName = null;
var outputName = "SampleProject";
if (colonIndex == -1 && words.length > 1) {
if (colonIndex == -1) {
projectName = words[0];
sampleName = words[1];
if (words.length > 1) {
sampleName = words[1];
}
if (words.length > 2) {
@@ -107,13 +112,25 @@ class CreateTemplate {
}
if (projectName == "project") {
projectName = "lime";
if (sampleName != null) {
outputName = sampleName;
}
}
if (projectName == null || projectName == "") {
projectName = "lime";
}
if (projectName != null && projectName != "" && sampleName == "project") {
if (projectName != null && projectName != "") {
var defines = new Map <String, Dynamic> ();
defines.set ("create", 1);