3bdc27f4de
Continue finishing the parts of the text extraction plan * add missing fields
53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
package fieldextraction
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
// CreateFieldExtractionInput represents the complete input for creating a field extraction
|
|
type CreateFieldExtractionInput struct {
|
|
DocumentID uuid.UUID
|
|
SingleFields *repository.AddFieldExtractionParams
|
|
ArrayFields []*repository.AddFieldExtractionArrayFieldParams // All arrays must be same length N
|
|
}
|
|
|
|
// MaxArrayFieldCount is the maximum number of array field rows allowed per field extraction.
|
|
// This prevents excessively large payloads that could impact performance.
|
|
const MaxArrayFieldCount = 10000
|
|
|
|
// ValidateArrayConsistency validates the array fields for a field extraction.
|
|
// It checks:
|
|
// - No nil entries in the array
|
|
// - Array length does not exceed MaxArrayFieldCount
|
|
// - Empty arrays are allowed (returns 0, nil)
|
|
//
|
|
// Note: The service layer assigns arrayIndex values sequentially (0, 1, 2, ...)
|
|
// when inserting, so we don't validate incoming arrayIndex values here.
|
|
// The database enforces uniqueness and non-negative constraints.
|
|
//
|
|
// Returns the array length if valid, error if validation fails.
|
|
func ValidateArrayConsistency(arrayFields []*repository.AddFieldExtractionArrayFieldParams) (int, error) {
|
|
// Empty array is valid
|
|
if len(arrayFields) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
// Check for maximum array size to prevent performance issues
|
|
if len(arrayFields) > MaxArrayFieldCount {
|
|
return 0, fmt.Errorf("array field count %d exceeds maximum allowed %d", len(arrayFields), MaxArrayFieldCount)
|
|
}
|
|
|
|
// Check for nil entries which would cause issues during insertion
|
|
for i, field := range arrayFields {
|
|
if field == nil {
|
|
return 0, fmt.Errorf("array field at index %d is nil", i)
|
|
}
|
|
}
|
|
|
|
return len(arrayFields), nil
|
|
}
|