buildAggregateInputIr function

CodeUnit buildAggregateInputIr(
  1. SchemaDocument schema,
  2. ModelDefinition model
)

Builds aggregate-input IR classes for model without rendering to a string.

Returns a CodeUnit whose CodeUnit.classes list contains exactly the Count, Avg (if numeric fields exist), Sum (if numeric fields exist), Min, and Max aggregate-input classes in that order. The result can be used in tests to verify structure independently of rendered source.

Implementation

CodeUnit buildAggregateInputIr(SchemaDocument schema, ModelDefinition model) {
  final generator = const ClientGenerator();
  final scalarFields = generator._scalarFields(schema, model);
  final numericFields = generator._numericAggregateFields(schema, model);
  final comparableFields = generator._comparableAggregateFields(schema, model);

  CodeClass inputClass(String className, List<FieldDefinition> fields) =>
      CodeClass(
        name: className,
        constructors: [
          CodeConstructor(
            className: className,
            parameters: [
              for (final f in fields)
                CodeParameter(
                  name: f.name,
                  isThis: true,
                  defaultValue: 'false',
                ),
            ],
          ),
        ],
        fields: [
          for (final f in fields)
            CodeField(name: f.name, type: 'bool', defaultValue: 'false'),
        ],
      );

  return CodeUnit(
    classes: [
      inputClass('${model.name}CountAggregateInput', scalarFields),
      if (numericFields.isNotEmpty) ...[
        inputClass('${model.name}AvgAggregateInput', numericFields),
        inputClass('${model.name}SumAggregateInput', numericFields),
      ],
      inputClass('${model.name}MinAggregateInput', comparableFields),
      inputClass('${model.name}MaxAggregateInput', comparableFields),
    ],
  );
}