fix(go): normalize history parsed field names
This commit is contained in:
@@ -27,7 +27,7 @@ func BuildFieldsEnvelope(env envelope.FrameEnvelope) (envelope.FrameEnvelope, bo
|
|||||||
return envelope.FrameEnvelope{}, false
|
return envelope.FrameEnvelope{}, false
|
||||||
}
|
}
|
||||||
rows := realtimeKVFields(env, env.Parsed)
|
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 {
|
for _, row := range rows {
|
||||||
key := row.Domain
|
key := row.Domain
|
||||||
if strings.TrimSpace(row.Field) != "" {
|
if strings.TrimSpace(row.Field) != "" {
|
||||||
@@ -35,11 +35,6 @@ func BuildFieldsEnvelope(env envelope.FrameEnvelope) (envelope.FrameEnvelope, bo
|
|||||||
}
|
}
|
||||||
fields[key] = row.Value
|
fields[key] = row.Value
|
||||||
}
|
}
|
||||||
for key, value := range env.Fields {
|
|
||||||
if strings.TrimSpace(key) != "" {
|
|
||||||
fields[key] = value
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
return envelope.FrameEnvelope{}, false
|
return envelope.FrameEnvelope{}, false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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) {
|
func TestRealtimeKVFieldsFromJT808ParsedFields(t *testing.T) {
|
||||||
jtRows := realtimeKVFields(envelope.FrameEnvelope{
|
jtRows := realtimeKVFields(envelope.FrameEnvelope{
|
||||||
Protocol: envelope.ProtocolJT808,
|
Protocol: envelope.ProtocolJT808,
|
||||||
|
|||||||
Reference in New Issue
Block a user