Skip to content

Commit 9077db3

Browse files
committed
Build strings with append and strings.Join
1 parent 233652f commit 9077db3

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

client.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/rand"
55
"fmt"
66
"os"
7+
"strings"
78

89
"github.com/gomodule/redigo/redis"
910
"github.com/olekukonko/tablewriter"
@@ -61,23 +62,24 @@ type Node struct {
6162
}
6263

6364
func (n *Node) String() string {
64-
s := "("
65+
s := []string{"("}
6566
if n.Alias != "" {
66-
s += n.Alias
67+
s = append(s, n.Alias)
6768
}
6869
if n.Label != "" {
69-
s += ":" + n.Label
70+
s = append(s, ":", n.Label)
7071
}
7172
if len(n.Properties) > 0 {
72-
p := ""
73+
p := make([]string, 0, len(n.Properties))
7374
for k, v := range n.Properties {
74-
p += fmt.Sprintf("%s:%v,", k, quoteString(v))
75+
p = append(p, fmt.Sprintf("%s:%v", k, quoteString(v)))
7576
}
76-
p = p[:len(p)-1]
77-
s += "{" + p + "}"
77+
s = append(s, "{")
78+
s = append(s, strings.Join(p, ","))
79+
s = append(s, "}")
7880
}
79-
s += ")"
80-
return s
81+
s = append(s, ")")
82+
return strings.Join(s, "")
8183
}
8284

8385
// Edge represents an edge connecting two nodes in the graph.
@@ -89,26 +91,28 @@ type Edge struct {
8991
}
9092

9193
func (e *Edge) String() string {
92-
s := "(" + e.Source.Alias + ")"
94+
s := []string{"(", e.Source.Alias, ")"}
9395

94-
s += "-["
96+
s = append(s, "-[")
9597
if e.Relation != "" {
96-
s += ":" + e.Relation
98+
s = append(s, ":", e.Relation)
9799
}
98100

99101
if len(e.Properties) > 0 {
100-
p := ""
102+
p := make([]string, 0, len(e.Properties))
101103
for k, v := range e.Properties {
102-
p += fmt.Sprintf("%s:%s,", k, quoteString(v))
104+
p = append(p, fmt.Sprintf("%s:%v", k, quoteString(v)))
103105
}
104-
p = p[:len(p)-1]
105-
s += "{" + p + "}"
106+
s = append(s, strings.Join(p, ","))
107+
s = append(s, "{")
108+
s = append(s, p...)
109+
s = append(s, "}")
106110
}
107-
s += "]->"
111+
s = append(s, "]->")
108112

109-
s += "(" + e.Destination.Alias + ")"
113+
s = append(s, "(", e.Destination.Alias, ")")
110114

111-
return s
115+
return strings.Join(s, "")
112116
}
113117

114118
// Graph represents a graph, which is a collection of nodes and edges.
@@ -159,14 +163,14 @@ func (g *Graph) AddEdge(e *Edge) error {
159163

160164
// Commit creates the entire graph, but will readd nodes if called again.
161165
func (g *Graph) Commit() (QueryResult, error) {
162-
q := "CREATE "
166+
items := make([]string, 0, len(g.Nodes)+len(g.Edges))
163167
for _, n := range g.Nodes {
164-
q += fmt.Sprintf("%s,", n)
168+
items = append(items, n.String())
165169
}
166170
for _, e := range g.Edges {
167-
q += fmt.Sprintf("%s,", e)
171+
items = append(items, e.String())
168172
}
169-
q = q[:len(q)-1]
173+
q := "CREATE " + strings.Join(items, ",")
170174
return g.Query(q)
171175
}
172176

0 commit comments

Comments
 (0)