parseFile static method

SpyYamlModel? parseFile(
  1. String filePath
)

Parse a single .spy.yaml file

Implementation

static SpyYamlModel? parseFile(String filePath) {
  final file = File(filePath);
  if (!file.existsSync()) return null;

  final content = file.readAsStringSync();
  final yaml = loadYaml(content) as YamlMap?;

  if (yaml == null) return null;

  final className = yaml['class'] as String?;
  if (className == null || className.isEmpty) return null;

  final fields = <SpyYamlField>[];

  final fieldsMap = yaml['fields'] as YamlMap?;
  if (fieldsMap != null) {
    fieldsMap.forEach((key, value) {
      final fieldName = key as String;
      final fieldType = value as String;
      final isOptional = fieldType.endsWith('?');

      fields.add(SpyYamlField(
        name: fieldName,
        type: isOptional ? fieldType.replaceAll('?', '') : fieldType,
        isOptional: isOptional,
      ));
    });
  }

  return SpyYamlModel(
    className: className,
    fields: fields,
    filePath: filePath,
  );
}