defaultCommandCallback property

Callback defaultCommandCallback
getter/setter pair

Implementation

static Callback defaultCommandCallback = (DartDeclaration self, dynamic testSubject, {required String key, dynamic value}) {
  self.isNullable = testSubject.endsWith('?');

  self.setName(key);

  if (value == null) {
    self.type = 'dynamic';
    return self;
  }

  if (value is Map) {
    self.type = key.toTitleCase();
    self.nestedClasses.add(JsonModel.fromMap(key, value as Map<String, dynamic>));
    return self;
  }

  if (value is List && value.isNotEmpty) {
    final firstListValue = value.first;
    if (firstListValue is List) {
      final nestedFirst = firstListValue.first;
      if (nestedFirst is Map) {
        final key = nestedFirst['\$key'];
        nestedFirst.remove('\$key');
        self.type = 'List<List<$key>>';
        self.nestedClasses.add(JsonModel.fromMap(key, nestedFirst as Map<String, dynamic>));
      }
    } else if (firstListValue is Map) {
      final key = firstListValue['\$key'];
      firstListValue.remove('\$key');
      self.type = 'List<$key>';
      self.nestedClasses.add(JsonModel.fromMap(key, firstListValue as Map<String, dynamic>));
    } else {
      final listValueType = firstListValue.runtimeType.toString();
      self.type = 'List<$listValueType>';
    }
    return self;
  }

  var newDeclaration = DartDeclaration.fromCommand(
    valueCommands,
    self,
    testSubject: value,
    key: key,
    value: value,
  );

  self.type = newDeclaration.type ?? value.runtimeType.toString();

  return self;
};