Skip to content

Commit

Permalink
fix: break many symbols with one name
Browse files Browse the repository at this point in the history
  • Loading branch information
siyul-park committed Dec 21, 2024
1 parent c67b1e0 commit 38210af
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 75 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ DOCKERFILE = deployments/Dockerfile
CGO_ENABLED ?= 1

.PHONY: init generate build clean tidy update sync check test coverage benchmark lint fmt vet doc docker-build
all: lint test build

init:
@cp .go.work go.work
Expand Down
129 changes: 76 additions & 53 deletions cmd/pkg/cli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -43,13 +42,10 @@ type debugView interface {

// Various debug view types
type (
errDebugView struct{ err error }
frameDebugView struct{ frame *agent.Frame }
framesDebugView struct{ frames []*agent.Frame }
breakpointDebugView struct {
id int
breakpoint *debug.Breakpoint
}
errDebugView struct{ err error }
frameDebugView struct{ frame *agent.Frame }
framesDebugView struct{ frames []*agent.Frame }
breakpointDebugView struct{ breakpoint *debug.Breakpoint }
breakpointsDebugView struct{ breakpoints []*debug.Breakpoint }
symbolDebugView struct{ symbol *symbol.Symbol }
symbolsDebugView struct{ symbols []*symbol.Symbol }
Expand Down Expand Up @@ -143,43 +139,58 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "quit", "q":
return m, tea.Quit
case "break", "b":
var sb *symbol.Symbol
if len(args) > 1 {
sb = m.findSymbol(args[1])
if sb == nil {
var bps []*debug.Breakpoint
if len(args) <= 1 {
bp := debug.NewBreakpoint()
m.debugger.AddBreakpoint(bp)

bps = append(bps, bp)
} else {
sbs := m.findSymbols(args[1])
if len(sbs) == 0 {
m.view = &errDebugView{err: fmt.Errorf("symbol '%s' not found", args[1])}
return m, nil
}
}

var inPort *port.InPort
var outPort *port.OutPort
if len(args) > 2 {
inPort, outPort = m.findPort(sb, args[2])
if inPort == nil && outPort == nil {
m.view = &errDebugView{err: fmt.Errorf("port '%s' not found on symbol '%s'", args[2], sb.Name())}
return m, nil
for _, sb := range sbs {
var inPort *port.InPort
var outPort *port.OutPort
if len(args) > 2 {
inPort, outPort = m.findPort(sb, args[2])
if inPort == nil && outPort == nil {
continue
}
}

bp := debug.NewBreakpoint(
debug.WithSymbol(sb),
debug.WithInPort(inPort),
debug.WithOutPort(outPort),
)
m.debugger.AddBreakpoint(bp)

bps = append(bps, bp)
}
}

bp := debug.NewBreakpoint(
debug.WithSymbol(sb),
debug.WithInPort(inPort),
debug.WithOutPort(outPort),
)
m.debugger.AddBreakpoint(bp)

bps := m.debugger.Breakpoints()
m.view = &breakpointDebugView{id: len(bps) - 1, breakpoint: bp}
if len(bps) == 1 {
m.view = &breakpointDebugView{breakpoint: bps[0]}
} else {
m.view = &breakpointsDebugView{breakpoints: bps}
}

return m, func() tea.Msg {
if m.debugger.Pause(context.Background()) {
if m.debugger.Breakpoint() == bp {
return m.debugger.Frame()
var cmds []tea.Cmd
for _, bp := range bps {
cmds = append(cmds, func() tea.Msg {
if m.debugger.Pause(context.Background()) {
if m.debugger.Breakpoint() == bp {
return m.debugger.Frame()
}
}
}
return nil
return nil
})
}
return m, tea.Batch(cmds...)
case "continue", "c":
m.view = nil

Expand All @@ -190,12 +201,14 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return nil
}
case "delete", "d":
bps := m.debugger.Breakpoints()

var bp *debug.Breakpoint
if len(args) > 1 {
if i, err := strconv.Atoi(args[1]); err == nil && i < len(bps) {
bp = bps[i]
bps := m.debugger.Breakpoints()
for _, b := range bps {
if b.ID().String() == args[1] {
bp = b
break
}
}
} else {
bp = m.debugger.Breakpoint()
Expand All @@ -211,12 +224,14 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.view = &breakpointsDebugView{breakpoints: bps}
return m, nil
case "breakpoint", "bp":
bps := m.debugger.Breakpoints()

var bp *debug.Breakpoint
if len(args) > 1 {
if i, err := strconv.Atoi(args[1]); err == nil && i < len(bps) {
bp = bps[i]
bps := m.debugger.Breakpoints()
for _, b := range bps {
if b.ID().String() == args[1] {
bp = b
break
}
}
} else {
bp = m.debugger.Breakpoint()
Expand All @@ -230,14 +245,21 @@ func (m *debugModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.view = &symbolsDebugView{symbols: sbs}
return m, nil
case "symbol", "sb":
var sb *symbol.Symbol
var sbs []*symbol.Symbol
if len(args) > 1 {
sb = m.findSymbol(args[1])
sbs = m.findSymbols(args[1])
} else {
sbs = []*symbol.Symbol{m.debugger.Symbol()}
}

if len(sbs) == 0 {
m.view = &errDebugView{err: fmt.Errorf("symbol '%s' not found", args[1])}
} else if len(sbs) == 1 {
m.view = &symbolDebugView{symbol: sbs[0]}
} else {
sb = m.debugger.Symbol()
m.view = &symbolsDebugView{symbols: sbs}
}

m.view = &symbolDebugView{symbol: sb}
return m, nil
case "processes", "procs":
procs := m.agent.Processes()
Expand Down Expand Up @@ -299,13 +321,14 @@ func (m *debugModel) nextInput(msg tea.Msg) tea.Cmd {
return cmd
}

func (m *debugModel) findSymbol(key string) *symbol.Symbol {
func (m *debugModel) findSymbols(key string) []*symbol.Symbol {
var symbols []*symbol.Symbol
for _, sb := range m.agent.Symbols() {
if sb.ID().String() == key || sb.Name() == key {
return sb
symbols = append(symbols, sb)
}
}
return nil
return symbols
}

func (m *debugModel) findPort(sb *symbol.Symbol, name string) (*port.InPort, *port.OutPort) {
Expand Down Expand Up @@ -419,7 +442,7 @@ func (v *breakpointDebugView) Interface() map[string]any {
return nil
}

value := map[string]any{"id": v.id}
value := map[string]any{"id": v.breakpoint.ID()}

sb := v.breakpoint.Symbol()
if sb != nil {
Expand Down Expand Up @@ -450,8 +473,8 @@ func (v *breakpointsDebugView) View() string {
writer := resource.NewWriter(buffer)

values := make([]any, 0, len(v.breakpoints))
for i, b := range v.breakpoints {
value := (&breakpointDebugView{id: i, breakpoint: b}).Interface()
for _, b := range v.breakpoints {
value := (&breakpointDebugView{breakpoint: b}).Interface()
values = append(values, value)
}

Expand Down
41 changes: 37 additions & 4 deletions cmd/pkg/cli/debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestNewDebugger(t *testing.T) {
}

func TestDebugModel_Update(t *testing.T) {
t.Run("break", func(t *testing.T) {
t.Run("break <symbol> <port>", func(t *testing.T) {
a := agent.New()
defer a.Close()

Expand Down Expand Up @@ -65,6 +65,39 @@ func TestDebugModel_Update(t *testing.T) {
assert.Len(t, d.Breakpoints(), 1)
})

t.Run("break", func(t *testing.T) {
a := agent.New()
defer a.Close()

d := debug.NewDebugger(a)
defer d.Close()

m := &debugModel{
input: textinput.New(),
agent: a,
debugger: d,
}

sb := &symbol.Symbol{
Spec: &spec.Meta{
ID: uuid.Must(uuid.NewV7()),
Kind: faker.UUIDHyphenated(),
Namespace: resource.DefaultNamespace,
Name: faker.UUIDHyphenated(),
},
Node: node.NewOneToOneNode(nil),
}
defer sb.Close()

m.agent.Load(sb)
defer m.agent.Unload(sb)

m.input.SetValue("break")
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

assert.Len(t, d.Breakpoints(), 1)
})

t.Run("continue", func(t *testing.T) {
a := agent.New()
defer a.Close()
Expand Down Expand Up @@ -101,7 +134,7 @@ func TestDebugModel_Update(t *testing.T) {
// TODO: assert
})

t.Run("delete", func(t *testing.T) {
t.Run("delete <breakpoint>", func(t *testing.T) {
a := agent.New()
defer a.Close()

Expand Down Expand Up @@ -131,7 +164,7 @@ func TestDebugModel_Update(t *testing.T) {
m.input.SetValue(fmt.Sprintf("break %s %s", sb.Name(), node.PortIn))
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

m.input.SetValue(fmt.Sprintf("delete %d", 0))
m.input.SetValue(fmt.Sprintf("delete %s", d.Breakpoints()[0].ID()))
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

assert.Len(t, d.Breakpoints(), 0)
Expand Down Expand Up @@ -203,7 +236,7 @@ func TestDebugModel_Update(t *testing.T) {
m.input.SetValue(fmt.Sprintf("break %s %s", sb.Name(), node.PortIn))
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

m.input.SetValue(fmt.Sprintf("breakpoint %d", 0))
m.input.SetValue(fmt.Sprintf("breakpoint %s", d.Breakpoints()[0].ID()))
m.Update(tea.KeyMsg{Type: tea.KeyEnter})

assert.Contains(t, m.View(), sb.Name())
Expand Down
20 changes: 15 additions & 5 deletions cmd/pkg/resource/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resource
import (
"fmt"
"io"
"math"
"slices"
"strings"

Expand Down Expand Up @@ -61,13 +62,18 @@ func (w *Writer) Write(value any) error {
counts[key]++
}
}
sizes := map[string]int{}

scores := map[string]float64{}
for _, element := range elements {
for key, value := range element {
sizes[key] += len(fmt.Sprint(value))
for key, val := range element {
scores[key] += 1.0 - 1.0/float64(len(fmt.Sprint(val)))
}
}

for key, score := range scores {
scores[key] = score / float64(counts[key])
}

var keys []string
for key, count := range counts {
if count >= len(elements)/2 {
Expand All @@ -79,8 +85,12 @@ func (w *Writer) Write(value any) error {
if diff := counts[y] - counts[x]; diff != 0 {
return diff
}
if diff := sizes[x] - sizes[y]; diff != 0 {
return diff
if diff := scores[x] - scores[y]; math.Abs(diff) > 0.1 {
if diff > 0 {
return 1
} else {
return -1
}
}
return strings.Compare(x, y)
})
Expand Down
6 changes: 3 additions & 3 deletions cmd/pkg/resource/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestWriter_Write(t *testing.T) {
"key1": "value1",
"key2": 123,
},
expected: " KEY2 KEY1 \n 123 value1 ",
expected: " KEY1 KEY2 \n value1 123 ",
},
{
input: []map[string]any{
Expand All @@ -31,7 +31,7 @@ func TestWriter_Write(t *testing.T) {
"key2": 456,
},
},
expected: " KEY2 KEY1 \n 123 value1 \n 456 value2 ",
expected: " KEY1 KEY2 \n value1 123 \n value2 456 ",
},
{
input: []map[string]any{
Expand All @@ -42,7 +42,7 @@ func TestWriter_Write(t *testing.T) {
"key2": 456,
},
},
expected: " KEY2 KEY1 \n <nil> value1 \n 456 <nil> ",
expected: " KEY1 KEY2 \n value1 <nil> \n <nil> 456 ",
},
}

Expand Down
Loading

0 comments on commit 38210af

Please sign in to comment.