Skip to content

Commit 97ee38a

Browse files
committed
feat: allow object to have two relations instead of duplicating it
1 parent 2b8e016 commit 97ee38a

2 files changed

Lines changed: 71 additions & 28 deletions

File tree

thorlog/v3/event.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ type Context []ContextObject
121121

122122
// ContextObject describes a relation of an object to another.
123123
type ContextObject struct {
124-
Object ReportableObject `json:"object" textlog:",expand"`
125-
RelationType string `json:"relation_type"` // RelationType is used to specify the type of relation, e.g. "derives from" or "related to"
126-
RelationName string `json:"relation_name"` // RelationName is used to specify the name of the relation, e.g. "parent". It is optional.
127-
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.
124+
Object ReportableObject `json:"object" textlog:",expand"`
125+
Relations []Relation `json:"relations" textlog:",expand" jsonschema:"minItems=1"`
126+
}
127+
128+
type Relation struct {
129+
Type string `json:"relation_type"` // RelationType is used to specify the type of relation, e.g. "derives from" or "related to"
130+
Name string `json:"relation_name"` // RelationName is used to specify the name of the relation, e.g. "parent". It is optional.
131+
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.
128132
}
129133

130134
func (c *ContextObject) UnmarshalJSON(data []byte) error {
@@ -148,18 +152,27 @@ func (c *ContextObject) UnmarshalJSON(data []byte) error {
148152
const omitInContext = "omitincontext"
149153

150154
func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry {
151-
var elementsByRelation [][]ContextObject
155+
type objectsByRelation struct {
156+
Relation Relation
157+
Objects []ContextObject
158+
}
159+
var elementsByRelation []objectsByRelation
152160
for _, element := range c {
153161
var groupExists bool
162+
if len(element.Relations) == 0 {
163+
continue
164+
}
165+
// only use the first relation for textlog conversion
166+
relation := element.Relations[0]
154167
for i := range elementsByRelation {
155-
if elementsByRelation[i][0].RelationName == element.RelationName {
156-
elementsByRelation[i] = append(elementsByRelation[i], element)
168+
if elementsByRelation[i].Relation == relation {
169+
elementsByRelation[i].Objects = append(elementsByRelation[i].Objects, element)
157170
groupExists = true
158171
break
159172
}
160173
}
161174
if !groupExists {
162-
elementsByRelation = append(elementsByRelation, []ContextObject{element})
175+
elementsByRelation = append(elementsByRelation, objectsByRelation{Relation: relation, Objects: []ContextObject{element}})
163176
}
164177
}
165178
oldOmit := t.Omit
@@ -175,11 +188,11 @@ func (c Context) MarshalTextLog(t jsonlog.TextlogFormatter) jsonlog.TextlogEntry
175188

176189
var result jsonlog.TextlogEntry
177190
for _, group := range elementsByRelation {
178-
for g, element := range group {
191+
for g, element := range group.Objects {
179192
marshaledElement := t.Format(element)
180193
for i := range marshaledElement {
181-
marshaledElement[i].Key = jsonlog.ConcatTextLabels(strings.ToUpper(element.RelationName), marshaledElement[i].Key)
182-
if !element.Unique {
194+
marshaledElement[i].Key = jsonlog.ConcatTextLabels(strings.ToUpper(group.Relation.Name), marshaledElement[i].Key)
195+
if !group.Relation.Unique {
183196
marshaledElement[i].Key = jsonlog.ConcatTextLabels(marshaledElement[i].Key, strconv.Itoa(g+1))
184197
}
185198
}

thorlog/v3/event_test.go

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ func TestContext_MarshalTextLog(t *testing.T) {
2626
name: "context with unique related object",
2727
c: &Context{
2828
{
29-
Object: NewFile("path/to/file"),
30-
RelationName: "file",
31-
Unique: true,
29+
Object: NewFile("path/to/file"),
30+
Relations: []Relation{{
31+
Name: "file",
32+
Unique: true,
33+
}},
3234
},
3335
},
3436
want: "FILE: path/to/file",
@@ -37,14 +39,18 @@ func TestContext_MarshalTextLog(t *testing.T) {
3739
name: "context with related object group",
3840
c: &Context{
3941
{
40-
Object: NewFile("path/to/file"),
41-
RelationName: "file",
42-
Unique: false,
42+
Object: NewFile("path/to/file"),
43+
Relations: []Relation{{
44+
Name: "file",
45+
Unique: false,
46+
}},
4347
},
4448
{
45-
Object: NewFile("path/to/otherfile"),
46-
RelationName: "file",
47-
Unique: false,
49+
Object: NewFile("path/to/otherfile"),
50+
Relations: []Relation{{
51+
Name: "file",
52+
Unique: false,
53+
}},
4854
},
4955
},
5056
want: "FILE_1: path/to/file FILE_2: path/to/otherfile",
@@ -53,18 +59,40 @@ func TestContext_MarshalTextLog(t *testing.T) {
5359
name: "context with different related objects",
5460
c: &Context{
5561
{
56-
Object: NewFile("path/to/file"),
57-
RelationName: "file",
58-
Unique: false,
62+
Object: NewFile("path/to/file"),
63+
Relations: []Relation{{
64+
Name: "file",
65+
Unique: false,
66+
}},
5967
},
6068
{
61-
Object: NewFile("path/to/otherfile"),
62-
RelationName: "archive",
63-
Unique: true,
69+
Object: NewFile("path/to/otherfile"),
70+
Relations: []Relation{{
71+
Name: "archive",
72+
Unique: true,
73+
}},
6474
},
6575
},
6676
want: "FILE_1: path/to/file ARCHIVE_FILE: path/to/otherfile",
6777
},
78+
{
79+
name: "context with object related in two ways",
80+
c: &Context{
81+
{
82+
Object: NewFile("path/to/file"),
83+
Relations: []Relation{{
84+
Name: "parent",
85+
Type: "derived from",
86+
Unique: true,
87+
}, {
88+
Name: "origin",
89+
Type: "derived from",
90+
Unique: true,
91+
}},
92+
},
93+
},
94+
want: "PARENT_FILE: path/to/6z6zfile",
95+
},
6896
}
6997
var formatter jsonlog.TextlogFormatter
7098
for _, tt := range tests {
@@ -104,8 +132,10 @@ func TestFinding_UnmarshalJSON(t *testing.T) {
104132
Subject: NewFile("path/to/file"),
105133
EventContext: Context{
106134
{
107-
Object: NewAtJob(),
108-
RelationType: "related to",
135+
Object: NewAtJob(),
136+
Relations: []Relation{{
137+
Type: "related to",
138+
}},
109139
},
110140
},
111141
Reasons: []Reason{

0 commit comments

Comments
 (0)