Skip to content

Commit

Permalink
Don't panic in property accessors if type assertion fails
Browse files Browse the repository at this point in the history
  • Loading branch information
lxn committed Dec 5, 2018
1 parent 7275c6e commit b92ba24
Show file tree
Hide file tree
Showing 20 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion button.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (b *Button) init() {
return b.Text()
},
func(v interface{}) error {
return b.SetText(v.(string))
return b.SetText(assertStringOr(v, ""))
},
b.textChangedPublisher.Event()))
}
Expand Down
2 changes: 1 addition & 1 deletion checkbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewCheckBox(parent Container) (*CheckBox, error) {
return cb.CheckState()
},
func(v interface{}) error {
cb.SetCheckState(v.(CheckState))
cb.SetCheckState(CheckState(assertIntOr(v, 0)))

return nil
},
Expand Down
6 changes: 3 additions & 3 deletions combobox.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func newComboBoxWithStyle(parent Container, style uint32) (*ComboBox, error) {
return cb.CurrentIndex()
},
func(v interface{}) error {
return cb.SetCurrentIndex(v.(int))
return cb.SetCurrentIndex(assertIntOr(v, -1))
},
cb.CurrentIndexChanged()))

Expand All @@ -151,7 +151,7 @@ func newComboBoxWithStyle(parent Container, style uint32) (*ComboBox, error) {
return cb.Text()
},
func(v interface{}) error {
return cb.SetText(v.(string))
return cb.SetText(assertStringOr(v, ""))
},
event))

Expand Down Expand Up @@ -183,7 +183,7 @@ func newComboBoxWithStyle(parent Container, style uint32) (*ComboBox, error) {
},
func(v interface{}) error {
if cb.Editable() {
return cb.SetText(v.(string))
return cb.SetText(assertStringOr(v, ""))
}

if cb.bindingValueProvider == nil {
Expand Down
2 changes: 1 addition & 1 deletion dateedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func newDateEdit(parent Container, style uint32) (*DateEdit, error) {
return de.Date()
},
func(v interface{}) error {
return de.SetDate(v.(time.Time))
return de.SetDate(assertTimeOr(v, time.Time{}))
},
de.dateChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion form.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (fb *FormBase) init(form Form) error {
return fb.Title()
},
func(v interface{}) error {
return fb.SetTitle(v.(string))
return fb.SetTitle(assertStringOr(v, ""))
},
fb.titleChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion groupbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewGroupBox(parent Container) (*GroupBox, error) {
return gb.Title()
},
func(v interface{}) error {
return gb.SetTitle(v.(string))
return gb.SetTitle(assertStringOr(v, ""))
},
gb.titleChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion imageview.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func NewImageView(parent Container) (*ImageView, error) {
return iv.Margin()
},
func(v interface{}) error {
return iv.SetMargin(v.(int))
return iv.SetMargin(assertIntOr(v, 0))
},
iv.MarginChanged()))

Expand Down
2 changes: 1 addition & 1 deletion label.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewLabelWithStyle(parent Container, style uint32) (*Label, error) {
return l.Text()
},
func(v interface{}) error {
return l.SetText(v.(string))
return l.setText(assertStringOr(v, ""))
},
l.textChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion lineedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func newLineEdit(parent Window) (*LineEdit, error) {
return le.Text()
},
func(v interface{}) error {
return le.SetText(v.(string))
return le.SetText(assertStringOr(v, ""))
},
le.textChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion linklabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewLinkLabel(parent Container) (*LinkLabel, error) {
return ll.Text()
},
func(v interface{}) error {
return ll.SetText(v.(string))
return ll.SetText(assertStringOr(v, ""))
},
ll.textChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion listbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewListBoxWithStyle(parent Container, style uint32) (*ListBox, error) {
return lb.CurrentIndex()
},
func(v interface{}) error {
return lb.SetCurrentIndex(v.(int))
return lb.SetCurrentIndex(assertIntOr(v, -1))
},
lb.CurrentIndexChanged()))

Expand Down
6 changes: 3 additions & 3 deletions numberedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func NewNumberEdit(parent Container) (*NumberEdit, error) {
return ne.Prefix()
},
func(v interface{}) error {
return ne.SetPrefix(v.(string))
return ne.SetPrefix(assertStringOr(v, ""))
},
ne.prefixChangedPublisher.Event()))

Expand All @@ -89,7 +89,7 @@ func NewNumberEdit(parent Container) (*NumberEdit, error) {
return ne.Suffix()
},
func(v interface{}) error {
return ne.SetSuffix(v.(string))
return ne.SetSuffix(assertStringOr(v, ""))
},
ne.suffixChangedPublisher.Event()))

Expand All @@ -98,7 +98,7 @@ func NewNumberEdit(parent Container) (*NumberEdit, error) {
return ne.Value()
},
func(v interface{}) error {
return ne.SetValue(v.(float64))
return ne.SetValue(assertFloat64Or(v, 0.0))
},
ne.edit.valueChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion slider.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func NewSliderWithOrientation(parent Container, orientation Orientation) (*Slide
return sl.Value()
},
func(v interface{}) error {
sl.SetValue(v.(int))
sl.SetValue(assertIntOr(v, 0))
return nil
},
sl.valueChangedPublisher.Event()))
Expand Down
2 changes: 1 addition & 1 deletion tableview.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func NewTableViewWithCfg(parent Container, cfg *TableViewCfg) (*TableView, error
return tv.CurrentIndex()
},
func(v interface{}) error {
return tv.SetCurrentIndex(v.(int))
return tv.SetCurrentIndex(assertIntOr(v, -1))
},
tv.CurrentIndexChanged()))

Expand Down
5 changes: 3 additions & 2 deletions tabpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
package walk

import (
"github.com/lxn/win"
"strconv"

"github.com/lxn/win"
)

const tabPageWindowClass = `\o/ Walk_TabPage_Class \o/`
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewTabPage() (*TabPage, error) {
return tp.Title()
},
func(v interface{}) error {
return tp.SetTitle(v.(string))
return tp.SetTitle(assertStringOr(v, ""))
},
tp.titleChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion tabwidget.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func NewTabWidget(parent Container) (*TabWidget, error) {
return tw.CurrentIndex()
},
func(v interface{}) error {
return tw.SetCurrentIndex(v.(int))
return tw.SetCurrentIndex(assertIntOr(v, -1))
},
tw.CurrentIndexChanged()))

Expand Down
2 changes: 1 addition & 1 deletion textedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func NewTextEditWithStyle(parent Container, style uint32) (*TextEdit, error) {
return te.Text()
},
func(v interface{}) error {
return te.SetText(v.(string))
return te.SetText(assertStringOr(v, ""))
},
te.textChangedPublisher.Event()))

Expand Down
32 changes: 32 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,38 @@ func uint16RemoveUint16(s []uint16, v uint16) []uint16 {
return ret
}

func assertFloat64Or(value interface{}, defaultValue float64) float64 {
if f, ok := value.(float64); ok {
return f
}

return defaultValue
}

func assertIntOr(value interface{}, defaultValue int) int {
if n, ok := value.(int); ok {
return n
}

return defaultValue
}

func assertStringOr(value interface{}, defaultValue string) string {
if s, ok := value.(string); ok {
return s
}

return defaultValue
}

func assertTimeOr(value interface{}, defaultValue time.Time) time.Time {
if t, ok := value.(time.Time); ok {
return t
}

return defaultValue
}

func ParseFloat(s string) (float64, error) {
s = strings.TrimSpace(s)

Expand Down
2 changes: 1 addition & 1 deletion webview.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func NewWebView(parent Container) (*WebView, error) {
return url
},
func(v interface{}) error {
return wv.SetURL(v.(string))
return wv.SetURL(assertStringOr(v, ""))
},
wv.urlChangedPublisher.Event()))

Expand Down
2 changes: 1 addition & 1 deletion widget.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (wb *WidgetBase) init(widget Widget) error {
return wb.window.(Widget).ToolTipText()
},
func(v interface{}) error {
wb.window.(Widget).SetToolTipText(v.(string))
wb.window.(Widget).SetToolTipText(assertStringOr(v, ""))
return nil
},
wb.toolTipTextChangedPublisher.Event())
Expand Down

0 comments on commit b92ba24

Please sign in to comment.