layout: extend config implementation

pull/341/head
Demian 4 years ago
parent 6005ff95b1
commit a2ad0d803c

@ -0,0 +1,57 @@
package layout
import (
"time"
"github.com/goccy/go-yaml"
"github.com/spf13/cast"
tele "gopkg.in/tucnak/telebot.v3"
)
type Config struct {
v map[string]interface{}
}
func (c *Config) Unmarshal(v interface{}) error {
data, err := yaml.Marshal(c.v)
if err != nil {
return err
}
return yaml.Unmarshal(data, c.v)
}
func (c *Config) Get(k string) *Config {
v, ok := c.v[k].(map[string]interface{})
if !ok {
return nil
}
return &Config{v: v}
}
func (c *Config) String(k string) string {
return cast.ToString(c.v[k])
}
func (c *Config) Int(k string) int {
return cast.ToInt(c.v[k])
}
func (c *Config) Int64(k string) int64 {
return cast.ToInt64(c.v[k])
}
func (c *Config) Float(k string) float64 {
return cast.ToFloat64(c.v[k])
}
func (c *Config) Bool(k string) bool {
return cast.ToBool(c.v[k])
}
func (c *Config) Duration(k string) time.Duration {
return cast.ToDuration(c.v[k])
}
func (c *Config) ChatID(k string) tele.ChatID {
return tele.ChatID(c.Int64(k))
}

@ -2,14 +2,11 @@ package layout
import (
"bytes"
"fmt"
"io/ioutil"
"sync"
"text/template"
"time"
"github.com/goccy/go-yaml"
"github.com/spf13/cast"
tele "gopkg.in/tucnak/telebot.v3"
)
@ -20,10 +17,11 @@ type (
ctxs map[tele.Context]string
funcs template.FuncMap
config map[string]interface{}
buttons map[string]Button
markups map[string]Markup
locales map[string]*template.Template
*Config
}
Button = tele.Btn
@ -55,7 +53,7 @@ func New(path string) (*Layout, error) {
}
// Built-in blank and helper functions
lt.funcs["config"] = lt.Get
lt.funcs["config"] = lt.String
lt.funcs["locale"] = func() string { return "" }
lt.funcs["text"] = func(k string) string { return "" }
@ -81,30 +79,6 @@ func (lt *Layout) Settings() tele.Settings {
return *lt.pref
}
func (lt *Layout) Get(k string) string {
return fmt.Sprint(lt.config[k])
}
func (lt *Layout) Int(k string) int {
return cast.ToInt(lt.config[k])
}
func (lt *Layout) Int64(k string) int64 {
return cast.ToInt64(lt.config[k])
}
func (lt *Layout) Float(k string) float64 {
return cast.ToFloat64(lt.config[k])
}
func (lt *Layout) Duration(k string) time.Duration {
return cast.ToDuration(lt.config[k])
}
func (lt *Layout) ChatID(k string) tele.ChatID {
return tele.ChatID(lt.Int64(k))
}
func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string {
locale, ok := lt.Locale(c)
if !ok {

@ -22,7 +22,7 @@ func TestLayout(t *testing.T) {
assert.Equal(t, "html", pref.ParseMode)
assert.Equal(t, &tele.LongPoller{}, pref.Poller)
assert.Equal(t, "string", lt.Get("str"))
assert.Equal(t, "string", lt.String("str"))
assert.Equal(t, 123, lt.Int("num"))
assert.Equal(t, int64(123), lt.Int64("num"))
assert.Equal(t, float64(123), lt.Float("num"))

@ -12,20 +12,18 @@ import (
tele "gopkg.in/tucnak/telebot.v3"
)
type (
Settings struct {
URL string
Token string
Updates int
LocalesDir string `json:"locales_dir"`
TokenEnv string `json:"token_env"`
ParseMode string `json:"parse_mode"`
Webhook *tele.Webhook `json:"webhook"`
LongPoller *tele.LongPoller `json:"long_poller"`
}
)
type Settings struct {
URL string
Token string
Updates int
LocalesDir string `json:"locales_dir"`
TokenEnv string `json:"token_env"`
ParseMode string `json:"parse_mode"`
Webhook *tele.Webhook `json:"webhook"`
LongPoller *tele.LongPoller `json:"long_poller"`
}
func (lt *Layout) UnmarshalYAML(data []byte) error {
var aux struct {
@ -39,7 +37,9 @@ func (lt *Layout) UnmarshalYAML(data []byte) error {
return err
}
lt.config = aux.Config
lt.Config = &Config{
v: aux.Config,
}
if pref := aux.Settings; pref != nil {
lt.pref = &tele.Settings{

Loading…
Cancel
Save