Prepare occurrence chronology keys before sorting
This commit is contained in:
@@ -91,8 +91,11 @@ func (n *Normalizer) PrepareNormalize(ctx context.Context, req contracts.Normali
|
||||
}
|
||||
|
||||
type normalizedRecord struct {
|
||||
occurrence dnd.LocationOccurrence
|
||||
inputIndex int
|
||||
occurrence dnd.LocationOccurrence
|
||||
inputIndex int
|
||||
earliest int
|
||||
hasEvidence bool
|
||||
kindRank int
|
||||
}
|
||||
|
||||
type nameCanonicalization struct{ from, to string }
|
||||
@@ -105,7 +108,8 @@ func normalizeList(input dnd.LocationOccurrenceList, documentIndex source.Docume
|
||||
findings := make([]diagnostics.Finding, 0)
|
||||
for index, inputOccurrence := range input.Occurrences {
|
||||
occurrence, change, found, refsChanged := normalizeOccurrence(inputOccurrence, order, registry)
|
||||
records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index}
|
||||
earliest, hasEvidence := order.EarliestValid(occurrence.SourceRefs)
|
||||
records[index] = normalizedRecord{occurrence: occurrence, inputIndex: index, earliest: earliest, hasEvidence: hasEvidence, kindRank: kindOrder(occurrence.Kind)}
|
||||
if change != nil {
|
||||
findings = append(findings, diagnostics.Finding{Scope: occurrenceScope(index), ReasonCode: ReasonCodeNameCanonicalized,
|
||||
Message: fmt.Sprintf("input index %d: location name canonicalized from %s to %s", index, diagnostics.Quote(change.from), diagnostics.Quote(change.to))})
|
||||
@@ -120,7 +124,14 @@ func normalizeList(input dnd.LocationOccurrenceList, documentIndex source.Docume
|
||||
}
|
||||
}
|
||||
sort.SliceStable(records, func(left, right int) bool {
|
||||
return lessOccurrence(order, records[left].occurrence, records[right].occurrence)
|
||||
l, r := records[left], records[right]
|
||||
if l.hasEvidence != r.hasEvidence {
|
||||
return l.hasEvidence
|
||||
}
|
||||
if l.hasEvidence && l.earliest != r.earliest {
|
||||
return l.earliest < r.earliest
|
||||
}
|
||||
return lessOccurrenceTies(order, l.occurrence, r.occurrence, l.kindRank, r.kindRank)
|
||||
})
|
||||
for position, record := range records {
|
||||
if position != record.inputIndex {
|
||||
@@ -236,14 +247,19 @@ func lessOccurrence(order shared.SourceRefOrder, left, right dnd.LocationOccurre
|
||||
if leftHasEvidence && leftPosition != rightPosition {
|
||||
return leftPosition < rightPosition
|
||||
}
|
||||
return lessOccurrenceTies(order, left, right, kindOrder(left.Kind), kindOrder(right.Kind))
|
||||
}
|
||||
|
||||
// The normalizer has already established equal earliest-evidence keys here.
|
||||
func lessOccurrenceTies(order shared.SourceRefOrder, left, right dnd.LocationOccurrence, leftRank, rightRank int) bool {
|
||||
if left.LocationID != right.LocationID {
|
||||
return left.LocationID < right.LocationID
|
||||
}
|
||||
if left.Name != right.Name {
|
||||
return left.Name < right.Name
|
||||
}
|
||||
if kindOrder(left.Kind) != kindOrder(right.Kind) {
|
||||
return kindOrder(left.Kind) < kindOrder(right.Kind)
|
||||
if leftRank != rightRank {
|
||||
return leftRank < rightRank
|
||||
}
|
||||
if left.Kind != right.Kind {
|
||||
return left.Kind < right.Kind
|
||||
|
||||
Reference in New Issue
Block a user