defaultCommandCallback function

DartDeclaration defaultCommandCallback(
  1. DartDeclaration self,
  2. dynamic testSubject, {
  3. required String key,
  4. dynamic value,
})

Implementation

DartDeclaration defaultCommandCallback(
  DartDeclaration self,
  dynamic testSubject, {
  required String key,
  dynamic value,
}) {
  self.isNullable = testSubject.toString().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 childKey = (nestedFirst['\$key'] as String? ?? singular.convert(key)).toTitleCase();

        nestedFirst.remove('\$key');
        self.type = 'List<List<$childKey>>';
        self.nestedClasses.add(JsonModel.fromMap(childKey, nestedFirst as Map<String, dynamic>));
      }
    } else if (firstListValue is Map) {
      final childKey = (firstListValue['\$key'] as String? ?? singular.convert(key)).toTitleCase();

      firstListValue.remove('\$key');
      self.type = 'List<$childKey>';
      self.nestedClasses.add(JsonModel.fromMap(childKey, firstListValue as Map<String, dynamic>));
    } else {
      final listValueType = firstListValue.runtimeType.toString();
      self.type = 'List<$listValueType>';
    }
    return self;
  }

  var v = value;
  if (value is String && value.contains('|')) {
    self.fakerDeclaration = value.split('|')[1];
    v = value.split('|')[0];

    bool parsed = false;
    try {
      v = int.parse(v.toString());
      parsed = true;
    } on FormatException catch (_) {}

    if (!parsed && v == 'true') {
      v = true;
      parsed = true;
    }
    if (!parsed && v == 'false') {
      v = false;
      parsed = true;
    }
  }

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

  self.jsonValue = v;
  self.type = newDeclaration.type ?? v.runtimeType.toString();

  return self;
}