Skip to content

Commit

Permalink
rename follow
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Dec 4, 2024
1 parent 0c5a81e commit e16999a
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The explorer can also be asked to dump an HTML page with the current state of va

s := structexplorer.NewService()
s.Explore("yours", yourStruct)
s.Follow("yours.field")
s.ExplorePath("yours.field") // dotted path of fields starting with an explore label
s.Dump()

Another method is to use a special test case which starts an explorer at the end of a test and then run it with a longer acceptable timeout.
Expand Down
5 changes: 2 additions & 3 deletions examples/dump/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ func TestWatch(t *testing.T) {
o2 := &thing{val: "blue"}
svc.Explore("thing2", o2) // without option starts at 0,0

svc.Follow("thing.arr", structexplorer.RowColumn(2, 2))
svc.Follow("thing2.arr", structexplorer.OnColumn(1))
svc.ExplorePath("thing.arr", structexplorer.RowColumn(2, 2))
svc.ExplorePath("thing2.arr", structexplorer.Column(1))

svc.Follow("thing2.non-existing")
svc.Dump()

// explore more
Expand Down
8 changes: 4 additions & 4 deletions explore_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ func RowColumn(row, column int) ExploreOption {
}
}

// OnColumn places the next object in the same column on a new free row.
func OnColumn(column int) ExploreOption {
// Column places the next object in the same column on a new free row.
func Column(column int) ExploreOption {
return ExploreOption{
placement: func(e *explorer) (newRow, newColumn int) {
return e.nextFreeRow(column) + 1, column
},
}
}

// OnRow places the next object in the same row on a new free column.
func OnRow(row int) ExploreOption {
// Row places the next object in the same row on a new free column.
func Row(row int) ExploreOption {
return ExploreOption{
placement: func(e *explorer) (newRow, newColumn int) {
return row, e.nextFreeColumn(row) + 1
Expand Down
4 changes: 2 additions & 2 deletions explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func newExplorerOnAll(labelValuePairs ...any) *explorer {
label: label,
hideZeros: true,
typeName: fmt.Sprintf("%T", value),
}, OnRow(row))
}, Row(row))
row++
}
return s
Expand Down Expand Up @@ -135,7 +135,7 @@ func (e *explorer) removeObjectAt(row, col int) {
func (e *explorer) updateObjectAt(row, col int, updater func(access objectAccess) objectAccess) {
old := e.objectAt(row, col)
e.removeObjectAt(row, col)
e.putObjectStartingAt(row, col, updater(old), OnRow(row))
e.putObjectStartingAt(row, col, updater(old), Row(row))
}

func (e *explorer) putObjectStartingAt(row, col int, access objectAccess, option ExploreOption) {
Expand Down
4 changes: 2 additions & 2 deletions explorer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestExplorerClear(t *testing.T) {
if got, want := len(x.accessMap), 1; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
x.putObjectStartingAt(1, 1, objectAccess{}, OnRow(0))
x.putObjectStartingAt(1, 1, objectAccess{}, Row(0))
if got, want := len(x.accessMap), 2; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
Expand Down Expand Up @@ -52,7 +52,7 @@ func TestExplorerTable(t *testing.T) {
t.Errorf("got [%v]:%T want [%v]:%T", got, got, want, want)
}
o2 := objectAccess{object: 1}
x.putObjectStartingAt(1, 1, o2, OnColumn(1))
x.putObjectStartingAt(1, 1, o2, Column(1))
o3 := x.objectAt(1, 1)
if o2.object != o3.object {
t.Fail()
Expand Down
12 changes: 6 additions & 6 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type Service interface {
// Explore adds a new entry (next available row in column 0) for a value unless it cannot be explored.
Explore(label string, value any, options ...ExploreOption) Service

// Follow adds a new entry for a value at the specified path unless it cannot be explored.
Follow(path string, options ...ExploreOption) Service
// ExplorePath adds a new entry for a value at the specified access path unless it cannot be explored.
ExplorePath(dottedPath string, options ...ExploreOption) Service
}

//go:embed index_tmpl.html
Expand Down Expand Up @@ -109,7 +109,7 @@ func (s *service) Explore(label string, value any, options ...ExploreOption) Ser
defer s.protect()()

row, column := 0, 0
placement := OnRow(row)
placement := Row(row)
if len(options) > 0 {
placement = options[0]
row, column = options[0].placement(s.explorer)
Expand Down Expand Up @@ -222,11 +222,11 @@ func (s *service) serveInstructions(w http.ResponseWriter, r *http.Request) {
}
}
oa.typeName = fmt.Sprintf("%T", v)
s.explorer.putObjectStartingAt(toRow, toColumn, oa, OnRow(toRow))
s.explorer.putObjectStartingAt(toRow, toColumn, oa, Row(toRow))
}
}

func (s *service) Follow(newPath string, options ...ExploreOption) Service {
func (s *service) ExplorePath(newPath string, options ...ExploreOption) Service {
if newPath == "" {
return s
}
Expand All @@ -243,7 +243,7 @@ func (s *service) Follow(newPath string, options ...ExploreOption) Service {
label: newPath,
hideZeros: true,
}
placement := OnRow(row)
placement := Row(row)
if len(options) > 0 {
placement = options[0]
}
Expand Down
6 changes: 3 additions & 3 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func TestServe(t *testing.T) {

func TestServiceFollow(t *testing.T) {
s := NewService("now", time.Now()).(*service)
s.Follow("now.loc")
s.ExplorePath("now.loc")
oa := s.explorer.accessMap[0][1]
if got, want := oa.label, "now.loc"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
}
s.Follow("now.ext", RowColumn(1, 1))
s.ExplorePath("now.ext", RowColumn(1, 1))
oa = s.explorer.accessMap[1][1]
if got, want := oa.label, "now.ext"; got != want {
t.Errorf("got [%[1]v:%[1]T] want [%[2]v:%[2]T]", got, want)
Expand All @@ -77,7 +77,7 @@ func TestServiceExploreWithOption(t *testing.T) {
}
func TestServicEmptyFollow(t *testing.T) {
s := NewService().(*service)
s.Follow("")
s.ExplorePath("")
if len(s.explorer.accessMap) != 0 {
t.Fail()
}
Expand Down

0 comments on commit e16999a

Please sign in to comment.