fromYaml static method

Config fromYaml({
  1. required String filePath,
})

Implementation

static Config fromYaml({required String filePath}) {
  // ファイルの存在可否
  final yamlFile = File(filePath);
  if (!yamlFile.existsSync()) {
    throw Exception('File not found: $filePath');
  }
  final yamlContent = yamlFile.readAsStringSync();
  return checkedYamlDecode<Config>(
    yamlContent,
    (Map? json) {
      // allowNullをfalseにすればjsonがnullの場合内部で例外が発生するが、クロージャーでは変わらずoptionalなのでこちらで処理しておく。
      if (json == null) {
        throw Exception('Yaml file is empty: $filePath');
      }
      if (json[Constants.packageName] == null) {
        throw Exception('${Constants.packageName} is not found: $filePath');
      }
      return Config.fromJson(json[Constants.packageName]);
    },
    allowNull: true,
  );
}