Skip to content

Commit

Permalink
layout: implement NewFromFS
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Mar 16, 2024
1 parent 9eb5343 commit 35d5dd3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module gopkg.in/telebot.v3

go 1.13
go 1.16

require (
github.com/goccy/go-yaml v1.9.5
Expand Down
28 changes: 20 additions & 8 deletions layout/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package layout
import (
"bytes"
"encoding/json"
"io/ioutil"
"io/fs"
"log"
"os"
"strings"
"sync"
"text/template"
Expand Down Expand Up @@ -69,7 +70,13 @@ type (

// New parses the given layout file.
func New(path string, funcs ...template.FuncMap) (*Layout, error) {
data, err := ioutil.ReadFile(path)
return NewFromFS(os.DirFS("."), path, funcs...)
}

// NewFromFS parses the layout from the given fs.FS. It allows to read layout
// from the go:embed filesystem.
func NewFromFS(fsys fs.FS, path string, funcs ...template.FuncMap) (*Layout, error) {
data, err := fs.ReadFile(fsys, path)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,10 +128,10 @@ var builtinFuncs = template.FuncMap{
// webhook: (or webhook settings)
//
// Usage:
//
// lt, err := layout.New("bot.yml")
// b, err := tele.NewBot(lt.Settings())
// // That's all!
//
func (lt *Layout) Settings() tele.Settings {
if lt.pref == nil {
panic("telebot/layout: settings is empty")
Expand Down Expand Up @@ -183,16 +190,20 @@ func (lt *Layout) Commands() (cmds []tele.Command) {
// used in b.SetCommands later.
//
// Example of bot.yml:
//
// commands:
// /start: '{{ text `cmdStart` }}'
//
// en.yml:
//
// cmdStart: Start the bot
//
// ru.yml:
//
// cmdStart: Запуск бота
//
// Usage:
//
// b.SetCommands(lt.CommandsLocale("en"), "en")
// b.SetCommands(lt.CommandsLocale("ru"), "ru")
func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tele.Command) {
Expand Down Expand Up @@ -226,13 +237,14 @@ func (lt *Layout) CommandsLocale(locale string, args ...interface{}) (cmds []tel
// The given optional argument will be passed to the template engine.
//
// Example of en.yml:
//
// start: Hi, {{.FirstName}}!
//
// Usage:
//
// func onStart(c tele.Context) error {
// return c.Send(lt.Text(c, "start", c.Sender()))
// }
//
func (lt *Layout) Text(c tele.Context, k string, args ...interface{}) string {
locale, ok := lt.Locale(c)
if !ok {
Expand Down Expand Up @@ -266,9 +278,9 @@ func (lt *Layout) TextLocale(locale, k string, args ...interface{}) string {
// Callback returns a callback endpoint used to handle buttons.
//
// Example:
//
// // Handling settings button
// b.Handle(lt.Callback("settings"), onSettings)
//
func (lt *Layout) Callback(k string) tele.CallbackEndpoint {
btn, ok := lt.buttons[k]
if !ok {
Expand All @@ -287,6 +299,7 @@ func (lt *Layout) Callback(k string) tele.CallbackEndpoint {
// text: Item #{{.Number}}
//
// Usage:
//
// btns := make([]tele.Btn, len(items))
// for i, item := range items {
// btns[i] = lt.Button(c, "item", struct {
Expand All @@ -301,7 +314,6 @@ func (lt *Layout) Callback(k string) tele.CallbackEndpoint {
// m := b.NewMarkup()
// m.Inline(m.Row(btns...))
// // Your generated markup is ready.
//
func (lt *Layout) Button(c tele.Context, k string, args ...interface{}) *tele.Btn {
locale, ok := lt.Locale(c)
if !ok {
Expand Down Expand Up @@ -360,13 +372,13 @@ func (lt *Layout) ButtonLocale(locale, k string, args ...interface{}) *tele.Btn
// - [settings]
//
// Usage:
//
// func onStart(c tele.Context) error {
// return c.Send(
// lt.Text(c, "start"),
// lt.Markup(c, "menu"),
// )
// }
//
func (lt *Layout) Markup(c tele.Context, k string, args ...interface{}) *tele.ReplyMarkup {
locale, ok := lt.Locale(c)
if !ok {
Expand Down Expand Up @@ -427,6 +439,7 @@ func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.Repl
// thumb_url: '{{ .PreviewURL }}'
//
// Usage:
//
// func onQuery(c tele.Context) error {
// results := make(tele.Results, len(articles))
// for i, article := range articles {
Expand All @@ -437,7 +450,6 @@ func (lt *Layout) MarkupLocale(locale, k string, args ...interface{}) *tele.Repl
// CacheTime: 100,
// })
// }
//
func (lt *Layout) Result(c tele.Context, k string, args ...interface{}) tele.Result {
locale, ok := lt.Locale(c)
if !ok {
Expand Down
10 changes: 10 additions & 0 deletions layout/layout_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package layout

import (
"embed"
"os"
"testing"
"time"
Expand All @@ -9,6 +10,9 @@ import (
tele "gopkg.in/telebot.v3"
)

//go:embed *
var fsys embed.FS

func TestLayout(t *testing.T) {
os.Setenv("TOKEN", "TEST")

Expand All @@ -17,10 +21,16 @@ func TestLayout(t *testing.T) {
t.Fatal(err)
}

ltfs, err := NewFromFS(fsys, "example.yml")
if err != nil {
t.Fatal(err)
}

pref := lt.Settings()
assert.Equal(t, "TEST", pref.Token)
assert.Equal(t, "html", pref.ParseMode)
assert.Equal(t, &tele.LongPoller{}, pref.Poller)
assert.Equal(t, pref, ltfs.Settings())

assert.ElementsMatch(t, []tele.Command{{
Text: "start",
Expand Down

0 comments on commit 35d5dd3

Please sign in to comment.