inputsFromElement function

Future<List<String>> inputsFromElement(
  1. GeneratorCtx ctx,
  2. ExecutableElement element
)

Implementation

Future<List<String>> inputsFromElement(
  GeneratorCtx ctx,
  ExecutableElement element,
) async {
  final _dartEmitter = DartEmitter();
  final inputMaybe =
      await Future.wait<String?>(element.parameters.map((e) async {
    if (isReqCtx(e.type)) {
      return null;
    } else {
      final argInfo = argInfoFromElement(e);
      final type = inferType(
        ctx.config.customTypes,
        e,
        e.name,
        e.type,
        nullable: argInfo.inline,
        isInput: true,
      ).accept(_dartEmitter).toString();

      if (argInfo.inline) {
        // TODO: 2G Check e.type is InputType?
        return '...$type.fields';
      } else {
        final defaultValueCode = e.defaultValueCode ??
            argInfo.defaultCode ??
            argInfo.defaultFunc?.call() as String?;
        // final isInput = e.type.element != null && isInputType(e.type.element!);
        final description = await documentationOfParameter(e, ctx.buildStep);
        // TODO: 1T test
        final deprecationReason = getDeprecationReason(e);
        final attachments = getAttachments(e);
        return refer(type)
            .property('inputField')
            .call(
              [literalString(e.name)],
              {
                if (defaultValueCode != null)
                  'defaultValue': refer(defaultValueCode),
                if (description.isNotEmpty)
                  'description': refer("'$description'"),
                if (deprecationReason != null)
                  'deprecationReason': literalString(deprecationReason),
                if (attachments != null) 'attachments': refer(attachments)
              },
            )
            .accept(_dartEmitter)
            .toString();
      }
    }
  }));
  return inputMaybe.whereType<String>().toList();
}