Skip to content

Commit 689bcd4

Browse files
authored
Merge pull request #26 from aliwatters/refactor/issue-11
refactor: consolidate Gmail inline helpers
2 parents 596e02e + b4e3bbc commit 689bcd4

File tree

5 files changed

+44
-142
lines changed

5 files changed

+44
-142
lines changed

internal/gmail/gmail_helpers.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ import (
1414
// emailContentType is the default Content-Type header for plain text emails.
1515
const emailContentType = "Content-Type: text/plain; charset=\"UTF-8\""
1616

17+
// parseBodyFormat parses the "body_format" argument from a request and returns the
18+
// corresponding BodyFormat. Defaults to BodyFormatText if not specified.
19+
func parseBodyFormat(args map[string]any) BodyFormat {
20+
bf := common.ParseStringArg(args, "body_format", "")
21+
switch bf {
22+
case "html":
23+
return BodyFormatHTML
24+
case "full":
25+
return BodyFormatFull
26+
default:
27+
return BodyFormatText
28+
}
29+
}
30+
31+
// extractAddRemoveLabels extracts "add_labels" and "remove_labels" string arrays
32+
// from request arguments using extractStringArray.
33+
func extractAddRemoveLabels(args map[string]any) (addLabels, removeLabels []string) {
34+
addLabels = extractStringArray(args["add_labels"])
35+
removeLabels = extractStringArray(args["remove_labels"])
36+
return
37+
}
38+
39+
// buildMessageFromArgs builds a gmail.Message with a raw RFC 2822 body from common
40+
// email arguments (to, subject, body, cc, bcc).
41+
func buildMessageFromArgs(args map[string]any) *gmail.Message {
42+
raw := buildEmailMessage(EmailMessage{
43+
To: common.ParseStringArg(args, "to", ""),
44+
Cc: common.ParseStringArg(args, "cc", ""),
45+
Bcc: common.ParseStringArg(args, "bcc", ""),
46+
Subject: common.ParseStringArg(args, "subject", ""),
47+
Body: common.ParseStringArg(args, "body", ""),
48+
})
49+
return &gmail.Message{Raw: raw}
50+
}
51+
1752
// EmailMessage holds the components for building an RFC 2822 email message.
1853
type EmailMessage struct {
1954
To string

internal/gmail/testable_drafts.go

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,8 @@ func TestableGmailUpdateDraft(ctx context.Context, request mcp.CallToolRequest,
9393
return errResult, nil
9494
}
9595

96-
to := common.ParseStringArg(request.Params.Arguments, "to", "")
97-
subject := common.ParseStringArg(request.Params.Arguments, "subject", "")
98-
body := common.ParseStringArg(request.Params.Arguments, "body", "")
99-
cc := common.ParseStringArg(request.Params.Arguments, "cc", "")
100-
bcc := common.ParseStringArg(request.Params.Arguments, "bcc", "")
101-
102-
raw := buildEmailMessage(EmailMessage{
103-
To: to,
104-
Cc: cc,
105-
Bcc: bcc,
106-
Subject: subject,
107-
Body: body,
108-
})
109-
110-
message := &gmail.Message{
111-
Raw: raw,
112-
}
113-
11496
draft := &gmail.Draft{
115-
Message: message,
97+
Message: buildMessageFromArgs(request.Params.Arguments),
11698
}
11799

118100
updated, err := svc.UpdateDraft(ctx, draftID, draft)
@@ -193,23 +175,7 @@ func TestableGmailDraft(ctx context.Context, request mcp.CallToolRequest, deps *
193175
return errResult, nil
194176
}
195177

196-
to := common.ParseStringArg(request.Params.Arguments, "to", "")
197-
subject := common.ParseStringArg(request.Params.Arguments, "subject", "")
198-
body := common.ParseStringArg(request.Params.Arguments, "body", "")
199-
cc := common.ParseStringArg(request.Params.Arguments, "cc", "")
200-
bcc := common.ParseStringArg(request.Params.Arguments, "bcc", "")
201-
202-
raw := buildEmailMessage(EmailMessage{
203-
To: to,
204-
Cc: cc,
205-
Bcc: bcc,
206-
Subject: subject,
207-
Body: body,
208-
})
209-
210-
message := &gmail.Message{
211-
Raw: raw,
212-
}
178+
message := buildMessageFromArgs(request.Params.Arguments)
213179

214180
// Support creating draft as reply
215181
if threadID := common.ParseStringArg(request.Params.Arguments, "thread_id", ""); threadID != "" {

internal/gmail/testable_labels.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,7 @@ func TestableGmailModifyMessage(ctx context.Context, request mcp.CallToolRequest
195195
return errResult, nil
196196
}
197197

198-
var addLabels, removeLabels []string
199-
if add, ok := request.Params.Arguments["add_labels"].([]any); ok {
200-
for _, l := range add {
201-
if s, ok := l.(string); ok {
202-
addLabels = append(addLabels, s)
203-
}
204-
}
205-
}
206-
if remove, ok := request.Params.Arguments["remove_labels"].([]any); ok {
207-
for _, l := range remove {
208-
if s, ok := l.(string); ok {
209-
removeLabels = append(removeLabels, s)
210-
}
211-
}
212-
}
213-
198+
addLabels, removeLabels := extractAddRemoveLabels(request.Params.Arguments)
214199
return modifyMessageLabels(ctx, svc, request, addLabels, removeLabels)
215200
}
216201

@@ -226,22 +211,7 @@ func TestableGmailModifyThread(ctx context.Context, request mcp.CallToolRequest,
226211
return errResult, nil
227212
}
228213

229-
var addLabels, removeLabels []string
230-
if add, ok := request.Params.Arguments["add_labels"].([]any); ok {
231-
for _, l := range add {
232-
if s, ok := l.(string); ok {
233-
addLabels = append(addLabels, s)
234-
}
235-
}
236-
}
237-
if remove, ok := request.Params.Arguments["remove_labels"].([]any); ok {
238-
for _, l := range remove {
239-
if s, ok := l.(string); ok {
240-
removeLabels = append(removeLabels, s)
241-
}
242-
}
243-
}
244-
214+
addLabels, removeLabels := extractAddRemoveLabels(request.Params.Arguments)
245215
modifyRequest := &gmail.ModifyThreadRequest{
246216
AddLabelIds: addLabels,
247217
RemoveLabelIds: removeLabels,
@@ -267,22 +237,7 @@ func TestableGmailBatchModify(ctx context.Context, request mcp.CallToolRequest,
267237
return errResult, nil
268238
}
269239

270-
var addLabels, removeLabels []string
271-
if add, ok := request.Params.Arguments["add_labels"].([]any); ok {
272-
for _, l := range add {
273-
if s, ok := l.(string); ok {
274-
addLabels = append(addLabels, s)
275-
}
276-
}
277-
}
278-
if remove, ok := request.Params.Arguments["remove_labels"].([]any); ok {
279-
for _, l := range remove {
280-
if s, ok := l.(string); ok {
281-
removeLabels = append(removeLabels, s)
282-
}
283-
}
284-
}
285-
240+
addLabels, removeLabels := extractAddRemoveLabels(request.Params.Arguments)
286241
req := &gmail.BatchModifyMessagesRequest{
287242
Ids: ids,
288243
AddLabelIds: addLabels,

internal/gmail/testable_messages.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,7 @@ func TestableGmailGetMessage(ctx context.Context, request mcp.CallToolRequest, d
7171
return mcp.NewToolResultError(fmt.Sprintf("Gmail API error: %v", err)), nil
7272
}
7373

74-
// Parse body_format parameter (defaults to "text" for reduced token usage)
75-
bodyFormat := BodyFormatText
76-
if bf := common.ParseStringArg(request.Params.Arguments, "body_format", ""); bf != "" {
77-
switch bf {
78-
case "html":
79-
bodyFormat = BodyFormatHTML
80-
case "full":
81-
bodyFormat = BodyFormatFull
82-
default:
83-
bodyFormat = BodyFormatText
84-
}
85-
}
86-
87-
result := FormatMessageWithOptions(msg, FormatMessageOptions{BodyFormat: bodyFormat})
74+
result := FormatMessageWithOptions(msg, FormatMessageOptions{BodyFormat: parseBodyFormat(request.Params.Arguments)})
8875
return common.MarshalToolResult(result)
8976
}
9077

@@ -109,20 +96,7 @@ func TestableGmailGetMessages(ctx context.Context, request mcp.CallToolRequest,
10996
messages := make([]map[string]any, 0, len(messageIDsRaw))
11097
var errors []string
11198

112-
// Parse body_format parameter (defaults to "text" for reduced token usage)
113-
bodyFormat := BodyFormatText
114-
if bf := common.ParseStringArg(request.Params.Arguments, "body_format", ""); bf != "" {
115-
switch bf {
116-
case "html":
117-
bodyFormat = BodyFormatHTML
118-
case "full":
119-
bodyFormat = BodyFormatFull
120-
default:
121-
bodyFormat = BodyFormatText
122-
}
123-
}
124-
125-
opts := FormatMessageOptions{BodyFormat: bodyFormat}
99+
opts := FormatMessageOptions{BodyFormat: parseBodyFormat(request.Params.Arguments)}
126100
for _, idRaw := range messageIDsRaw {
127101
messageID, ok := idRaw.(string)
128102
if !ok {
@@ -161,22 +135,7 @@ func TestableGmailSend(ctx context.Context, request mcp.CallToolRequest, deps *G
161135
return errResult, nil
162136
}
163137

164-
subject := common.ParseStringArg(request.Params.Arguments, "subject", "")
165-
body := common.ParseStringArg(request.Params.Arguments, "body", "")
166-
cc := common.ParseStringArg(request.Params.Arguments, "cc", "")
167-
bcc := common.ParseStringArg(request.Params.Arguments, "bcc", "")
168-
169-
raw := buildEmailMessage(EmailMessage{
170-
To: to,
171-
Cc: cc,
172-
Bcc: bcc,
173-
Subject: subject,
174-
Body: body,
175-
})
176-
177-
message := &gmail.Message{
178-
Raw: raw,
179-
}
138+
message := buildMessageFromArgs(request.Params.Arguments)
180139

181140
sent, err := svc.SendMessage(ctx, message)
182141
if err != nil {

internal/gmail/testable_threads.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,7 @@ func TestableGmailGetThread(ctx context.Context, request mcp.CallToolRequest, de
2828
return mcp.NewToolResultError(fmt.Sprintf("Gmail API error: %v", err)), nil
2929
}
3030

31-
// Parse body_format parameter (defaults to "text" for reduced token usage)
32-
bodyFormat := BodyFormatText
33-
if bf := common.ParseStringArg(request.Params.Arguments, "body_format", ""); bf != "" {
34-
switch bf {
35-
case "html":
36-
bodyFormat = BodyFormatHTML
37-
case "full":
38-
bodyFormat = BodyFormatFull
39-
default:
40-
bodyFormat = BodyFormatText
41-
}
42-
}
43-
44-
opts := FormatMessageOptions{BodyFormat: bodyFormat}
31+
opts := FormatMessageOptions{BodyFormat: parseBodyFormat(request.Params.Arguments)}
4532
messages := make([]map[string]any, 0, len(thread.Messages))
4633
for _, msg := range thread.Messages {
4734
messages = append(messages, FormatMessageWithOptions(msg, opts))

0 commit comments

Comments
 (0)