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 package tg
import ( 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. // The argument for handling in channenl behaviours.
type ChannelContext struct { type ChannelContext struct {
} }

View file

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

View file

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

View file

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

View file

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