Skip to content

Commit

Permalink
all: fix forcetypeasert lint errors (add giu.Assert method)
Browse files Browse the repository at this point in the history
  • Loading branch information
gucio321 committed Sep 29, 2021
1 parent 50bb713 commit 0c43040
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ linters:
- errcheck
- errorlint
- exportloopref
#- forcetypeassert
- forcetypeassert
#- funlen
- gci
#- godot
Expand Down
4 changes: 3 additions & 1 deletion CodeEditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion EventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion Msgbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion ProgressIndicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion SplitLayout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image/color"
"image/draw"
"image/png"
"log"
"os"
"path/filepath"

Expand Down Expand Up @@ -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...))
}
24 changes: 18 additions & 6 deletions Widgets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions Window.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down

0 comments on commit 0c43040

Please sign in to comment.