From 4c28558c252df60bb4995f866f10c06f1632038e Mon Sep 17 00:00:00 2001 From: Nil Zork Date: Sun, 23 Feb 2025 15:28:09 +0100 Subject: [PATCH] Explore replaces object with same label --- CHANGES.md | 4 ++++ explorer.go | 9 +++++++++ index_tmpl.html | 2 +- service.go | 26 ++++++++++++++++++-------- 4 files changed, 32 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 3ccd5dc..045438c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,7 @@ +### v0.8.1 + + - Explore replaces object with same root label. + ### v0.8.0 - add SetDefault to make a Service globally accessing to add new structs. diff --git a/explorer.go b/explorer.go index 6f3d501..14010c6 100644 --- a/explorer.go +++ b/explorer.go @@ -138,6 +138,15 @@ func (e *explorer) updateObjectAt(row, col int, updater func(access objectAccess e.putObjectStartingAt(row, col, updater(old), Row(row)) } +func (e *explorer) putObjectAt(row, col int, access objectAccess) { + r, ok := e.accessMap[row] + if !ok { + r = map[int]objectAccess{} + e.accessMap[row] = r + } + r[col] = access +} + func (e *explorer) putObjectStartingAt(row, col int, access objectAccess, option ExploreOption) { r, ok := e.accessMap[row] if !ok { diff --git a/index_tmpl.html b/index_tmpl.html index 56b3e59..0503364 100644 --- a/index_tmpl.html +++ b/index_tmpl.html @@ -164,7 +164,7 @@ break; case "auto": default: - toggleButton.textContent = "🔄"; + toggleButton.textContent = "★"; toggleButton.title = "Auto Theme"; break; } diff --git a/service.go b/service.go index 023c6dc..07b0852 100644 --- a/service.go +++ b/service.go @@ -21,7 +21,8 @@ type Service interface { // Dump writes an HTML file for displaying the current state of the explorer and its entries. Dump() - // Explore adds a new entry (next available row in column 0) for a value unless it cannot be explored. + // Explore adds or replaces (matching on label) a new entry for a value unless it cannot be explored. + // The object will be placed on the next available column on row 1. Explore(label string, value any, options ...ExploreOption) Service // ExplorePath adds a new entry for a value at the specified access path unless it cannot be explored. @@ -104,20 +105,15 @@ func (s *service) serveIndex(w http.ResponseWriter, _ *http.Request) { } } -// Explore adds a new entry (next available row in column 0) for a value if it can be explored. +// Explore adds or replaces a new entry (next available row in column 0) for a value if it can be explored. func (s *service) Explore(label string, value any, options ...ExploreOption) Service { defer s.protect()() - row, column := 0, 0 - placement := Row(row) - if len(options) > 0 { - placement = options[0] - row, column = options[0].placement(s.explorer) - } if !canExplore(value) { slog.Info("value can not be explored", "value", value) return s } + oa := objectAccess{ isRoot: true, object: value, @@ -127,6 +123,20 @@ func (s *service) Explore(label string, value any, options ...ExploreOption) Ser typeName: fmt.Sprintf("%T", value), } + // are we replacing an object access? + _, oldRow, oldcolumn, ok := s.explorer.rootAccessWithLabel(label) + if ok { + s.explorer.putObjectAt(oldRow, oldcolumn, oa) + return s + } + + // add as new + row, column := 0, 0 + placement := Row(0) + if len(options) > 0 { + placement = options[0] + row, column = options[0].placement(s.explorer) + } s.explorer.putObjectStartingAt(row, column, oa, placement) return s }