Skip to content

Commit

Permalink
Refactor found to ok
Browse files Browse the repository at this point in the history
Ok is more idiomatic in Go.
  • Loading branch information
ankur22 committed Jan 11, 2024
1 parent b2b536b commit 5f754c2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 32 deletions.
16 changes: 8 additions & 8 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,13 @@ func (h *ElementHandle) ownerFrame(apiCtx context.Context) *Frame {
if frameId == "" {
return nil
}
frame, found := h.frame.page.frameManager.getFrameByID(frameId)
if found {
frame, ok := h.frame.page.frameManager.getFrameByID(frameId)
if ok {
return frame
}
for _, page := range h.frame.page.browserCtx.browser.pages {
frame, found = page.frameManager.getFrameByID(frameId)
if found {
frame, ok = page.frameManager.getFrameByID(frameId)
if ok {
return frame
}
}
Expand Down Expand Up @@ -766,8 +766,8 @@ func (h *ElementHandle) ContentFrame() (*Frame, error) {
return nil, fmt.Errorf("element is not an iframe")
}

frame, found := h.frame.manager.getFrameByID(node.FrameID)
if !found {
frame, ok := h.frame.manager.getFrameByID(node.FrameID)
if !ok {
return nil, fmt.Errorf("frame not found for id %s", node.FrameID)
}

Expand Down Expand Up @@ -1008,8 +1008,8 @@ func (h *ElementHandle) OwnerFrame() (*Frame, error) {
return nil, fmt.Errorf("no frame found for node: %w", err)
}

frame, found := h.frame.manager.getFrameByID(node.FrameID)
if !found {
frame, ok := h.frame.manager.getFrameByID(node.FrameID)
if !ok {
return nil, fmt.Errorf("no frame found for id %s", node.FrameID)
}

Expand Down
4 changes: 2 additions & 2 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,8 @@ func (f *Frame) onLoadingStopped() {
}

func (f *Frame) position() (*Position, error) {
frame, found := f.manager.getFrameByID(cdp.FrameID(f.page.targetID))
if !found {
frame, ok := f.manager.getFrameByID(cdp.FrameID(f.page.targetID))
if !ok {
return nil, fmt.Errorf("could not find frame with id %s", f.page.targetID)
}
if frame == f.page.frameManager.MainFrame() {
Expand Down
16 changes: 8 additions & 8 deletions common/frame_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ func (m *FrameManager) frameAttached(frameID cdp.FrameID, parentFrameID cdp.Fram
func (m *FrameManager) frameDetached(frameID cdp.FrameID, reason cdppage.FrameDetachedReason) {
m.logger.Debugf("FrameManager:frameDetached", "fmid:%d fid:%v", m.ID(), frameID)

frame, found := m.getFrameByID(frameID)
if !found {
frame, ok := m.getFrameByID(frameID)
if !ok {
m.logger.Debugf("FrameManager:frameDetached:return",
"fmid:%d fid:%v cannot find frame",
m.ID(), frameID)
Expand All @@ -190,8 +190,8 @@ func (m *FrameManager) frameLifecycleEvent(frameID cdp.FrameID, event LifecycleE
"fmid:%d fid:%v event:%s",
m.ID(), frameID, lifecycleEventToString[event])

frame, found := m.getFrameByID(frameID)
if found {
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLifecycleEvent(event)
}
}
Expand All @@ -200,8 +200,8 @@ func (m *FrameManager) frameLoadingStarted(frameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameLoadingStarted",
"fmid:%d fid:%v", m.ID(), frameID)

frame, found := m.getFrameByID(frameID)
if found {
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLoadingStarted()
}
}
Expand All @@ -210,8 +210,8 @@ func (m *FrameManager) frameLoadingStopped(frameID cdp.FrameID) {
m.logger.Debugf("FrameManager:frameLoadingStopped",
"fmid:%d fid:%v", m.ID(), frameID)

frame, found := m.getFrameByID(frameID)
if found {
frame, ok := m.getFrameByID(frameID)
if ok {
frame.onLoadingStopped()
}
}
Expand Down
22 changes: 11 additions & 11 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,8 @@ func (fs *FrameSession) initIsolatedWorld(name string) error {
if fs.isMainFrame() {
frames = fs.manager.Frames()
} else {
frame, found := fs.manager.getFrameByID(cdp.FrameID(fs.targetID))
if found {
frame, ok := fs.manager.getFrameByID(cdp.FrameID(fs.targetID))
if ok {
frames = []*Frame{frame}
}
}
Expand Down Expand Up @@ -662,8 +662,8 @@ func (fs *FrameSession) onExecutionContextCreated(event *cdpruntime.EventExecuti
k6ext.Panic(fs.ctx, "unmarshaling executionContextCreated event JSON: %w", err)
}
var world executionWorld
frame, found := fs.manager.getFrameByID(i.FrameID)
if found {
frame, ok := fs.manager.getFrameByID(i.FrameID)
if ok {
if i.IsDefault {
world = mainWorld
} else if event.Context.Name == utilityWorldName && !frame.hasContext(utilityWorld) {
Expand Down Expand Up @@ -763,12 +763,12 @@ func (fs *FrameSession) onFrameNavigated(frame *cdp.Frame, initial bool) {
)

var (
spanID = fs.mainFrameSpan.SpanContext().SpanID().String()
newFrame, found = fs.manager.getFrameByID(frame.ID)
spanID = fs.mainFrameSpan.SpanContext().SpanID().String()
newFrame, ok = fs.manager.getFrameByID(frame.ID)
)

// Only set the k6SpanId reference if it's a new frame.
if !found {
if !ok {
return
}

Expand Down Expand Up @@ -854,8 +854,8 @@ func (fs *FrameSession) onPageLifecycle(event *cdppage.EventLifecycleEvent) {
"sid:%v tid:%v fid:%v event:%s eventTime:%q",
fs.session.ID(), fs.targetID, event.FrameID, event.Name, event.Timestamp.Time())

_, found := fs.manager.getFrameByID(event.FrameID)
if !found {
_, ok := fs.manager.getFrameByID(event.FrameID)
if !ok {
return
}

Expand Down Expand Up @@ -956,8 +956,8 @@ func (fs *FrameSession) onAttachedToTarget(event *target.EventAttachedToTarget)

// attachIFrameToTarget attaches an IFrame target to a given session.
func (fs *FrameSession) attachIFrameToTarget(ti *target.Info, sid target.SessionID) error {
fr, found := fs.manager.getFrameByID(cdp.FrameID(ti.TargetID))
if !found {
fr, ok := fs.manager.getFrameByID(cdp.FrameID(ti.TargetID))
if !ok {
// IFrame should be attached to fs.page with EventFrameAttached
// event before.
fs.logger.Debugf("FrameSession:attachIFrameToTarget:return",
Expand Down
6 changes: 3 additions & 3 deletions common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ func (m *NetworkManager) onRequest(event *network.EventRequestWillBeSent, interc
}

var frame *Frame = nil
var found bool
var ok bool
if event.FrameID != "" {
frame, found = m.frameManager.getFrameByID(event.FrameID)
frame, ok = m.frameManager.getFrameByID(event.FrameID)
}
if !found {
if !ok {
m.logger.Debugf("NetworkManager:onRequest", "url:%s method:%s type:%s fid:%s frame is nil",
event.Request.URL, event.Request.Method, event.Initiator.Type, event.FrameID)
}
Expand Down

0 comments on commit 5f754c2

Please sign in to comment.