Add normalize report diagnostics
This commit is contained in:
@@ -12,31 +12,57 @@ import (
|
||||
"gitea.maximumdirect.net/eric/seriatim/schema"
|
||||
)
|
||||
|
||||
// BuildResult contains normalize output plus deterministic transformation diagnostics.
|
||||
type BuildResult struct {
|
||||
Output any
|
||||
SortingChanged bool
|
||||
IDsReassigned bool
|
||||
SegmentsWithCategories int
|
||||
}
|
||||
|
||||
// Build converts parsed normalize input into a selected seriatim output schema.
|
||||
func Build(parsed ParsedTranscript, cfg config.NormalizeConfig) (any, error) {
|
||||
func Build(parsed ParsedTranscript, cfg config.NormalizeConfig) (BuildResult, error) {
|
||||
ordered := sortedSegments(parsed.Segments)
|
||||
sortingChanged := didSortingChangeOrder(ordered)
|
||||
idsReassigned := didReassignIDs(ordered)
|
||||
segmentsWithCategories := countSegmentsWithCategories(ordered)
|
||||
|
||||
switch cfg.OutputSchema {
|
||||
case config.OutputSchemaMinimal:
|
||||
output := buildMinimal(ordered)
|
||||
if err := schema.ValidateMinimalTranscript(output); err != nil {
|
||||
return nil, fmt.Errorf("validate normalize output: %w", err)
|
||||
return BuildResult{}, fmt.Errorf("validate normalize output: %w", err)
|
||||
}
|
||||
return output, nil
|
||||
return BuildResult{
|
||||
Output: output,
|
||||
SortingChanged: sortingChanged,
|
||||
IDsReassigned: idsReassigned,
|
||||
SegmentsWithCategories: segmentsWithCategories,
|
||||
}, nil
|
||||
case config.OutputSchemaIntermediate:
|
||||
output := buildIntermediate(ordered)
|
||||
if err := schema.ValidateIntermediateTranscript(output); err != nil {
|
||||
return nil, fmt.Errorf("validate normalize output: %w", err)
|
||||
return BuildResult{}, fmt.Errorf("validate normalize output: %w", err)
|
||||
}
|
||||
return output, nil
|
||||
return BuildResult{
|
||||
Output: output,
|
||||
SortingChanged: sortingChanged,
|
||||
IDsReassigned: idsReassigned,
|
||||
SegmentsWithCategories: segmentsWithCategories,
|
||||
}, nil
|
||||
case config.OutputSchemaFull:
|
||||
output := buildFull(ordered, cfg)
|
||||
if err := schema.ValidateTranscript(output); err != nil {
|
||||
return nil, fmt.Errorf("validate normalize output: %w", err)
|
||||
return BuildResult{}, fmt.Errorf("validate normalize output: %w", err)
|
||||
}
|
||||
return output, nil
|
||||
return BuildResult{
|
||||
Output: output,
|
||||
SortingChanged: sortingChanged,
|
||||
IDsReassigned: idsReassigned,
|
||||
SegmentsWithCategories: segmentsWithCategories,
|
||||
}, nil
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported output schema %q", cfg.OutputSchema)
|
||||
return BuildResult{}, fmt.Errorf("unsupported output schema %q", cfg.OutputSchema)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,3 +182,35 @@ func copyIntPtr(value *int) *int {
|
||||
copied := *value
|
||||
return &copied
|
||||
}
|
||||
|
||||
func didSortingChangeOrder(segments []InputSegment) bool {
|
||||
for index, segment := range segments {
|
||||
if segment.InputIndex != index {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func didReassignIDs(segments []InputSegment) bool {
|
||||
if len(segments) == 0 {
|
||||
return false
|
||||
}
|
||||
for index, segment := range segments {
|
||||
newID := index + 1
|
||||
if segment.OriginalID == nil || *segment.OriginalID != newID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func countSegmentsWithCategories(segments []InputSegment) int {
|
||||
count := 0
|
||||
for _, segment := range segments {
|
||||
if len(segment.Categories) > 0 {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user