textToListFunction function

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

Implementation

String textToListFunction({
  required String key,
  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<List<${nameClass}>>? ${nameMethod},
""");
      paramJson.call("""
      "${key}": (${nameMethod} != null)? ${nameMethod}.map((res) => res.map((e) => e.toJson()).toList().cast<Map>()).toList().cast<List<Map>>(): null,
""");
      return """
  ${comment}
List<List<${nameClass}>> get ${nameMethod} {
    try {
      if (rawData["${key}"] is List == false){
        return [];
      }
      return (rawData["inline_keyboard"] as List<List>).map((e) => e.map((e) => ${nameClass}(e as Map)).toList().cast<${nameClass}>()).toList().cast<List<${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<List<${returnType}>>? ${nameMethod},
""");
    paramJson.call("""
      "${key}": ${nameMethod},
""");
    return """

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

""";
  }
  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;
    }
  }

""";
}