Getting rid off the encoding legacy. Should think of the better and more general way.

This commit is contained in:
Andrey Parhomenko 2023-09-21 20:32:24 +03:00
parent 908d235cad
commit 307ebd03e3
5 changed files with 11 additions and 35 deletions

View file

@ -1,33 +1,9 @@
package tg
import (
"reflect"
//"reflect"
)
// Jsonable Action.
type action struct {
Type string
Action Action
}
func newAction(a Action) *action {
typ, ok := actionMapByReflect[reflect.TypeOf(a)]
if !ok {
panic(ActionNotDefinedErr)
}
return &action{
Type: typ,
Action: a,
}
}
func (a *action) Act(c *Context) {
if a != nil && a.Action != nil {
a.Action.Act(c)
}
}
// The argument for handling in channenl behaviours.
type ChannelContext struct {
}

View file

@ -6,7 +6,7 @@ package tg
// The type describes behaviour for the bot in personal chats.
type Behaviour struct {
Root Widget
Init *action
Init Action
Screens ScreenMap
}
@ -26,7 +26,7 @@ func (b *Behaviour) WithRoot(root Widget) *Behaviour {
// The Action will be called on session creation,
// not when starting or restarting the bot with the Start Action.
func (b *Behaviour) WithInit(a Action) *Behaviour {
b.Init = newAction(a)
b.Init = a
return b
}

View file

@ -10,7 +10,7 @@ type Button struct {
Data string
Url string
SendLocation bool
Action *action
Action Action
}
type ButtonMap map[string]*Button
@ -44,7 +44,7 @@ func (btn *Button) WithUrl(url string) *Button {
// Set the action when pressing the button.
// By default is nil and does nothing.
func (btn *Button) WithAction(a Action) *Button {
btn.Action = newAction(a)
btn.Action = a
return btn
}

View file

@ -15,7 +15,7 @@ type CommandName string
type Command struct {
Name CommandName
Description string
Action *action
Action Action
Widget Widget
}
type CommandMap map[CommandName]*Command
@ -27,7 +27,7 @@ func NewCommand(name CommandName) *Command {
}
func (c *Command) WithAction(a Action) *Command {
c.Action = newAction(a)
c.Action = a
return c
}

View file

@ -1,11 +1,11 @@
package tg
import (
"encoding/json"
"reflect"
//"encoding/json"
//"reflect"
)
func (a *action) UnmarshalJSON(data []byte) error {
/*func (a *action) UnmarshalJSON(data []byte) error {
var err error
m := make(map[string]any)
err = json.Unmarshal(data, &m)
@ -31,4 +31,4 @@ func (a *action) UnmarshalJSON(data []byte) error {
a.Action = vr
return nil
}
}*/