jsonToScripts function

Future<Directory> jsonToScripts(
  1. List<Map> scripts_origin, {
  2. String? comment,
  3. bool isMethodApi = true,
  4. required Directory directory,
  5. String path_package_json_dart = "import \"json_dart.dart\";",
  6. int try_loop = 2,
})

Implementation

Future<Directory> jsonToScripts(
  List<Map> scripts_origin, {
  String? comment,
  bool isMethodApi = true,
  required Directory directory,
  String path_package_json_dart = "import \"json_dart.dart\";",
  int try_loop = 2,
}) async {
  String base_name = p.basename(directory.path);
  String baseName = base_name.camelCaseClass();
  List<Map> scripts = [...scripts_origin];
  if (!directory.existsSync()) {
    await directory.create(recursive: true);
  }

  for (var loop_index_first = 0;
      loop_index_first < try_loop;
      loop_index_first++) {
    // print(loop_index_first);
    void json_script_update({
      required Map newData,
      required String keyData,
    }) {
      if (newData["@type"] is String == false) {
        newData["@type"] = keyData;
      }
      for (var i = 0; i < scripts.length; i++) {
        Map data = scripts[i];
        if (newData["@type"] == data["@type"]) {
          newData.forEach((key, value) {
            if (value is Map) {
              if (value["@type"] is String == false) {
                scripts[i][key] = {
                  "@type": key,
                  ...value,
                };
                json_script_update(
                  newData: scripts[i][key],
                  keyData: keyData,
                );
              } else {
                json_script_update(
                  newData: value,
                  keyData: keyData,
                );
              }
            } else if (value is List) {
              void json_list_update({
                required List values,
                required String key,
              }) {
                for (var index = 0; index < values.length; index++) {
                  dynamic value_data = values[index];
                  if (value_data is Map) {
                    if (value_data["@type"] is String == false) {
                      if (scripts[i][key][index] is Map) {
                        scripts[i][key][index] = {
                          "@type": key,
                          ...value_data,
                        };
                        json_script_update(
                          newData: scripts[i][key][index],
                          keyData: keyData,
                        );
                      }
                    } else {
                      json_script_update(
                        newData: value_data,
                        keyData: keyData,
                      );
                    }
                  } else if (value_data is List) {
                    json_list_update(values: value_data, key: key);
                  }
                }
              }

              json_list_update(values: value, key: key);
            } else {
              data[key] = value;
            }
          });
          return;
        }
      }
      if (newData["@type"] is String) {
        scripts.add(newData);
        json_script_update(
          newData: newData,
          keyData: keyData,
        );
      } else {
        // print(newData);
      }
    }

    void json_list_update({required List values, required String key}) {
      for (var index = 0; index < values.length; index++) {
        dynamic value_data = values[index];
        if (value_data is Map) {
          json_script_update(newData: value_data, keyData: key);
        } else if (value_data is List) {
          json_list_update(values: value_data, key: key);
        }
      }
    }

    for (var i = 0; i < scripts.length; i++) {
      Map data = scripts[i];
      if (data["@type"] is String == false) {
        continue;
      }

      data.forEach((key, value) {
        if (value is Map) {
          if (value["@type"] is String == false) {
            scripts[i][key] = {"@type": key, ...value};
            json_script_update(
              newData: scripts[i][key],
              keyData: key,
            );
          } else {
            json_script_update(
              newData: value.clone(),
              keyData: key,
            );
          }
        } else if (value is List) {
          json_list_update(
            values: value.clone(),
            key: key,
          );
        } else {}
      });
    }
  }

  List<String> datas = [
    """
export "json_dart.dart";
"""
  ];

  for (var i = 0; i < scripts.length; i++) {
    Map data = scripts[i];
    if (data["@type"] is String == false) {
      continue;
    }
    datas.add("export \"${data["@type"].toString().snakeCaseClass()}.dart\";");
    var res = jsonToScriptStatic(
      data.cast<String, dynamic>(),
      directory: directory,
      isMethodApi: isMethodApi,
      comment: comment,
      path_package_json_dart: path_package_json_dart,
    );
    await File(p.join(directory.path,
            "${data["@type"].toString().snakeCaseClass()}.dart"))
        .writeAsString(res);
  }
  String json_dart = """
// ignore_for_file: non_constant_identifier_names, unused_import
import 'dart:convert';

/// return original data json
class Json${baseName} {

  /// return original data json
  Map rawData;

  /// return original data json
  Json${baseName}(this.rawData);

  /// return original data json
  static Map get defaultData {
    return {
      "@type": "jsonDart",
    };
  }

  /// return original data json
  static Json${baseName} create({
    String special_type = ${json.encode("Json${baseName}")},
  }) {
    Map jsonCreate = {
      "@type": special_type,
    };

    jsonCreate.forEach((key, value) {
      try {
        if (value == null) {
          jsonCreate.remove(key);
          // jsonCreate[key] = value;
        }
      } catch (e, stack) {
        print("Azka \${e.toString()}, \${stack.toString()}");
      }
    });
    return Json${baseName}(jsonCreate);
  }

  /// operator map data
  operator *(value) {}

  /// operator map data
  operator /(value) {}

  /// operator map data
  Map operator +(Map value) {
    rawData.addAll(value);
    return rawData;
  }

  /// return original data json
  Map operator -(List values) {
    utils_remove_by_keys_void(values);
    return rawData;
  }

  /// operator map data
  operator [](key) {
    return rawData[key];
  }

  /// operator map data
  void operator []=(key, value) {
    rawData[key] = value;
  }

  /// return original data json
  void utils_remove_values_null_void() {
    rawData.forEach((key, value) {
      if (value == null) {
        rawData.remove(key);
      }
    });
  }

  /// return original data json
  Map utils_remove_values_null() {
    utils_remove_values_null_void();
    return rawData;
  }

  /// return original data json
  void utils_remove_by_values_void(List values) {
    rawData.forEach((key, value) {
      for (var element in values) {
        if (value == element) {
          rawData.remove(key);
        }
      }
    });
  }

  /// return original data json
  Map utils_remove_by_values(List values) {
    utils_remove_by_values_void(values);
    return rawData;
  }

  void utils_remove_by_keys_void(List keys) {
    for (var element in keys) {
      rawData.remove(element);
    }
  }

  /// return original data json
  Map utils_remove_by_keys(List keys) {
    utils_remove_by_keys_void(keys);
    return rawData;
  }

  /// return original data json
  Map utils_filter_by_keys(List keys) {
    Map jsonData = {};
    for (var key in keys) {
      jsonData[key] = rawData[key];
    }
    return jsonData;
  }

  /// return original data json
  Map toMap() {
    return rawData;
  }

  /// return original data json
  Map toJson() {
    return rawData;
  }

  /// 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(rawData);
  }
}

extension Json${baseName}Extensions on List<Json${baseName}> {
  List<Map> toJson() {
    return map((e) => e.toJson()).toList().cast<Map>();
  }

  /// return string data encode json original data
  String toStringPretty() {
    return JsonEncoder.withIndent(" " * 2).convert(toJson());
  }
}

""";
  datas.sort();
  await File(p.join(directory.path, "json_dart.dart")).writeAsString(json_dart);
  await File(p.join(directory.path, "${p.basename(directory.path)}.dart"))
      .writeAsString(datas.toSet().toList().join("\n"));

  return directory;
}