Skip to content

Commit

Permalink
feat(types): parse mode in generated types (#38)
Browse files Browse the repository at this point in the history
* fix(types): use fmt.Stringer in ParseMode defention
  • Loading branch information
mr-linch authored Jun 28, 2022
1 parent c148211 commit 54b7233
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/quote-bot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func run(ctx context.Context) error {
Description: quoteText(quote.Text),
InputMessageContent: tg.InputTextMessageContent{
MessageText: messageText,
ParseMode: tg.HTML.String(),
ParseMode: tg.HTML,
},
ReplyMarkup: tg.NewInlineKeyboardMarkup(
tg.NewButtonRow(
Expand Down
9 changes: 8 additions & 1 deletion parse_mode.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package tg

import (
"encoding"
"fmt"
"html"
"regexp"
"strings"
)

type ParseMode interface {
String() string
encoding.TextMarshaler
fmt.Stringer

// Change separator for next calls
Sep(v string) ParseMode
Expand Down Expand Up @@ -140,6 +143,10 @@ func (pm parseMode) Line(v ...string) string {
return strings.Join(v, " ")
}

func (pm parseMode) MarshalText() ([]byte, error) {
return []byte(pm.String()), nil
}

func (pm parseMode) String() string {
return pm.name
}
Expand Down
4 changes: 4 additions & 0 deletions parse_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

func TestParseModeHTML(t *testing.T) {
assert.Equal(t, "HTML", HTML.String())
marshaled, err := HTML.MarshalText()
assert.NoError(t, err)
assert.Equal(t, "HTML", string(marshaled))

assert.Equal(t, "Hello World", HTML.Line("Hello", "World"))
assert.Equal(t, "Hello\nWorld", HTML.Text("Hello", "World"))
assert.Equal(t, "<b>Hello World</b>", HTML.Bold("Hello", "World"))
Expand Down
40 changes: 20 additions & 20 deletions types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 54b7233

Please sign in to comment.