buildContextFields method

List<ContextField> buildContextFields(
  1. Map<String, dynamic> fields,
  2. List<NestedContextField> nestedFields
)

Converts a { fieldName: schemaType } map to a list of ContextFields.

Nested objects and lists of objects are lifted into NestedContextField entries so templates can generate custom model classes.

Implementation

List<ContextField> buildContextFields(
  Map<String, dynamic> fields,
  List<NestedContextField> nestedFields,
) {
  return fields.entries.map((entry) {
    final value = entry.value;

    // If nested JSON object → treat as custom model type
    if (value is Map<String, dynamic>) {
      final someData = buildContextFields(value, nestedFields);
      nestedFields.add(
        NestedContextField(name: entry.key.camelCaseToPascalCase(), properties: someData),
      );
      return ContextField(
        name: entry.key,
        type: entry.key.camelCaseToPascalCase(),
        isCustom: true,
      );
    }

    // If list of objects → List<CustomModel>
    if (value is List && value.isNotEmpty) {
      var type = "";
      var isCustom = false;
      if (value.first is Map<String, dynamic>) {
        nestedFields.add(
          NestedContextField(
            name: entry.key.camelCaseToPascalCase(),
            properties: buildContextFields(value.first, nestedFields),
          ),
        );
        type = entry.key.camelCaseToPascalCase();
        isCustom = true;
      } else {
        type = getDartType(value.first);
      }
      return ContextField(name: entry.key, type: "List<$type>", isList: true, isCustom: isCustom);
    }

    final type = getDartType(value);
    return ContextField(name: entry.key, type: type, isList: type.contains('List'));
  }).toList();
}