Getting rid off the encoding legacy. Should think of the better and more general way.
This commit is contained in:
parent
908d235cad
commit
307ebd03e3
5 changed files with 11 additions and 35 deletions
26
tg/action.go
26
tg/action.go
|
@ -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 {
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
}
|
}*/
|
||||||
|
|
Loading…
Reference in a new issue