Skip to content

Commit 1b087de

Browse files
riquezhangtyrzhang
authored andcommitted
feat: the Stringer implements in JSON mode
1 parent 88764d0 commit 1b087de

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

rtree.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"fmt"
1010
"math"
1111
"sort"
12+
"strings"
1213
)
1314

1415
// Comparator compares two spatials and returns whether they are equal.
@@ -71,7 +72,7 @@ func (tree *Rtree) Size() int {
7172
}
7273

7374
func (tree *Rtree) String() string {
74-
return "foo"
75+
return fmt.Sprintf(`{"size":%d,"depth":%d,"root":%v}`, tree.size, tree.height, tree.root)
7576
}
7677

7778
// Depth returns the maximum depth of tree.
@@ -223,7 +224,17 @@ type node struct {
223224
}
224225

225226
func (n *node) String() string {
226-
return fmt.Sprintf("node{leaf: %v, entries: %v}", n.leaf, n.entries)
227+
format := func(v []entry) string {
228+
var s []string
229+
for _, e := range v {
230+
s = append(s, fmt.Sprintf("%v", e))
231+
}
232+
return fmt.Sprintf(`[%v]`, strings.Join(s, ","))
233+
}
234+
if n.leaf {
235+
return fmt.Sprintf(`{"leaf":%v,"entries":%v}`, n.leaf, format(n.entries))
236+
}
237+
return fmt.Sprintf(`{"entries":%v}`, format(n.entries))
227238
}
228239

229240
// entry represents a spatial index record stored in a tree node.
@@ -235,9 +246,9 @@ type entry struct {
235246

236247
func (e entry) String() string {
237248
if e.child != nil {
238-
return fmt.Sprintf("entry{bb: %v, child: %v}", e.bb, e.child)
249+
return fmt.Sprintf(`{"bb":"%v","child":%v}`, e.bb, e.child)
239250
}
240-
return fmt.Sprintf("entry{bb: %v, obj: %v}", e.bb, e.obj)
251+
return fmt.Sprintf(`{"bb":"%v"}`, e.bb)
241252
}
242253

243254
// Spatial is an interface for objects that can be stored in an Rtree and queried.

0 commit comments

Comments
 (0)