-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: marshaling of WithSubject events
If a struct is composed with an interface the go json marshaler treats that as a field, thus we need to have a custom json marshaler for the type.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package marshaler | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/openmeterio/openmeter/internal/event/metadata" | ||
) | ||
|
||
type event struct { | ||
Value string `json:"value"` | ||
} | ||
|
||
func (e *event) EventMetadata() metadata.EventMetadata { | ||
return metadata.EventMetadata{} | ||
} | ||
|
||
func (e *event) Validate() error { | ||
return nil | ||
} | ||
|
||
func (e *event) EventName() string { | ||
return "event" | ||
} | ||
|
||
func TestWithSubject(t *testing.T) { | ||
marshaler := New(nil) | ||
|
||
ev := &event{ | ||
Value: "value", | ||
} | ||
|
||
evWithSource := WithSource("source", ev) | ||
msg, err := marshaler.Marshal(evWithSource) | ||
|
||
// Check if the source is set in the metadata | ||
assert.NoError(t, err) | ||
assert.Equal(t, "source", msg.Metadata.Get(CloudEventsHeaderSource)) | ||
|
||
// Check if the event can be unmarshaled | ||
unmarshaledEvent := &event{} | ||
err = marshaler.Unmarshal(msg, unmarshaledEvent) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, ev, unmarshaledEvent) | ||
} |