Prepare deterministic D&D normalization in core
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package npcoccurrences
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/pipeline"
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/modules/dnd"
|
||||
)
|
||||
|
||||
func runNormalize(normalizer *Normalizer, ctx context.Context, input contracts.NormalizeInput[dnd.NPCOccurrenceList]) (pipeline.OperationResult[dnd.NPCOccurrenceList], error) {
|
||||
execution := pipeline.ExecutionContext[dnd.NPCOccurrenceList]{Assess: func(context.Context, dnd.NPCOccurrenceList) (contracts.CandidateAssessment, error) {
|
||||
return contracts.CandidateAssessment{}, nil
|
||||
}}
|
||||
return pipeline.ExecuteOperation(ctx, execution, func(ctx context.Context) (contracts.PreparedOperation[dnd.NPCOccurrenceList], error) {
|
||||
return normalizer.PrepareNormalize(ctx, input)
|
||||
})
|
||||
}
|
||||
@@ -61,35 +61,35 @@ func (n *Normalizer) Key() string { return Key }
|
||||
|
||||
func (n *Normalizer) ReferenceSlots() []contracts.ReferenceSlot { return referenceSlots() }
|
||||
|
||||
func (n *Normalizer) Normalize(ctx context.Context, req contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]) (contracts.TypedNormalizeResult[dnd.NPCOccurrenceList], error) {
|
||||
func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.NormalizeInput[dnd.NPCOccurrenceList]) (contracts.PreparedOperation[dnd.NPCOccurrenceList], error) {
|
||||
if n == nil || n.npcResolver == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("normalizer must not be nil")
|
||||
return nil, normalizerErrorf("normalizer must not be nil")
|
||||
}
|
||||
if ctx == nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("context must not be nil")
|
||||
return nil, normalizerErrorf("context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("context error before normalize: %w", err)
|
||||
return nil, normalizerErrorf("context error before normalize: %w", err)
|
||||
}
|
||||
|
||||
registry, err := n.npcResolver.Resolve(req.References)
|
||||
if err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("resolve NPC registry: %w", err)
|
||||
return nil, normalizerErrorf("resolve NPC registry: %w", err)
|
||||
}
|
||||
if !registry.Bound() {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("NPC registry reference is required")
|
||||
return nil, normalizerErrorf("NPC registry reference is required")
|
||||
}
|
||||
index := source.NewDocumentIndex(req.Source)
|
||||
order := shared.NewSourceRefOrderFromIndex(index)
|
||||
value, findings, err := normalizeList(req.MergeOutput.Value, index, order, registry)
|
||||
if err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("validate NPC registry pairs: %w", err)
|
||||
return nil, normalizerErrorf("validate NPC registry pairs: %w", err)
|
||||
}
|
||||
diagnosticGroups, err := diagnostics.NormalizationDiagnostics(findings)
|
||||
if err != nil {
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{}, normalizerErrorf("collect diagnostics: %w", err)
|
||||
return nil, normalizerErrorf("collect diagnostics: %w", err)
|
||||
}
|
||||
return contracts.TypedNormalizeResult[dnd.NPCOccurrenceList]{Value: value, Diagnostics: diagnosticGroups}, nil
|
||||
return contracts.DeterministicOperation[dnd.NPCOccurrenceList]{Evaluation: contracts.CandidateEvaluation[dnd.NPCOccurrenceList]{Result: contracts.EvaluationView[dnd.NPCOccurrenceList]{Candidate: &value, Diagnostics: diagnosticGroups}}}, nil
|
||||
}
|
||||
|
||||
type normalizedRecord struct {
|
||||
|
||||
@@ -24,7 +24,7 @@ func TestNormalizeValidatesPairsAndClones(t *testing.T) {
|
||||
SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}, {SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}, {SourceID: doc.ID, StartUnitID: 30, EndUnitID: 30}},
|
||||
}}}
|
||||
original := append([]source.SourceRef(nil), input.Occurrences[0].SourceRefs...)
|
||||
result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}})
|
||||
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}})
|
||||
if err != nil {
|
||||
t.Fatalf("Normalize() error = %v", err)
|
||||
}
|
||||
@@ -38,7 +38,7 @@ func TestNormalizeValidatesPairsAndClones(t *testing.T) {
|
||||
if !reflect.DeepEqual(input.Occurrences[0].SourceRefs, original) {
|
||||
t.Fatalf("Normalize() mutated input: %#v", input)
|
||||
}
|
||||
second, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: result.Value}})
|
||||
second, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: result.Value}})
|
||||
if err != nil || !reflect.DeepEqual(second.Value, result.Value) || len(second.Diagnostics) != 0 {
|
||||
t.Fatalf("second normalization = %#v, %v; want idempotent output without findings", second, err)
|
||||
}
|
||||
@@ -53,11 +53,11 @@ func TestNormalizeRequiresOperationRegistryAndPreservesEmptyRepresentation(t *te
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
if _, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{}); err == nil {
|
||||
if _, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{}); err == nil {
|
||||
t.Fatal("Normalize() accepted an unbound NPC registry")
|
||||
}
|
||||
for _, input := range []dnd.NPCOccurrenceList{{}, {Occurrences: []dnd.NPCOccurrence{}}} {
|
||||
result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{
|
||||
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{
|
||||
MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}, References: npcReferences(t),
|
||||
})
|
||||
if err != nil || (result.Value.Occurrences == nil) != (input.Occurrences == nil) {
|
||||
@@ -77,7 +77,7 @@ func TestNormalizeRejectsUnknownIDsAndMismatchedNames(t *testing.T) {
|
||||
{NPCID: identity.DeriveID("Ária"), Name: "Borin", Kind: dnd.NPCOccurrenceKindOther, SourceRefs: []source.SourceRef{{SourceID: doc.ID, StartUnitID: 10, EndUnitID: 10}}},
|
||||
} {
|
||||
input := dnd.NPCOccurrenceList{Occurrences: []dnd.NPCOccurrence{occurrence}}
|
||||
if result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}}); err == nil {
|
||||
if result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}}); err == nil {
|
||||
t.Fatalf("Normalize() = %#v, %v; want registry pair rejection", result, err)
|
||||
}
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func TestNormalizeOrdersAndCollapsesExactDuplicatesOnly(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}})
|
||||
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -147,7 +147,7 @@ func TestNormalizeBoundsFindings(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := normalizer.Normalize(context.Background(), contracts.TypedNormalizeRequest[dnd.NPCOccurrenceList]{
|
||||
result, err := runNormalize(normalizer, context.Background(), contracts.NormalizeInput[dnd.NPCOccurrenceList]{
|
||||
Source: doc, MergeOutput: contracts.MergeArtifact[dnd.NPCOccurrenceList]{Value: input},
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user