Implementation
String jsonToIsarDynamic(
Map<String, dynamic> data, {
String className = "Root",
bool isMain = true,
bool isUseClassName = false,
String? comment,
}) {
comment ??= "";
List<String> classMessages = [];
className = className.camelCaseClass();
String classMessage = """
${((isMain) ? """
// ignore_for_file: non_constant_identifier_names
import 'dart:convert';
import 'package:isar/isar.dart';
part "${className.snakeCaseClass().toLowerCase()}.g.dart";
@collection""" : "@embedded")}
class ${className} {
// Id id = Isar.autoIncrement;
${() {
if (isMain) {
return "Id isar_data_id = Isar.autoIncrement;";
// if (!data.containsKey("id")) {
// return "Id id = Isar.autoIncrement;";
// }
}
return "";
}.call()}
""";
// ${() {
// if (isMain) {
// if (!data.containsKey("id")) {
// return "Id id = Isar.autoIncrement;";
// }
// }
// return "";
// }.call()}
String classDataCreate = """
${comment}
static ${className} create({
bool utils_is_print_data = false,
""";
// String classDataCreateData = "";
String dataToJson = """{
""";
String operator_data = "";
data.forEach((key, value) {
String nameClass = key.camelCaseClass();
if (isUseClassName) {
nameClass = "${className}${key.camelCaseClass()}";
}
if (value is String) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "String",
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value is int) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "int",
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
} else if (value is double) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "double",
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
} else if (value is num) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "num",
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value is bool) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "bool",
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value is Map) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "Map",
isClass: true,
isUseClassName: isUseClassName,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
classMessages.add(jsonToIsar(
value.cast<String, dynamic>(),
className: nameClass,
isMain: false,
isUseClassName: isUseClassName,
comment: comment,
).first);
}
if (value is List) {
if (value.isNotEmpty) {
if (value.first is Map) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "Map",
isClass: true,
isUseClassName: isUseClassName,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
classMessages.add(
jsonToIsar(
(value.first as Map).cast<String, dynamic>(),
className: nameClass,
isMain: false,
isUseClassName: isUseClassName,
comment: comment,
).first,
);
}
if (value.first is bool) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "bool",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value.first is String) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "String",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value.first is int) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "int",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
} else if (value.first is double) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "double",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
} else if (value.first is num) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "num",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
if (value.first == null) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
} else {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "Object",
isClass: false,
isList: true,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
}
if (value == null) {
classMessage += textToFunctionIsar(
key: key,
value: value,
className: className,
returnType: "Object",
isClass: false,
isList: false,
comment: comment,
callback: (text) {},
paramFunction: (text) {},
paramJson: (text) {
dataToJson += text;
},
operatorData: (text) {
operator_data += text;
},
);
}
});
//
dataToJson += "\n\n }";
classDataCreate += "\n\n}) {";
classDataCreate += """
${className} ${className[0].toLowerCase() + className.substring(1, className.length)}_data_create = ${className}();
if (utils_is_print_data){
// print(${className[0].toLowerCase() + className.substring(1, className.length)}_data_create.toStringPretty());
}
return ${className[0].toLowerCase() + className.substring(1, className.length)}_data_create;
""";
classDataCreate += "\n\n}";
classMessage += """
/// operator map data
operator [](key) {
return toJson()[key];
}
/// operator map data
void operator []=(key, value) {
${operator_data}
}
/// return original data json
Map utils_remove_values_null() {
Map rawData = toJson();
rawData.forEach((key, value) {
if (value == null) {
rawData.remove(key);
}
});
return rawData;
}
/// return original data json
Map utils_remove_by_values(List values) {
Map rawData = toJson();
rawData.forEach((key, value) {
for (var element in values) {
if (value == element) {
rawData.remove(key);
}
}
});
return rawData;
}
/// return original data json
Map utils_remove_by_keys(List keys) {
Map rawData = toJson();
for (var element in keys) {
rawData.remove(element);
}
return rawData;
}
/// return original data json
Map utils_filter_by_keys(List keys) {
Map rawData = toJson();
Map jsonData = {};
for (var key in keys) {
jsonData[key] = rawData[key];
}
return jsonData;
}
/// return original data json
Map toMap() {
return toJson();
}
/// return original data json
Map toJson() {
return ${dataToJson};
}
/// return string data encode json original data
String toStringPretty() {
return JsonEncoder.withIndent(" " * 2).convert(toJson());
}
/// return string data encode json original data
@override
String toString() {
return json.encode(toJson());
}
/// return original data json
static Map get defaultData {
return ${json.encode(data)};
}
${classDataCreate}
}""";
classMessage += "\n\n${classMessages.join("\n\n")}";
return classMessage;
}