Skip to content

Commit

Permalink
feat: the Stringer implements in JSON mode
Browse files Browse the repository at this point in the history
  • Loading branch information
riquezhang authored and tyrzhang committed Dec 20, 2024
1 parent 88764d0 commit 1b087de
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"math"
"sort"
"strings"
)

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

func (tree *Rtree) String() string {
return "foo"
return fmt.Sprintf(`{"size":%d,"depth":%d,"root":%v}`, tree.size, tree.height, tree.root)
}

// Depth returns the maximum depth of tree.
Expand Down Expand Up @@ -223,7 +224,17 @@ type node struct {
}

func (n *node) String() string {
return fmt.Sprintf("node{leaf: %v, entries: %v}", n.leaf, n.entries)
format := func(v []entry) string {
var s []string
for _, e := range v {
s = append(s, fmt.Sprintf("%v", e))
}
return fmt.Sprintf(`[%v]`, strings.Join(s, ","))
}
if n.leaf {
return fmt.Sprintf(`{"leaf":%v,"entries":%v}`, n.leaf, format(n.entries))
}
return fmt.Sprintf(`{"entries":%v}`, format(n.entries))
}

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

func (e entry) String() string {
if e.child != nil {
return fmt.Sprintf("entry{bb: %v, child: %v}", e.bb, e.child)
return fmt.Sprintf(`{"bb":"%v","child":%v}`, e.bb, e.child)
}
return fmt.Sprintf("entry{bb: %v, obj: %v}", e.bb, e.obj)
return fmt.Sprintf(`{"bb":"%v"}`, e.bb)
}

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

0 comments on commit 1b087de

Please sign in to comment.