Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
fix: Avoid nil pointer exception in AddTemporaryData (#460)
Browse files Browse the repository at this point in the history
* fix: Avoid nil pointer exception in AddTemporaryData

Signed-off-by: Florian Bacher <[email protected]>

* added unit test

Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl authored May 5, 2022
1 parent a0381c5 commit 5672c07
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions pkg/api/models/keptn_context_extended_c_e.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,11 @@ const temporaryDataRootKey = "temporaryData"
// AddTemporaryData adds further (temporary) properties to the data section of the keptn event
func (ce *KeptnContextExtendedCE) AddTemporaryData(key string, tmpData TemporaryData, opts AddTemporaryDataOptions) error {
eventData := map[string]interface{}{}
err := ce.DataAs(&eventData)
if err != nil {
return err
if ce.Data != nil {
err := ce.DataAs(&eventData)
if err != nil {
return err
}
}
if temporaryData, found := eventData[temporaryDataRootKey]; found {
if _, kfound := temporaryData.(map[string]interface{})[key]; kfound {
Expand Down
21 changes: 21 additions & 0 deletions pkg/api/models/keptn_context_extended_c_e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ func TestAddTemporaryData(t *testing.T) {
require.Nil(t, err)
}

func TestAddTemporaryData_EventDataNil(t *testing.T) {
event := models.KeptnContextExtendedCE{}

type AdditionalData struct {
SomeString string `json:"someString"`
SomeInt int `json:"someInt"`
}
temporaryDataToAdd := models.TemporaryData(AdditionalData{
SomeString: "Bernd",
SomeInt: 34,
})
err := event.AddTemporaryData("distributor", temporaryDataToAdd, models.AddTemporaryDataOptions{})
require.Nil(t, err)

addi := AdditionalData{}
err = event.GetTemporaryData("distributor", &addi)
fmt.Println(addi.SomeInt)

require.Nil(t, err)
}

func TestKeptnContextExtendedCE_TemporaryData(t *testing.T) {
type TestData struct {
v0_2_0.EventData
Expand Down

0 comments on commit 5672c07

Please sign in to comment.