textToFunctionDart function

String textToFunctionDart({
  1. required String key,
  2. required dynamic value,
  3. required String returnType,
  4. required String className,
  5. bool isClass = false,
  6. bool isList = false,
  7. String? comment,
  8. required void paramFunction(
    1. String text
    ),
  9. required void paramJson(
    1. String text
    ),
})

Implementation

String textToFunctionDart({
  required String key,
  required dynamic value,
  required String returnType,
  required String className,
  bool isClass = false,
  bool isList = false,
  String? comment,
  required void Function(String text) paramFunction,
  required void Function(String text) paramJson,
}) {
  comment ??= "";
  String nameClass = className.camelCaseClass();

  String nameMethod = key
      .replaceAll(RegExp(r"^(@|[0-9]+)", caseSensitive: false), "special_")
      .replaceAll(RegExp(r"([\-]+)", caseSensitive: false), "_")
      .replaceAll(RegExp(r"^(_)", caseSensitive: false), "special_");
  if (RegExp(r"^(do|is|in)$", caseSensitive: false).hasMatch(nameMethod)) {
    nameMethod += "_";
  }

  if (isClass) {
    if (isList) {
      paramFunction.call("""
      List<${nameClass}>? ${nameMethod},
""");

// old
//       paramJson.call("""
//       "${key}": (${nameMethod} != null)? ${nameMethod}.map((res) => res.toJson()).toList().cast<Map>(): null,
// """);

      paramJson.call("""
      "${key}": (${nameMethod} != null)? ${nameMethod}.toJson(): null,
""");

      return """
  ${comment}
  List<${nameClass}> get ${nameMethod} {
    try {
      if (rawData["${key}"] is List == false){
        return [];
      }
      return (rawData["${key}"] as List).map((e) => ${nameClass}(e as ${returnType})).toList().cast<${nameClass}>();
    } catch (e) {
      return [];
    }
  }

""";
    }
    paramFunction.call("""
      ${nameClass}? ${nameMethod},
""");
    paramJson.call("""
      "${key}": (${nameMethod} != null)?${nameMethod}.toJson(): null,
""");
    return """

  ${comment}
  ${nameClass} get ${nameMethod} {
    try {
      if (rawData["${key}"] is ${returnType} == false){
        return ${nameClass}({});
      }
      return ${nameClass}(rawData["${key}"] as ${returnType});
    } catch (e) {
      return ${nameClass}({});
    }
  }

""";
  }
  if (isList) {
    paramFunction.call("""
      List<${returnType}>? ${nameMethod},
""");
    paramJson.call("""
      "${key}": ${nameMethod},
""");
    return """

  ${comment}
  List<${returnType}> get ${nameMethod} {
    try {
      if (rawData["${key}"] is List == false){
        return [];
      }
      return (rawData["${key}"] as List).cast<${returnType}>();
    } catch (e) {
      return [];
    }
  }

""";
  }
  if (RegExp(r"^(special_)", caseSensitive: false).hashData(nameMethod)) {
    paramFunction.call("""
    ${returnType} ${nameMethod} = ${json.encode(value)},
""");
  } else {
    paramFunction.call("""
    ${returnType}? ${nameMethod},
""");
  }
  paramJson.call("""
      "${key}": ${nameMethod},
""");

  return """

  ${comment}
  $returnType? get ${nameMethod} {
    try {
      if (rawData["${key}"] is ${returnType} == false){
        return null;
      }
      return rawData["${key}"] as ${returnType};
    } catch (e) {
      return null;
    }
  }

""";
}