createArgFromParam function

Expression createArgFromParam(
  1. ServerParamAnnotation annotation,
  2. ServerParam param
)

Implementation

Expression createArgFromParam(
  ServerParamAnnotation annotation,
  ServerParam param,
) {
  final paramsRef =
      refer('context').property('request').property('pathParameters');

  var paramValue =
      paramsRef.index(literalString(annotation.name ?? param.name));

  final acceptsNull = annotation.acceptsNull;
  if ((acceptsNull != null && !acceptsNull) ||
      (!param.isNullable && annotation.pipe == null)) {
    paramValue = paramValue.ifNullThen(
      createMissingArgumentException(
        key: annotation.name ?? param.name,
        location: '@${AnnotationType.param.name}',
      ).thrown.parenthesized,
    );
  }

  if (annotation.pipe case final pipe?) {
    final name = annotation.name;
    return createPipe(
      pipe,
      annotationArgument: name == null ? literalNull : literalString(name),
      nameOfParameter: param.name,
      type: AnnotationType.param,
      access: paramValue,
    );
  }

  return paramValue;
}