refactor(go): default to go vehicle topics

This commit is contained in:
lingniu
2026-07-02 20:22:47 +08:00
parent 3248336bea
commit 2ae9794dda
15 changed files with 130 additions and 27 deletions

View File

@@ -8,6 +8,7 @@ import (
"github.com/segmentio/kafka-go"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
)
type Sink interface {
@@ -48,9 +49,9 @@ func NewKafkaSink(cfg KafkaConfig) (*KafkaSink, error) {
func newKafkaSinkWithWriter(writer kafkaWriter, cfg KafkaConfig) *KafkaSink {
rawTopics := map[envelope.Protocol]string{
envelope.ProtocolGB32960: "vehicle.raw.gb32960.v1",
envelope.ProtocolJT808: "vehicle.raw.jt808.v1",
envelope.ProtocolYutongMQTT: "vehicle.raw.yutong-mqtt.v1",
envelope.ProtocolGB32960: topics.RawGB32960,
envelope.ProtocolJT808: topics.RawJT808,
envelope.ProtocolYutongMQTT: topics.RawYutongMQTT,
}
for protocol, topic := range cfg.RawTopics {
if topic != "" {
@@ -59,7 +60,7 @@ func newKafkaSinkWithWriter(writer kafkaWriter, cfg KafkaConfig) *KafkaSink {
}
unifiedTopic := cfg.UnifiedTopic
if unifiedTopic == "" {
unifiedTopic = "vehicle.event.unified.v1"
unifiedTopic = topics.Unified
}
return &KafkaSink{writer: writer, rawTopics: rawTopics, unifiedTopic: unifiedTopic}
}

View File

@@ -23,8 +23,8 @@ func TestKafkaSinkRoutesRawTopics(t *testing.T) {
t.Fatalf("messages = %d", len(writer.messages))
}
msg := writer.messages[0]
if msg.Topic != "vehicle.raw.jt808.v1" {
t.Fatalf("topic = %q", msg.Topic)
if got, want := msg.Topic, "vehicle.raw.go.jt808.v1"; got != want {
t.Fatalf("topic = %q, want %q", got, want)
}
if string(msg.Key) != "JT808:13307795425" {
t.Fatalf("key = %q", string(msg.Key))
@@ -82,7 +82,7 @@ func TestKafkaSinkPublishesDurableRecordsInSingleWriterCall(t *testing.T) {
if len(writer.messages) != 2 {
t.Fatalf("messages = %d, want 2", len(writer.messages))
}
if writer.messages[0].Topic != "vehicle.raw.jt808.v1" || writer.messages[1].Topic != "custom.unified" {
if writer.messages[0].Topic != "vehicle.raw.go.jt808.v1" || writer.messages[1].Topic != "custom.unified" {
t.Fatalf("topics = %q, %q", writer.messages[0].Topic, writer.messages[1].Topic)
}
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/nats-io/nats.go"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/envelope"
"lingniu-vehicle-ingest/go/vehicle-gateway/internal/topics"
)
type NATSConfig struct {
@@ -55,9 +56,9 @@ func NewNATSSink(cfg NATSConfig) (*NATSSink, error) {
func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink {
rawSubjects := map[envelope.Protocol]string{
envelope.ProtocolGB32960: "vehicle.raw.gb32960.v1",
envelope.ProtocolJT808: "vehicle.raw.jt808.v1",
envelope.ProtocolYutongMQTT: "vehicle.raw.yutong-mqtt.v1",
envelope.ProtocolGB32960: topics.RawGB32960,
envelope.ProtocolJT808: topics.RawJT808,
envelope.ProtocolYutongMQTT: topics.RawYutongMQTT,
}
for protocol, subject := range cfg.RawSubjects {
if subject != "" {
@@ -66,7 +67,7 @@ func newNATSSinkWithPublisher(publisher natsPublisher, cfg NATSConfig) *NATSSink
}
unifiedSubject := cfg.UnifiedSubject
if unifiedSubject == "" {
unifiedSubject = "vehicle.event.unified.v1"
unifiedSubject = topics.Unified
}
return &NATSSink{
publisher: publisher,

View File

@@ -40,6 +40,23 @@ func TestNATSSinkRoutesRawAndUnifiedSubjects(t *testing.T) {
}
}
func TestNATSSinkDefaultsToGoRawSubjects(t *testing.T) {
publisher := &recordingNATSPublisher{}
sink := newNATSSinkWithPublisher(publisher, NATSConfig{})
err := sink.PublishRaw(context.Background(), envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
Phone: "13307795425",
ReceivedAtMS: 1782918600000,
})
if err != nil {
t.Fatalf("PublishRaw() error = %v", err)
}
if got, want := publisher.messages[0].subject, "vehicle.raw.go.jt808.v1"; got != want {
t.Fatalf("subject = %q, want %q", got, want)
}
}
type recordingNATSPublisher struct {
messages []recordedNATSMessage
}