parseModelField function

ModelField parseModelField(
  1. String input
)

Implementation

ModelField parseModelField(String input) {
  final trimmed = input.trim();
  if (trimmed.isEmpty) {
    throw FormatException('Field definition cannot be empty.');
  }

  final colonIndex = trimmed.indexOf(':');
  if (colonIndex <= 0 || colonIndex == trimmed.length - 1) {
    throw FormatException(
      'Invalid field format "$input". Expected "name:type".',
    );
  }

  final name = trimmed.substring(0, colonIndex).trim();
  final type = trimmed.substring(colonIndex + 1).trim();

  if (name.isEmpty || !RegExp(r'^[a-z][a-zA-Z0-9_]*$').hasMatch(name)) {
    throw FormatException(
      'Invalid field name "$name". Use lowerCamelCase starting with a letter.',
    );
  }

  final isNullable = type.endsWith('?');
  final rawType = isNullable ? type.substring(0, type.length - 1) : type;

  final parsed = _parseType(rawType);
  final dartType = isNullable ? '${parsed.dartType}?' : parsed.dartType;

  return ModelField(
    name: name,
    dartType: dartType,
    category: parsed.category,
    importPath: parsed.importPath,
    innerType: parsed.innerType,
  );
}