feat(go): reuse parsed fields across projections

This commit is contained in:
lingniu
2026-07-03 17:38:50 +08:00
parent 368b18fc96
commit 378a5d5e57
11 changed files with 620 additions and 64 deletions

View File

@@ -318,11 +318,11 @@ func jsonString(value any) string {
}
func parsedFieldsJSONString(env envelope.FrameEnvelope) string {
fieldsEnv, ok := realtime.BuildFieldsEnvelope(env)
if !ok || len(fieldsEnv.Fields) == 0 {
fields, _, ok := realtime.ParsedFieldsForEnvelope(env)
if !ok || len(fields) == 0 {
return ""
}
return jsonString(fieldsEnv.Fields)
return jsonString(fields)
}
func floatField(env envelope.FrameEnvelope, key string) (float64, bool) {

View File

@@ -111,8 +111,8 @@ func TestWriterChunksOversizedParsedFields(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec)
env := sampleEnvelope()
env.Parsed = map[string]any{
"large_payload": strings.Repeat("x", 16_128),
env.ParsedFields = map[string]any{
"jt808.location.large_payload": strings.Repeat("x", 16_128),
}
if err := writer.AppendRawFrame(context.Background(), env); err != nil {
@@ -131,6 +131,30 @@ func TestWriterChunksOversizedParsedFields(t *testing.T) {
}
}
func TestWriterUsesEnvelopeParsedFieldsWithoutReflattening(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec)
env := sampleEnvelope()
env.Parsed = map[string]any{
"location": map[string]any{"speed_kmh": 99},
}
env.ParsedFields = map[string]any{
"jt808.location.speed_kmh": "12.3",
}
if err := writer.AppendRawFrame(context.Background(), env); err != nil {
t.Fatalf("AppendRawFrame() error = %v", err)
}
rawInsert := findSQL(exec.calls, "INSERT INTO raw_")
if !strings.Contains(rawInsert, `"jt808.location.speed_kmh":"12.3"`) {
t.Fatalf("raw insert should use precomputed parsed fields: %s", rawInsert)
}
if strings.Contains(rawInsert, `"jt808.location.speed_kmh":"99"`) {
t.Fatalf("raw insert should not re-flatten parsed payload: %s", rawInsert)
}
}
func TestWriterLeavesParsedFieldsEmptyWhenNoParsedPayload(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec)