createParamVar function

Expression createParamVar(
  1. BaseParameterAnnotation annotation,
  2. ServerParam param, {
  3. String routePath = '',
})

Implementation

Expression createParamVar(
  BaseParameterAnnotation annotation,
  ServerParam param, {
  String routePath = '',
}) {
  if (annotation is! ServerParamAnnotation) {
    throw ArgumentError('Invalid annotation type: ${annotation.runtimeType}');
  }

  final paramKey = annotation.name ?? param.name;
  final wildcardKey = wildcardParamKey(routePath, paramKey);

  if (wildcardKey != null &&
      param.type.iterableType == IterableType.list &&
      param.type.name == 'List<String>') {
    return refer('context')
        .property('request')
        .property('wildcardParameters')
        .index(literalString(wildcardKey))
        .nullSafeProperty('toList')
        .call([]);
  }

  final paramsRef = refer(
    'context',
  ).property('request').property('pathParameters');

  return paramsRef.index(literalString(wildcardKey ?? paramKey));
}