From 0c43040b875ec56c4f4cb343f729fab23e92af4c Mon Sep 17 00:00:00 2001 From: gucio321 Date: Wed, 29 Sep 2021 20:19:19 +0200 Subject: [PATCH] all: fix forcetypeasert lint errors (add giu.Assert method) --- .golangci.yml | 2 +- CodeEditor.go | 4 +++- EventHandler.go | 4 +++- Msgbox.go | 4 +++- ProgressIndicator.go | 4 +++- SplitLayout.go | 4 +++- Utils.go | 15 +++++++++++++++ Widgets.go | 24 ++++++++++++++++++------ Window.go | 8 ++++---- 9 files changed, 53 insertions(+), 16 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 53f878ee..ff206e57 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -41,7 +41,7 @@ linters: - errcheck - errorlint - exportloopref - #- forcetypeassert + - forcetypeassert #- funlen - gci #- godot diff --git a/CodeEditor.go b/CodeEditor.go index e0dcef8d..af8ffaee 100644 --- a/CodeEditor.go +++ b/CodeEditor.go @@ -206,7 +206,9 @@ func (ce *CodeEditorWidget) getState() (state *codeEditorState) { Context.SetState(ce.title, state) } else { - state = s.(*codeEditorState) + var isOk bool + state, isOk = s.(*codeEditorState) + Assert(isOk, "CodeEditorWidget", "getState", "unexpected widget's state type") } return state diff --git a/EventHandler.go b/EventHandler.go index 74f6b482..c7398537 100644 --- a/EventHandler.go +++ b/EventHandler.go @@ -116,7 +116,9 @@ func (eh *EventHandler) Build() { var state *eventHandlerState stateID := GenAutoID("eventHandlerState") if s := Context.GetState(stateID); s != nil { - state = s.(*eventHandlerState) + var isOk bool + state, isOk = s.(*eventHandlerState) + Assert(isOk, "EventHandler", "Build", "unexpected type of state received") } else { newState := &eventHandlerState{} Context.SetState(stateID, newState) diff --git a/Msgbox.go b/Msgbox.go index aee36675..50a9dd18 100644 --- a/Msgbox.go +++ b/Msgbox.go @@ -109,7 +109,9 @@ func PrepareMsgbox() Layout { state = &msgboxState{title: "Info", content: "Content", buttons: MsgboxButtonsOk, resultCallback: nil, open: false} Context.SetState(msgboxID, state) } else { - state = stateRaw.(*msgboxState) + var isOk bool + state, isOk = stateRaw.(*msgboxState) + Assert(isOk, "MsgboxWidget", "PrepareMsgbox", "got state of unexpected type") } if state.open { diff --git a/ProgressIndicator.go b/ProgressIndicator.go index aee5edae..20c3f23e 100644 --- a/ProgressIndicator.go +++ b/ProgressIndicator.go @@ -68,7 +68,9 @@ func (p *ProgressIndicatorWidget) Build() { Context.SetState(p.internalID, &ps) go ps.update() } else { - state := s.(*progressIndicatorState) + var isOk bool + state, isOk := s.(*progressIndicatorState) + Assert(isOk, "ProgressIndicatorWidget", "Build", "got unexpected type of widget's sate") child := Child().Border(false).Size(p.width, p.height).Layout(Layout{ Custom(func() { diff --git a/SplitLayout.go b/SplitLayout.go index 3572209e..4f79385e 100644 --- a/SplitLayout.go +++ b/SplitLayout.go @@ -96,7 +96,9 @@ func (s *SplitLayoutWidget) Build() { splitLayoutState = &SplitLayoutState{delta: 0.0, sashPos: s.sashPos} Context.SetState(stateID, splitLayoutState) } else { - splitLayoutState = state.(*SplitLayoutState) + var isOk bool + splitLayoutState, isOk = state.(*SplitLayoutState) + Assert(isOk, "SplitLayoutWidget", "Build", "got unexpected type of widget's state") } itemSpacingX, itemSpacingY := GetItemInnerSpacing() diff --git a/Utils.go b/Utils.go index 6ee32f89..78c56781 100644 --- a/Utils.go +++ b/Utils.go @@ -6,6 +6,7 @@ import ( "image/color" "image/draw" "image/png" + "log" "os" "path/filepath" @@ -183,3 +184,17 @@ func PushClipRect(clipRectMin, clipRectMax image.Point, intersectWithClipRect bo func PopClipRect() { imgui.PopClipRect() } + +func Assert(cond bool, t, method, msg string, args ...interface{}) { + if !cond { + fatal(t, method, msg, args...) + } +} + +func fatal(widgetName, method, message string, args ...interface{}) { + if widgetName != "" { + widgetName = fmt.Sprintf("(*%s)", widgetName) + } + + log.Panicf("giu: %s.%s: %s", widgetName, method, fmt.Sprintf(message, args...)) +} diff --git a/Widgets.go b/Widgets.go index c761c1b7..150a5dad 100644 --- a/Widgets.go +++ b/Widgets.go @@ -454,7 +454,9 @@ func (b *ImageButtonWithRgbaWidget) Build() { Context.SetState(b.id, &ImageState{texture: tex}) }) } else { - imgState := state.(*ImageState) + var isOk bool + imgState, isOk := state.(*ImageState) + Assert(isOk, "ImageButtonWithRgbaWidget", "Build", "got unexpected type of widget's state") b.ImageButtonWidget.texture = imgState.texture } @@ -905,7 +907,9 @@ func (i *ImageWithRgbaWidget) Build() { imgState.texture = tex }) } else { - imgState = state.(*ImageState) + var isOk bool + imgState, isOk = state.(*ImageState) + Assert(isOk, "ImageWithRgbaWidget", "Build", "unexpected type of widget's state recovered") } i.img.texture = imgState.texture @@ -954,7 +958,9 @@ func (i *ImageWithFileWidget) Build() { }) } } else { - imgState = state.(*ImageState) + var isOk bool + imgState, isOk = state.(*ImageState) + Assert(isOk, "ImageWithFileWidget", "Build", "wrong type of widget's state got") } i.img.texture = imgState.texture @@ -1076,7 +1082,9 @@ func (i *ImageWithURLWidget) Build() { } }() } else { - imgState = state.(*ImageState) + var isOk bool + imgState, isOk = state.(*ImageState) + Assert(isOk, "ImageWithURLWidget", "Build", "wrong type of widget's state recovered.") } switch { @@ -1172,7 +1180,9 @@ func (i *InputTextWidget) Build() { state = &inputTextState{} Context.SetState(i.label, state) } else { - state = s.(*inputTextState) + var isOk bool + state, isOk = s.(*inputTextState) + Assert(isOk, "InputTextWidget", "Build", "wrong state type recovered.") } if i.width != 0 { @@ -2762,7 +2772,9 @@ func (l *ListBoxWidget) Build() { state = &ListBoxState{selectedIndex: 0} Context.SetState(l.id, state) } else { - state = s.(*ListBoxState) + var isOk bool + state, isOk = s.(*ListBoxState) + Assert(isOk, "ListBoxWidget", "Build", "wrong state type recovered") } child := Child().Border(l.border).Size(l.width, l.height).Layout(Layout{ diff --git a/Window.go b/Window.go index e54ac2c4..759dc0a1 100644 --- a/Window.go +++ b/Window.go @@ -187,10 +187,10 @@ func (w *WindowWidget) getStateID() string { // returns window state func (w *WindowWidget) getState() (state *windowState) { - s := Context.GetState(w.getStateID()) - - if s != nil { - state = s.(*windowState) + if s := Context.GetState(w.getStateID()); s != nil { + var isOk bool + state, isOk = s.(*windowState) + Assert(isOk, "WindowWidget", "getState", "unexpected state recovered.") } else { state = &windowState{}