Skip to content

Commit 446f6a1

Browse files
committed
New graph.Node method to create a copy
Node.Copy() creates a copy of the node. It could be useful to return in a query output the modified content of a node without actually modiying it.
1 parent 88137ed commit 446f6a1

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

graffiti/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ require (
4040
github.com/skydive-project/go-debouncer v1.0.0
4141
github.com/spf13/cast v1.3.1
4242
github.com/spf13/cobra v1.1.1
43+
github.com/stretchr/testify v1.5.1
4344
github.com/tchap/zapext v1.0.0
4445
github.com/xeipuuv/gojsonschema v1.2.0
4546
go.uber.org/zap v1.16.0

graffiti/graph/graph.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,21 @@ func (n *Node) String() string {
575575
return string(b)
576576
}
577577

578+
func (n *Node) Copy() *Node {
579+
return &Node{
580+
graphElement: graphElement{
581+
ID: n.ID,
582+
Host: n.Host,
583+
Origin: n.Origin,
584+
CreatedAt: n.CreatedAt,
585+
UpdatedAt: n.UpdatedAt,
586+
DeletedAt: n.DeletedAt,
587+
Revision: n.Revision,
588+
Metadata: n.Metadata.Copy(),
589+
},
590+
}
591+
}
592+
578593
// UnmarshalJSON custom unmarshal function
579594
func (n *Node) UnmarshalJSON(b []byte) error {
580595
// wrapper to avoid unmarshal infinite loop

graffiti/graph/graph_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import (
2222
"strconv"
2323
"strings"
2424
"testing"
25+
"time"
26+
27+
"github.com/stretchr/testify/assert"
2528
)
2629

2730
func newGraph(t *testing.T) *Graph {
@@ -509,6 +512,47 @@ func TestEvents(t *testing.T) {
509512
}
510513
}
511514

515+
func TestNodeCopy(t *testing.T) {
516+
n := &Node{
517+
graphElement: graphElement{
518+
ID: Identifier("id"),
519+
Host: "Host",
520+
Origin: "Origin",
521+
CreatedAt: Time(time.Unix(100, 0)),
522+
UpdatedAt: Time(time.Unix(200, 0)),
523+
DeletedAt: Time(time.Unix(300, 0)),
524+
Revision: 1,
525+
Metadata: Metadata{"foo": "bar"},
526+
},
527+
}
528+
529+
cn := n.Copy()
530+
assert.Equal(t, n, cn)
531+
532+
// Check that modifications in the copied node do not affect the origin node
533+
ok := cn.Metadata.SetField("new", "value")
534+
assert.Truef(t, ok, "copied node set metadata")
535+
assert.NotEqualf(t, n, cn, "Metadata")
536+
537+
cn.Host = "modified"
538+
assert.NotEqualf(t, n, cn, "Host")
539+
540+
cn.Origin = "modified"
541+
assert.NotEqualf(t, n, cn, "Origin")
542+
543+
cn.Revision = 99
544+
assert.NotEqualf(t, n, cn, "Revision")
545+
546+
cn.CreatedAt = Time(time.Unix(100, 99))
547+
assert.NotEqualf(t, n, cn, "CreatedAt")
548+
549+
cn.UpdatedAt = Time(time.Unix(200, 99))
550+
assert.NotEqualf(t, n, cn, "UpdatedAt")
551+
552+
cn.DeletedAt = Time(time.Unix(300, 99))
553+
assert.NotEqualf(t, n, cn, "DeletedAt")
554+
}
555+
512556
type FakeRecursiveListener1 struct {
513557
DefaultGraphListener
514558
graph *Graph

0 commit comments

Comments
 (0)