Skip to content

Commit

Permalink
refactor(message): simplify styling api
Browse files Browse the repository at this point in the history
Use single variadic param everywhere.
This allows using v... instead of (v[0], v[1:]...) and
reduces signature.
  • Loading branch information
ernado committed Apr 13, 2021
1 parent 45b710e commit e03ad3a
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 35 deletions.
4 changes: 2 additions & 2 deletions telegram/message/draft.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ func (b *Builder) SaveDraft(ctx context.Context, msg string) error {
// SaveStyledDraft saves given styled message as draft.
//
// See https://core.telegram.org/api/drafts#saving-drafts.
func (b *Builder) SaveStyledDraft(ctx context.Context, text StyledTextOption, texts ...StyledTextOption) error {
func (b *Builder) SaveStyledDraft(ctx context.Context, texts ...StyledTextOption) error {
p, err := b.peer(ctx)
if err != nil {
return xerrors.Errorf("peer: %w", err)
}

tb := entity.Builder{}
if err := styling.Perform(&tb, text, texts...); err != nil {
if err := styling.Perform(&tb, texts...); err != nil {
return err
}
msg, entities := tb.Complete()
Expand Down
7 changes: 2 additions & 5 deletions telegram/message/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,14 @@ func (b *EditMessageBuilder) Textf(ctx context.Context, format string, args ...i
}

// StyledText edits message using given message.
func (b *EditMessageBuilder) StyledText(
ctx context.Context,
text StyledTextOption, texts ...StyledTextOption,
) (tg.UpdatesClass, error) {
func (b *EditMessageBuilder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) {
p, err := b.builder.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}

tb := entity.Builder{}
if err := styling.Perform(&tb, text, texts...); err != nil {
if err := styling.Perform(&tb, texts...); err != nil {
return nil, err
}
msg, entities := tb.Complete()
Expand Down
6 changes: 2 additions & 4 deletions telegram/message/inline/message_auto_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
// MessageMediaAutoBuilder is a builder of inline result text message.
type MessageMediaAutoBuilder struct {
message *tg.InputBotInlineMessageMediaAuto
option styling.StyledTextOption
options []styling.StyledTextOption
}

func (b *MessageMediaAutoBuilder) apply() (tg.InputBotInlineMessageClass, error) {
tb := entity.Builder{}
if err := styling.Perform(&tb, b.option, b.options...); err != nil {
if err := styling.Perform(&tb, b.options...); err != nil {
return nil, err
}
msg, entities := tb.Complete()
Expand All @@ -33,10 +32,9 @@ func MediaAuto(msg string) *MessageMediaAutoBuilder {
}

// MediaAutoStyled creates new message text option builder.
func MediaAutoStyled(text styling.StyledTextOption, texts ...styling.StyledTextOption) *MessageMediaAutoBuilder {
func MediaAutoStyled(texts ...styling.StyledTextOption) *MessageMediaAutoBuilder {
return &MessageMediaAutoBuilder{
message: &tg.InputBotInlineMessageMediaAuto{},
option: text,
options: texts,
}
}
Expand Down
6 changes: 2 additions & 4 deletions telegram/message/inline/message_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ import (
// MessageTextBuilder is a builder of inline result text message.
type MessageTextBuilder struct {
message *tg.InputBotInlineMessageText
option styling.StyledTextOption
options []styling.StyledTextOption
}

func (b *MessageTextBuilder) apply() (tg.InputBotInlineMessageClass, error) {
tb := entity.Builder{}
if err := styling.Perform(&tb, b.option, b.options...); err != nil {
if err := styling.Perform(&tb, b.options...); err != nil {
return nil, err
}
msg, entities := tb.Complete()
Expand All @@ -33,10 +32,9 @@ func MessageText(msg string) *MessageTextBuilder {
}

// MessageStyledText creates new message text option builder.
func MessageStyledText(text styling.StyledTextOption, texts ...styling.StyledTextOption) *MessageTextBuilder {
func MessageStyledText(texts ...styling.StyledTextOption) *MessageTextBuilder {
return &MessageTextBuilder{
message: &tg.InputBotInlineMessageText{},
option: text,
options: texts,
}
}
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/media.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func performTextOptions(media *tg.InputSingleMedia, opts []StyledTextOption) error {
if len(opts) > 0 {
tb := entity.Builder{}
if err := styling.Perform(&tb, opts[0], opts[1:]...); err != nil {
if err := styling.Perform(&tb, opts...); err != nil {
return err
}
media.Message, media.Entities = tb.Complete()
Expand Down
9 changes: 3 additions & 6 deletions telegram/message/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func CorrectPollAnswer(text string) PollAnswerOption {
type PollBuilder struct {
input tg.InputMediaPoll
answers []PollAnswerOption
opt StyledTextOption
opts []StyledTextOption
}

Expand Down Expand Up @@ -118,8 +117,7 @@ func (p *PollBuilder) Explanation(msg string) *PollBuilder {
}

// StyledExplanation sets styled explanation message.
func (p *PollBuilder) StyledExplanation(text StyledTextOption, texts ...StyledTextOption) *PollBuilder {
p.opt = text
func (p *PollBuilder) StyledExplanation(texts ...StyledTextOption) *PollBuilder {
p.opts = texts
return p
}
Expand All @@ -134,10 +132,9 @@ func (p *PollBuilder) apply(ctx context.Context, b *multiMediaBuilder) error {
p.input.Poll.ID = id
}

// Check if p.opt is not zero.
if !p.opt.Zero() {
if len(p.opts) > 0 {
tb := entity.Builder{}
if err := styling.Perform(&tb, p.opt, p.opts...); err != nil {
if err := styling.Perform(&tb, p.opts...); err != nil {
return err
}
p.input.Solution, p.input.SolutionEntities = tb.Complete()
Expand Down
2 changes: 1 addition & 1 deletion telegram/message/style_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func sendStyledText(ctx context.Context) error {
}

_, err := message.NewSender(tg.NewClient(client)).
Self().StyledText(ctx, formats[0], formats[1:]...)
Self().StyledText(ctx, formats...)
return err
})
}
Expand Down
13 changes: 5 additions & 8 deletions telegram/message/styling/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ type textBuilder struct {
*entity.Builder
}

func (b *textBuilder) Perform(text StyledTextOption, texts ...StyledTextOption) error {
b.GrowEntities(len(texts) + 1)
length := text.size
func (b *textBuilder) Perform(texts ...StyledTextOption) error {
b.GrowEntities(len(texts))
var length int
for i := range texts {
length += texts[i].size
}
b.GrowText(length)

if err := text.perform(b); err != nil {
return xerrors.Errorf("perform first styling option: %w", err)
}
for idx, opt := range texts {
if err := opt.perform(b); err != nil {
return xerrors.Errorf("perform %d styling option: %w", idx+2, err)
Expand All @@ -31,7 +28,7 @@ func (b *textBuilder) Perform(text StyledTextOption, texts ...StyledTextOption)
}

// Perform performs all options to the given builder.
func Perform(builder *entity.Builder, text StyledTextOption, texts ...StyledTextOption) error {
func Perform(builder *entity.Builder, texts ...StyledTextOption) error {
tb := &textBuilder{Builder: builder}
return tb.Perform(text, texts...)
return tb.Perform(texts...)
}
6 changes: 2 additions & 4 deletions telegram/message/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@ func (b *Builder) Textf(ctx context.Context, format string, args ...interface{})
}

// StyledText sends styled text message.
func (b *Builder) StyledText(
ctx context.Context, text StyledTextOption, texts ...StyledTextOption,
) (tg.UpdatesClass, error) {
func (b *Builder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}

tb := entity.Builder{}
if err := styling.Perform(&tb, text, texts...); err != nil {
if err := styling.Perform(&tb, texts...); err != nil {
return nil, err
}
msg, entities := tb.Complete()
Expand Down

0 comments on commit e03ad3a

Please sign in to comment.