encode function Null safety

dynamic encode(
  1. Map<String, Object> typeTable,
  2. String path,
  3. Object type,
  4. Object? value
)

Implementation

dynamic encode(
    Map<String, Object> typeTable, String path, Object type, Object? value) {
  if (type is EnumTypeDescription) {
    if (!type.enumValues.contains(value) || value == null) {
      throw SdkgenTypeException(
          'Invalid Type at \'$path\', expected ${type.type}, got ${jsonEncode(value)}');
    }
    return type.stringValues[type.enumValues.indexOf(value)];
  } else if (type is StructTypeDescription) {
    if (value.runtimeType != type.type) {
      throw SdkgenTypeException(
          'Invalid Type at \'$path\', expected ${type.type}, got ${jsonEncode(value)}');
    }
    var map = Function.apply(type.exportAsMap, [value]) as Map<String, Object?>;
    var resultMap = {};
    map.forEach((fieldName, fieldValue) {
      resultMap[fieldName] = encode(
          typeTable, '$path.$fieldName', type.fields[fieldName]!, fieldValue);
    });
    return resultMap;
  } else if (type is String) {
    if (type.endsWith('?')) {
      if (value == null) {
        return null;
      } else {
        return encode(
            typeTable, path, type.substring(0, type.length - 1), value);
      }
    } else if (type.endsWith('[]')) {
      if (value is! List) {
        throw SdkgenTypeException(
            'Invalid Type at \'$path\', expected ${jsonEncode(type)}, got ${jsonEncode(value)}');
      }
      return value
          .asMap()
          .entries
          .map((entry) => encode(typeTable, '$path[${entry.key}]',
              type.substring(0, type.length - 2), entry.value))
          .toList();
    } else {
      switch (type) {
        case 'bytes':
          if (value is! List<int>) {
            throw SdkgenTypeException(
                'Invalid Type at \'$path\', expected ${jsonEncode(type)}, got ${jsonEncode(value)}');
          }
          return Base64Encoder().convert(value);
        case 'bigint':
          if (value is! BigInt) {
            throw SdkgenTypeException(
                'Invalid Type at \'$path\', expected ${jsonEncode(type)}, got ${jsonEncode(value)}');
          }
          return value.toString();
        case 'date':
          if (value is! DateTime) {
            throw SdkgenTypeException(
                'Invalid Type at \'$path\', expected ${jsonEncode(type)}, got ${jsonEncode(value)}');
          }
          return value.toIso8601String().split('T')[0];
        case 'datetime':
          if (value is! DateTime) {
            throw SdkgenTypeException(
                'Invalid Type at \'$path\', expected ${jsonEncode(type)}, got ${jsonEncode(value)}');
          }
          return value.toUtc().toIso8601String().replaceAll('Z', '');
        default:
          if (simpleTypes.contains(type)) {
            return simpleEncodeDecode(typeTable, path, type, value);
          } else if (typeTable.containsKey(type)) {
            return encode(typeTable, path, typeTable[type]!, value);
          } else {
            throw SdkgenTypeException(
                'Unknown type \'${jsonEncode(type)}\' at \'$path\'');
          }
      }
    }
  } else {
    throw SdkgenTypeException(
        'Unknown type \'${jsonEncode(type)}\' at \'$path\'');
  }
}