---
name: diff_entities
kind: function
lang: go
domain: datascience
version: "1.0.0"
purity: pure
signature: "func DiffEntities(before, after []map[string]any, key string, ignoreFields []string) map[string]any"
description: "Compara dos snapshots de entities y devuelve diferencias campo a campo. Detecta añadidas, eliminadas, modificadas e inalteradas. Ignora created_at y updated_at por defecto (pasar nil para usar defaults)."
tags: [datascience, diff, entities, operations, snapshot, comparison]
uses_functions: []
uses_types: []
returns: []
returns_optional: false
error_type: ""
imports: ["fmt"]
params:
  - name: before
    desc: "slice de snapshots de entities antes (ej: resultado de query anterior)"
  - name: after
    desc: "slice de snapshots de entities después (ej: resultado de query actual)"
  - name: key
    desc: "nombre del campo clave para emparejar entities (ej: 'id', 'uuid')"
  - name: ignoreFields
    desc: "slice de nombres de campos a ignorar en comparación (nil usa defaults ['created_at', 'updated_at'])"
output: "mapa con 'summary', 'added', 'removed', 'modified', 'unchanged' - análisis completo de diferencias"
tested: true
tests:
  - "entity añadida"
  - "entity eliminada"
  - "entity modificada con detalle de campos"
  - "entities identicas → unchanged"
  - "ignore_fields funciona"
  - "lista vacia vs lista con datos"
  - "summary format correcto"
test_file_path: "functions/datascience/diff_entities_test.go"
file_path: "functions/datascience/diff_entities.go"
---

## Ejemplo

```go
before := []map[string]any{
    {"id": "1", "name": "Alice", "status": "active"},
    {"id": "2", "name": "Bob"},
}
after := []map[string]any{
    {"id": "1", "name": "Alice", "status": "inactive"},
    {"id": "3", "name": "Carol"},
}
result := DiffEntities(before, after, "id", nil)
// result["summary"] = "1 added, 1 removed, 1 modified, 0 unchanged"
// result["added"] = [{"id": "3", "name": "Carol"}]
// result["removed"] = [{"id": "2", "name": "Bob"}]
// result["modified"] = [{"key": "1", "changes": {"status": {"old": "active", "new": "inactive"}}}]
```

## Notas

Funcion pura. Compara valores con fmt.Sprintf("%v", ...) para manejar tipos heterogeneos en map[string]any.
ignoreFields nil usa los defaults ["created_at", "updated_at"]. Para no ignorar ningun campo, pasar []string{}.
Semantica identica a diff_entities_py_datascience, permite comparar resultados entre ejecuciones del mismo pipeline.
