Preserve effective transcript member values

This commit is contained in:
2026-11-17 03:56:40 +00:00
parent a88141ef4a
commit fd65d9ca1e
3 changed files with 69 additions and 0 deletions

View File

@@ -137,6 +137,11 @@ cancellation distinct.
### R4 — Preserve Effective Transcript Members During Typed Decoding (P3)
**Complete.** Each segment resolves duplicate keys before field conversion
and processes case variants in the baseline key order. The decoder keeps
numeric tokens intact. Schema tests cover superseded and effective type
mismatches, both case-variant orders, and exact IDs in both envelopes.
**Affected plan:** Stage 5, item 1.
**Evidence:** The baseline selected the final value of an exact duplicate JSON

View File

@@ -4,8 +4,10 @@ import (
"bytes"
"encoding/json"
"fmt"
"maps"
"math"
"slices"
"strings"
"gitea.maximumdirect.net/eric/audita/v3/internal/core/jsonnumber"
)
@@ -19,6 +21,38 @@ type SourceSegment struct {
Categories []string `json:"categories,omitempty"`
}
// UnmarshalJSON decodes only the final value of each exact key, then applies
// case variants in the same key order as the original object round trip.
func (s *SourceSegment) UnmarshalJSON(raw []byte) error {
var members map[string]json.RawMessage
if err := json.Unmarshal(raw, &members); err != nil {
return err
}
for _, key := range slices.Sorted(maps.Keys(members)) {
var field any
switch {
case strings.EqualFold(key, "id"):
field = &s.ID
case strings.EqualFold(key, "speaker"):
field = &s.Speaker
case strings.EqualFold(key, "start"):
field = &s.Start
case strings.EqualFold(key, "end"):
field = &s.End
case strings.EqualFold(key, "text"):
field = &s.Text
case strings.EqualFold(key, "categories"):
field = &s.Categories
default:
continue
}
if err := json.Unmarshal(members[key], field); err != nil {
return fmt.Errorf("field %q: %w", key, err)
}
}
return nil
}
type Segment struct {
ID int `json:"id"`
Speaker string `json:"speaker"`

View File

@@ -336,6 +336,36 @@ func TestSourceIDRejectsFractionAndOverflow(t *testing.T) {
}
}
func TestSourceTranscriptUsesEffectiveMemberValues(t *testing.T) {
for _, tc := range []struct {
name, speaker, text, wantText string
wantError bool
}{
{"superseded type mismatch", `"speaker":17,"speaker":"A"`, `"text":"hello"`, "hello", false},
{"effective type mismatch", `"speaker":"A","speaker":17`, `"text":"hello"`, "", true},
{"case variant after exact key", `"speaker":"A"`, `"text":"hello","TEXT":"other"`, "hello", false},
{"case variant before exact key", `"speaker":"A"`, `"TEXT":"other","text":"hello"`, "hello", false},
} {
t.Run(tc.name, func(t *testing.T) {
raw := `[{` + tc.speaker + "," + tc.text + `,"start":0,"end":1}]`
transcript, err := ParseSourceTranscriptJSON([]byte(raw))
if tc.wantError {
if err == nil {
t.Fatal("expected a type mismatch in the effective member")
}
return
}
if err != nil {
t.Fatal(err)
}
segment := transcript.Segments[0]
if segment.Speaker != "A" || segment.Text != tc.wantText {
t.Errorf("effective speaker = %q, text = %q", segment.Speaker, segment.Text)
}
})
}
}
func TestTranscriptClonePreservesPresenceAndIndependence(t *testing.T) {
original := &Transcript{Segments: []Segment{
{ID: 40, Text: "original", Categories: []string{"keep"}},