Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions thorlog/v3/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,17 @@ var _ common.Event = (*Finding)(nil)
type Context []ContextObject

type ContextObject struct {
Object ReportableObject `json:"object" textlog:",expand"`
Relation string `json:"relation"`
Unique bool `json:"unique"`
Object ReportableObject `json:"object" textlog:",expand"`
RelationType string `json:"relation_type"` // RelationType is used to specify the type of relation, e.g. "derives from" or "related to"
RelationName string `json:"relation_name"` // RelationName is used to specify the name of the relation, e.g. "parent". It is optional.
Unique bool `json:"unique"` // Unique indicates whether the relation is unique, i.e. there can only be one object with this relation type / name in the context.
}

func (c *ContextObject) UnmarshalJSON(data []byte) error {
type plainContextObject ContextObject
var rawContextObject struct {
Object EmbeddedObject `json:"object"`
Relation string `json:"relation"`
Unique bool `json:"unique"`
Object EmbeddedObject `json:"object"`
plainContextObject
}
if err := json.Unmarshal(data, &rawContextObject); err != nil {
return err
Expand All @@ -110,9 +111,8 @@ func (c *ContextObject) UnmarshalJSON(data []byte) error {
if !isReportable {
return fmt.Errorf("object of type %q must implement the reportable interface", rawContextObject.Object.Object.EmbeddedHeader().Type)
}
*c = ContextObject(rawContextObject.plainContextObject) // Copy the fields from rawContextObject to c
c.Object = reportableObject
c.Relation = rawContextObject.Relation
c.Unique = rawContextObject.Unique
return nil
}

Expand All @@ -121,7 +121,7 @@ func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry
for _, element := range c {
var groupExists bool
for i := range elementsByRelation {
if elementsByRelation[i][0].Relation == element.Relation {
if elementsByRelation[i][0].RelationName == element.RelationName {
elementsByRelation[i] = append(elementsByRelation[i], element)
groupExists = true
break
Expand All @@ -137,7 +137,7 @@ func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry
for g, element := range group {
marshaledElement := t.Format(element)
for i := range marshaledElement {
marshaledElement[i].Key = jsonlog.ConcatTextLabels(strings.ToUpper(element.Relation), marshaledElement[i].Key)
marshaledElement[i].Key = jsonlog.ConcatTextLabels(strings.ToUpper(element.RelationName), marshaledElement[i].Key)
if !element.Unique {
marshaledElement[i].Key = jsonlog.ConcatTextLabels(marshaledElement[i].Key, strconv.Itoa(g+1))
}
Expand Down
33 changes: 17 additions & 16 deletions thorlog/v3/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func TestContext_MarshalTextLog(t *testing.T) {
name: "context with unique related object",
c: &Context{
{
Object: NewFile("path/to/file"),
Relation: "file",
Unique: true,
Object: NewFile("path/to/file"),
RelationName: "file",
Unique: true,
},
},
want: "FILE: path/to/file",
Expand All @@ -37,14 +37,14 @@ func TestContext_MarshalTextLog(t *testing.T) {
name: "context with related object group",
c: &Context{
{
Object: NewFile("path/to/file"),
Relation: "file",
Unique: false,
Object: NewFile("path/to/file"),
RelationName: "file",
Unique: false,
},
{
Object: NewFile("path/to/otherfile"),
Relation: "file",
Unique: false,
Object: NewFile("path/to/otherfile"),
RelationName: "file",
Unique: false,
},
},
want: "FILE_1: path/to/file FILE_2: path/to/otherfile",
Expand All @@ -53,14 +53,14 @@ func TestContext_MarshalTextLog(t *testing.T) {
name: "context with different related objects",
c: &Context{
{
Object: NewFile("path/to/file"),
Relation: "file",
Unique: false,
Object: NewFile("path/to/file"),
RelationName: "file",
Unique: false,
},
{
Object: NewFile("path/to/otherfile"),
Relation: "archive",
Unique: true,
Object: NewFile("path/to/otherfile"),
RelationName: "archive",
Unique: true,
},
},
want: "FILE_1: path/to/file ARCHIVE_FILE: path/to/otherfile",
Expand Down Expand Up @@ -104,7 +104,8 @@ func TestFinding_UnmarshalJSON(t *testing.T) {
Subject: NewFile("path/to/file"),
EventContext: Context{
{
Object: NewAtJob(),
Object: NewAtJob(),
RelationType: "related to",
},
},
Reasons: []Reason{
Expand Down