Skip to content

Commit

Permalink
Fix handling of emtpy sink configs
Browse files Browse the repository at this point in the history
Toml parsing library can return nil in case the object on the list
that is instantiated doesn't have any properties set. This change
fixes that by creating default emtpy object so we don't crash.
  • Loading branch information
p2004a committed Dec 6, 2023
1 parent e3ed12f commit f8e2d22
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ func parseTLSConfig(config *fTLSConfig, defaultServerName string, basePath strin
}

func parseMQTTSink(basePath string, sinkID int, sink *fMQTTSink) (*MQTTSink, error) {
if sink == nil {
sink = &fMQTTSink{}
}
res := &MQTTSink{}
if sink.Name == "" {
res.Name = fmt.Sprintf("unnamed-mqtt-sink-%d", sinkID)
Expand Down Expand Up @@ -196,6 +199,9 @@ func parseMQTTSink(basePath string, sinkID int, sink *fMQTTSink) (*MQTTSink, err
}

func parseCloudPubSubSink(basePath string, sinkID int, sink *fCloudPubSubSink) (*CloudPubSubSink, error) {
if sink == nil {
sink = &fCloudPubSubSink{}
}
ctx := context.Background()
res := &CloudPubSubSink{}
if sink.Name == "" {
Expand Down Expand Up @@ -246,6 +252,9 @@ func parseCloudPubSubSink(basePath string, sinkID int, sink *fCloudPubSubSink) (
}

func parseStdoutSink(sinkID int, sink *fStdoutSink) (*StdoutSink, error) {
if sink == nil {
sink = &fStdoutSink{}
}
res := &StdoutSink{}
if sink.Name == "" {
res.Name = fmt.Sprintf("unnamed-stdout-sink-%d", sinkID)
Expand Down
18 changes: 18 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,21 @@ func TestParsingEmpty(t *testing.T) {
t.Errorf("unexpected difference:\n%v", diff)
}
}

func TestParsingEmptySink(t *testing.T) {
config, err := Read("testdata/test2/config.toml")
if err != nil {
t.Fatalf("Failed to parse config: %v", err)
}
expectedConfig := &Config{
Adapter: "hci0",
Sinks: []Sink{
&StdoutSink{
Name: "unnamed-stdout-sink-0",
},
},
}
if diff := cmpConfig(config, expectedConfig); diff != "" {
t.Errorf("unexpected difference:\n%v", diff)
}
}
1 change: 1 addition & 0 deletions pkg/config/testdata/test2/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[[sinks.stdout]]

0 comments on commit f8e2d22

Please sign in to comment.