From f1daaeb1b0b40bda704da24c05d05e30f654687c Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Wed, 22 Nov 2023 19:07:08 +0200 Subject: [PATCH] pkg,service/dap: use switch instead of ifs (#3576) --- pkg/config/split.go | 7 ++++--- pkg/proc/core/linux_core.go | 7 ++++--- service/dap/server.go | 13 +++++++------ service/dap/server_test.go | 7 ++++--- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/pkg/config/split.go b/pkg/config/split.go index 9515671e..97d01373 100644 --- a/pkg/config/split.go +++ b/pkg/config/split.go @@ -47,11 +47,12 @@ func SplitQuotedFields(in string, quote rune) []string { } case inQuote: - if ch == quote { + switch ch { + case quote: state = inField - } else if ch == '\\' { + case '\\': state = inQuoteEscaped - } else { + default: buf.WriteRune(ch) } diff --git a/pkg/proc/core/linux_core.go b/pkg/proc/core/linux_core.go index 4dd94f64..7a2b011d 100644 --- a/pkg/proc/core/linux_core.go +++ b/pkg/proc/core/linux_core.go @@ -280,11 +280,12 @@ func readNote(r io.ReadSeeker, machineType elf.Machine) (*note, error) { descReader := bytes.NewReader(desc) switch note.Type { case elf.NT_PRSTATUS: - if machineType == _EM_X86_64 { + switch machineType { + case _EM_X86_64: note.Desc = &linuxPrStatusAMD64{} - } else if machineType == _EM_AARCH64 { + case _EM_AARCH64: note.Desc = &linuxPrStatusARM64{} - } else { + default: return nil, fmt.Errorf("unsupported machine type") } if err := binary.Read(descReader, binary.LittleEndian, note.Desc); err != nil { diff --git a/service/dap/server.go b/service/dap/server.go index 1880fd2e..96e45c8d 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -2416,17 +2416,18 @@ func (s *Session) childrenToDAPVariables(v *fullyQualifiedVariable) []dap.Variab c := &v.Children[i] cfqname := fmt.Sprintf("%s.%s", v.fullyQualifiedNameOrExpr, c.Name) - if strings.HasPrefix(c.Name, "~") || strings.HasPrefix(c.Name, ".") { + switch { + case strings.HasPrefix(c.Name, "~") || strings.HasPrefix(c.Name, "."): cfqname = "" - } else if v.isScope && v.fullyQualifiedNameOrExpr == "" { + case v.isScope && v.fullyQualifiedNameOrExpr == "": cfqname = c.Name - } else if v.fullyQualifiedNameOrExpr == "" { + case v.fullyQualifiedNameOrExpr == "": cfqname = "" - } else if v.Kind == reflect.Interface { + case v.Kind == reflect.Interface: cfqname = fmt.Sprintf("%s.(%s)", v.fullyQualifiedNameOrExpr, c.Name) // c is data - } else if v.Kind == reflect.Ptr { + case v.Kind == reflect.Ptr: cfqname = fmt.Sprintf("(*%v)", v.fullyQualifiedNameOrExpr) // c is the nameless pointer value - } else if v.Kind == reflect.Complex64 || v.Kind == reflect.Complex128 { + case v.Kind == reflect.Complex64 || v.Kind == reflect.Complex128: cfqname = "" // complex children are not struct fields and can't be accessed directly } cvalue, cvarref := s.convertVariable(c, cfqname) diff --git a/service/dap/server_test.go b/service/dap/server_test.go index 563dbe2e..d215b109 100644 --- a/service/dap/server_test.go +++ b/service/dap/server_test.go @@ -5210,11 +5210,12 @@ func runDebugSessionWithBPs(t *testing.T, client *daptest.Client, cmd string, cm cmdRequest() client.ExpectInitializedEvent(t) - if cmd == "launch" { + switch cmd { + case "launch": client.ExpectLaunchResponse(t) - } else if cmd == "attach" { + case "attach": client.ExpectAttachResponse(t) - } else { + default: panic("expected launch or attach command") }