fix(go): normalize history parsed field names

This commit is contained in:
lingniu
2026-07-03 14:42:10 +08:00
parent 6624930cc1
commit 96e9382d73
2 changed files with 43 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ func BuildFieldsEnvelope(env envelope.FrameEnvelope) (envelope.FrameEnvelope, bo
return envelope.FrameEnvelope{}, false
}
rows := realtimeKVFields(env, env.Parsed)
fields := make(map[string]any, len(rows)+len(env.Fields))
fields := make(map[string]any, len(rows))
for _, row := range rows {
key := row.Domain
if strings.TrimSpace(row.Field) != "" {
@@ -35,11 +35,6 @@ func BuildFieldsEnvelope(env envelope.FrameEnvelope) (envelope.FrameEnvelope, bo
}
fields[key] = row.Value
}
for key, value := range env.Fields {
if strings.TrimSpace(key) != "" {
fields[key] = value
}
}
if len(fields) == 0 {
return envelope.FrameEnvelope{}, false
}

View File

@@ -39,6 +39,48 @@ func TestRealtimeKVFieldsFromGB32960ParsedDomains(t *testing.T) {
}
}
func TestBuildFieldsEnvelopeUsesOnlyMappedFieldNames(t *testing.T) {
fieldsEnv, ok := BuildFieldsEnvelope(envelope.FrameEnvelope{
Protocol: envelope.ProtocolGB32960,
VIN: "VIN001",
MessageID: "0x02",
EventTimeMS: 1000,
ReceivedAtMS: 1100,
EventID: "event-1",
Fields: map[string]any{
"charge_status": 1,
envelope.FieldSOCPercent: 88.0,
envelope.FieldTotalMileageKM: 100.1,
},
Parsed: map[string]any{
"data_units": []any{
map[string]any{"type": "0x01", "name": "vehicle", "value": map[string]any{
"charge_status": 1,
"soc_percent": 88.0,
"total_mileage_km": 100.1,
}},
},
},
})
if !ok {
t.Fatal("BuildFieldsEnvelope() should emit mapped fields")
}
for _, bareKey := range []string{"charge_status", "soc_percent", "total_mileage_km"} {
if _, exists := fieldsEnv.Fields[bareKey]; exists {
t.Fatalf("fields envelope should not expose bare env.Fields key %q: %#v", bareKey, fieldsEnv.Fields)
}
}
for _, mappedKey := range []string{
"gb32960.vehicle.charge_status",
"gb32960.vehicle.soc_percent",
"gb32960.vehicle.total_mileage_km",
} {
if _, exists := fieldsEnv.Fields[mappedKey]; !exists {
t.Fatalf("fields envelope missing mapped key %q: %#v", mappedKey, fieldsEnv.Fields)
}
}
}
func TestRealtimeKVFieldsFromJT808ParsedFields(t *testing.T) {
jtRows := realtimeKVFields(envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,