Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: the Stringer implements in JSON mode #59

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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