Skip to content

Commit

Permalink
Explore replaces object with same label
Browse files Browse the repository at this point in the history
  • Loading branch information
emicklei committed Feb 23, 2025
1 parent 91f5a0f commit 4c28558
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 9 additions & 0 deletions explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion index_tmpl.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
break;
case "auto":
default:
toggleButton.textContent = "🔄";
toggleButton.textContent = "";
toggleButton.title = "Auto Theme";
break;
}
Expand Down
26 changes: 18 additions & 8 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down

0 comments on commit 4c28558

Please sign in to comment.