jsonToScriptStatic function

String jsonToScriptStatic(
  1. Map<String, dynamic> data, {
  2. bool isMethodApi = true,
  3. String? comment,
  4. required Directory directory,
  5. String path_package_json_dart = "import \"json_dart.dart\";",
})

Implementation

String jsonToScriptStatic(
  Map<String, dynamic> data, {
  bool isMethodApi = true,
  String? comment,
  required Directory directory,
  String path_package_json_dart = "import \"json_dart.dart\";",
}) {
  String base_name = p.basename(directory.path);
  String baseName = base_name.camelCaseClass();
  comment ??= "";
  // List<String> classMessages = [];
  String className = data["@type"].toString().camelCaseClass();
  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 Json${baseName} {

  ${comment}
  ${className}(super.rawData);

  static Map get defaultData {
    return ${json.encode(data)};
  }
""";

  String classDataCreate = """

  ${comment}
  static ${className} create({

""";
  String classDataCreateJson = """

    // ${className} ${className[0].toLowerCase() + className.substring(1, className.length)} = ${className}({
Map ${className[0].toLowerCase() + className.substring(1, className.length)}_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 int) {
      classMessage += textToFunctionDart(
        key: key,
        value: value,
        className: className,
        returnType: "int",
        comment: comment,
        paramFunction: (text) {
          classDataCreate += text;
        },
        paramJson: (text) {
          classDataCreateJson += text;
        },
      );
    } else 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;
        },
      );
    }
    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 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 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 == 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 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 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 == 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 += "})  {";
  classDataCreate += """
${classDataCreateJson}


          ${className[0].toLowerCase() + className.substring(1, className.length)}_data_create_json.removeWhere((key, value) => value == null);
${className} ${className[0].toLowerCase() + className.substring(1, className.length)}_data_create = ${className}(${className[0].toLowerCase() + className.substring(1, className.length)}_data_create_json);

return ${className[0].toLowerCase() + className.substring(1, className.length)}_data_create;

""";

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