Remove ErrMsg and ModelAssertionErr

This commit is contained in:
Christian Rocha 2020-04-22 11:00:30 -04:00
parent aa6d766e42
commit f93b752fcc
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
3 changed files with 30 additions and 54 deletions

View File

@ -15,11 +15,12 @@ import (
const url = "https://charm.sh/" const url = "https://charm.sh/"
type Model struct { type Model struct {
Status int status int
Error error err error
} }
type statusMsg int type statusMsg int
type errMsg error
func main() { func main() {
p := tea.NewProgram(initialize, update, view, nil) p := tea.NewProgram(initialize, update, view, nil)
@ -33,7 +34,10 @@ func initialize() (tea.Model, tea.Cmd) {
} }
func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
m, _ := model.(Model) m, ok := model.(Model)
if !ok {
return Model{err: errors.New("could not perform assertion on model during update")}, nil
}
switch msg := msg.(type) { switch msg := msg.(type) {
@ -50,12 +54,11 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
} }
case statusMsg: case statusMsg:
m.Status = int(msg) m.status = int(msg)
return m, tea.Quit return m, tea.Quit
case tea.ErrMsg: case errMsg:
// TODO: get the error out of tea.ErrMsg less hackily m.err = msg
m.Error = errors.New(msg.Error())
return m, nil return m, nil
default: default:
@ -66,10 +69,10 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
func view(model tea.Model) string { func view(model tea.Model) string {
m, _ := model.(Model) m, _ := model.(Model)
s := fmt.Sprintf("Checking %s...", url) s := fmt.Sprintf("Checking %s...", url)
if m.Error != nil { if m.err != nil {
s += fmt.Sprintf("something went wrong: %s", m.Error) s += fmt.Sprintf("something went wrong: %s", m.err)
} else if m.Status != 0 { } else if m.status != 0 {
s += fmt.Sprintf("%d %s", m.Status, http.StatusText(m.Status)) s += fmt.Sprintf("%d %s", m.status, http.StatusText(m.status))
} }
return s return s
} }
@ -80,7 +83,7 @@ func checkServer() tea.Msg {
} }
res, err := c.Get(url) res, err := c.Get(url)
if err != nil { if err != nil {
return tea.NewErrMsg(err.Error()) return errMsg(err)
} }
return statusMsg(res.StatusCode) return statusMsg(res.StatusCode)
} }

View File

@ -12,15 +12,14 @@ import (
) )
type Model struct { type Model struct {
Input input.Model textInput input.Model
Error error err error
} }
type tickMsg struct{} type tickMsg struct{}
type errMsg error
func main() { func main() {
tea.UseSysLog("tea")
p := tea.NewProgram( p := tea.NewProgram(
initialize, initialize,
update, update,
@ -38,8 +37,8 @@ func initialize() (tea.Model, tea.Cmd) {
inputModel.Placeholder = "Pikachu" inputModel.Placeholder = "Pikachu"
return Model{ return Model{
Input: inputModel, textInput: inputModel,
Error: nil, err: nil,
}, nil }, nil
} }
@ -50,8 +49,9 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
// When we encounter errors in Update we simply add the error to the // When we encounter errors in Update we simply add the error to the
// model so we can handle it in the view. We could also return a command // model so we can handle it in the view. We could also return a command
// that does something else with the error, like logs it via IO. // that does something else with the error, like logs it via IO.
m.Error = errors.New("could not perform assertion on model") return Model{
return m, nil err: errors.New("could not perform assertion on model in update"),
}, nil
} }
switch msg := msg.(type) { switch msg := msg.(type) {
@ -64,12 +64,12 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) {
} }
// We handle errors just like any other message // We handle errors just like any other message
case tea.ErrMsg: case errMsg:
m.Error = msg m.err = msg
return m, nil return m, nil
} }
m.Input, cmd = input.Update(msg, m.Input) m.textInput, cmd = input.Update(msg, m.textInput)
return m, cmd return m, cmd
} }
@ -79,7 +79,7 @@ func subscriptions(model tea.Model) tea.Subs {
// it the model it expects. // it the model it expects.
"input": func(model tea.Model) tea.Msg { "input": func(model tea.Model) tea.Msg {
m, _ := model.(Model) m, _ := model.(Model)
return input.Blink(m.Input) return input.Blink(m.textInput)
}, },
} }
} }
@ -88,12 +88,12 @@ func view(model tea.Model) string {
m, ok := model.(Model) m, ok := model.(Model)
if !ok { if !ok {
return "Oh no: could not perform assertion on model." return "Oh no: could not perform assertion on model."
} else if m.Error != nil { } else if m.err != nil {
return fmt.Sprintf("Uh oh: %s", m.Error) return fmt.Sprintf("Uh oh: %s", m.err)
} }
return fmt.Sprintf( return fmt.Sprintf(
"Whats your favorite Pokémon?\n\n%s\n\n%s", "Whats your favorite Pokémon?\n\n%s\n\n%s",
input.View(m.Input), input.View(m.textInput),
"(esc to quit)", "(esc to quit)",
) )
} }

27
tea.go
View File

@ -1,7 +1,6 @@
package tea package tea
import ( import (
"errors"
"io" "io"
"os" "os"
"strings" "strings"
@ -94,32 +93,6 @@ type Program struct {
subscriptions Subscriptions subscriptions Subscriptions
} }
// ErrMsg is just a regular message containing an error. We handle it in Update
// just like a regular message by case switching. Of course, the developer
// could also define her own errors as well.
type ErrMsg struct {
error
}
// String implements String() on the error interface for ErrMsg
func (e ErrMsg) String() string {
return e.Error()
}
// NewErrMsg is a convenience function for creating a generic ErrMsg
func NewErrMsg(s string) ErrMsg {
return ErrMsg{errors.New(s)}
}
// NewErrMsgFromErr is a convenience function for creating an ErrMsg from an
// existing error
func NewErrMsgFromErr(e error) ErrMsg {
return ErrMsg{e}
}
// ModelAssertionErr can be used when a model assertion didn't go as planned
var ModelAssertionErr = NewErrMsg("could not perform assertion on model")
// Quit is a command that tells the program to exit // Quit is a command that tells the program to exit
func Quit() Msg { func Quit() Msg {
return quitMsg{} return quitMsg{}