parseFields function

List<FieldSpec> parseFields(
  1. String? raw
)

Parses a --fields value like "title:String, done:bool, age:int?" into a list of FieldSpec. Whitespace is tolerated; a missing type defaults to String. Returns an empty list for null/blank input.

Implementation

List<FieldSpec> parseFields(String? raw) {
  if (raw == null || raw.trim().isEmpty) return const [];
  final fields = <FieldSpec>[];
  for (final part in raw.split(',')) {
    final token = part.trim();
    if (token.isEmpty) continue;
    final colon = token.indexOf(':');
    final rawName = colon == -1 ? token : token.substring(0, colon);
    var type = colon == -1 ? 'String' : token.substring(colon + 1).trim();
    if (type.isEmpty) type = 'String';

    var nullable = false;
    if (type.endsWith('?')) {
      nullable = true;
      type = type.substring(0, type.length - 1).trim();
    }
    final name = Naming.camel(rawName);
    if (name.isEmpty) continue;
    fields.add(FieldSpec(name, type, nullable));
  }
  return fields;
}