Implementation
String jsonToScriptStatic(
Map<String, dynamic> data, {
bool isMethodApi = true,
String? comment,
required Directory directory,
String path_package_json_dart =
"import \"package:general_lib/general_lib.dart\";",
}) {
// String base_name = p.basename(directory.path);
// String baseName = base_name.camelCaseClass();
comment ??= "";
// List<String> classMessages = [];
final String className = data["@type"].toString().camelCaseClass();
final String class_name =
className[0].toLowerCase() + className.substring(1, className.length);
String package_data = "";
String classMessage = """
// ignore_for_file: non_constant_identifier_names, unused_import
${path_package_json_dart}
// import "dart:convert";
{package_data}
${comment}
class ${className} extends JsonScheme {
${comment}
${className}(super.rawData);
/// return default data
///
static Map get defaultData {
return ${json.encode(data)};
}
/// check data
/// if raw data
/// - rawData["@type"] == ${data["@type"]}
/// if same return true
bool json_scheme_utils_checkDataIsSameBySpecialType() {
return rawData["@type"] == defaultData["@type"];
}
/// check value data whatever do yout want
bool json_scheme_utils_checkDataIsSameBuilder({
required bool Function(Map rawData, Map defaultData) onResult,
}) {
return onResult(rawData["@type"], defaultData["@type"]);
}
/// create [${className}]
/// Empty
static ${className} empty() {
return ${className}({});
}
""";
String classDataCreate = """
${comment}
static ${className} create({
bool schemeUtilsIsSetDefaultData = false,
""";
String classDataCreateJson = """
// ${className} ${class_name} = ${className}({
final Map ${class_name}_data_create_json = {
""";
data.forEach((key, value) {
if (value is String) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "String",
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
if (value is double) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "double",
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value is num) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "num",
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value is int) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "int",
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
if (value is bool) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "bool",
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
if (value is Map) {
// package_data += "\nimport \"${className.snakeCaseClass()}.dart\"";
String value_class_name = () {
if (value["@type"] is String && (value["@type"] as String).isNotEmpty) {
return value["@type"];
} else {
return key;
}
}();
package_data += "\nimport \"${value_class_name.snakeCaseClass()}.dart\";";
classMessage += textToFunctionDart(
key: key,
value: value,
// className: className,
className: value_class_name.camelCaseClass(),
returnType: "Map",
isClass: true,
// isUseClassName: isUseClassName,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
// classMessages.add(
// jsonToScript(
// value.cast<String, dynamic>(),
// className: nameClass,
// isMain: false,
// // isUseClassName: isUseClassName,
// comment: comment,
// directory: directory,
// ).first,
// );
return;
}
if (value is List) {
if (value.isNotEmpty) {
if (value.first is List) {
List values = value.first;
if (values.isNotEmpty) {
if (values.first is Map) {
String value_class_name = () {
if (values.first["@type"] is String &&
(values.first["@type"] as String).isNotEmpty) {
return values.first["@type"];
}
return key;
}();
package_data +=
"\nimport \"${value_class_name.snakeCaseClass()}.dart\";";
classMessage += textToListFunction(
key: key,
className: value_class_name.camelCaseClass(),
returnType: "Map",
isClass: true,
// isUseClassName: isUseClassName,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first is bool) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "bool",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first is String) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "String",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first is double) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "double",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first is num) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "num",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first is int) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "int",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (values.first == null) {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
} else {
classMessage += textToListFunction(
key: key,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
return;
}
if (value.first is Map) {
// package_data += "${className.snakeCaseClass()}.dart";
String value_class_name = () {
if (value.first["@type"] is String &&
(value.first["@type"] as String).isNotEmpty) {
return value.first["@type"];
}
return key;
}();
package_data +=
"\nimport \"${value_class_name.snakeCaseClass()}.dart\";";
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: value_class_name.camelCaseClass(),
returnType: "Map",
isClass: true,
// isUseClassName: isUseClassName,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
// classMessages.add(
// jsonToScript(
// (value.first as Map).cast<String, dynamic>(),
// className: nameClass,
// isMain: false,
// // isUseClassName: isUseClassName,
// comment: comment,
// directory: directory,
// ).first,
// );
} else if (value.first is bool) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "bool",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value.first is String) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "String",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value.first is double) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "double",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value.first is num) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "num",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value.first is int) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "int",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else if (value.first == null) {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
} else {
classMessage += textToFunctionDart(
key: key,
value: value.first,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
} else {
classMessage += textToFunctionDart(
key: key,
value: "",
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
return;
}
if (value == null) {
classMessage += textToFunctionDart(
key: key,
value: value,
className: className,
returnType: "Object",
isClass: false,
isList: false,
comment: comment,
paramFunction: (text) {
classDataCreate += text;
},
paramJson: (text) {
classDataCreateJson += text;
},
);
}
});
classDataCreateJson += "\n\n};";
classDataCreate += "}) {";
final String json_name_create = "${class_name}_data_create_json";
classDataCreate += """
${classDataCreateJson}
${json_name_create}.removeWhere((key, value) => value == null);
if (schemeUtilsIsSetDefaultData) {
defaultData.forEach((key, value) {
if (${json_name_create}.containsKey(key) == false) {
${json_name_create}[key] = value;
}
});
}
return ${className}(${class_name}_data_create_json);
""";
classDataCreate += "\n\n }";
classMessage += classDataCreate;
classMessage += """
}""";
// classMessage += "\n\n${classMessages.join("\n\n")}";/
classMessage = classMessage.replaceAll(
RegExp(r"{package_data}", caseSensitive: false),
package_data.split("\n").toSet().toList().join("\n"));
return classMessage;
}