fields.go

  1// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
  2//
  3// SPDX-License-Identifier: AGPL-3.0-or-later
  4
  5package tasks
  6
  7import (
  8	"fmt"
  9
 10	"git.secluded.site/go-lunatask"
 11	"github.com/mark3labs/mcp-go/mcp"
 12
 13	"git.sr.ht/~amolith/lunatask-mcp-server/tools/shared"
 14)
 15
 16// setCreatePriority sets priority on a create task request.
 17func (h *Handler) setCreatePriority(
 18	task *lunatask.CreateTaskRequest,
 19	arguments map[string]any,
 20) *mcp.CallToolResult {
 21	priorityArg, exists := arguments["priority"]
 22	if !exists || priorityArg == nil {
 23		return nil
 24	}
 25
 26	priorityStr, ok := priorityArg.(string)
 27	if !ok {
 28		result, _ := shared.ReportError(
 29			"Invalid type for 'priority' argument: expected string.",
 30		)
 31
 32		return result
 33	}
 34
 35	translated, errResult := ParsePriority(priorityStr)
 36	if errResult != nil {
 37		return errResult
 38	}
 39
 40	task.Priority = &translated
 41
 42	return nil
 43}
 44
 45// setCreateEisenhower sets eisenhower on a create task request.
 46func (h *Handler) setCreateEisenhower(
 47	task *lunatask.CreateTaskRequest,
 48	arguments map[string]any,
 49) *mcp.CallToolResult {
 50	eisenhowerArg, exists := arguments["eisenhower"]
 51	if !exists || eisenhowerArg == nil {
 52		return nil
 53	}
 54
 55	eisenhowerStr, ok := eisenhowerArg.(string)
 56	if !ok {
 57		result, _ := shared.ReportError(
 58			"Invalid type for 'eisenhower' argument: expected string.",
 59		)
 60
 61		return result
 62	}
 63
 64	translated, errResult := ParseEisenhower(eisenhowerStr)
 65	if errResult != nil {
 66		return errResult
 67	}
 68
 69	task.Eisenhower = &translated
 70
 71	return nil
 72}
 73
 74// setCreateMotivation sets motivation on a create task request.
 75func (h *Handler) setCreateMotivation(
 76	task *lunatask.CreateTaskRequest,
 77	arguments map[string]any,
 78) *mcp.CallToolResult {
 79	motivationVal, exists := arguments["motivation"]
 80	if !exists || motivationVal == nil {
 81		return nil
 82	}
 83
 84	motivation, ok := motivationVal.(string)
 85	if !ok {
 86		result, _ := shared.ReportError("'motivation' must be a string")
 87
 88		return result
 89	}
 90
 91	if motivation == "" {
 92		return nil
 93	}
 94
 95	if errResult := ValidateMotivation(motivation); errResult != nil {
 96		return errResult
 97	}
 98
 99	task.Motivation = &motivation
100
101	return nil
102}
103
104// setCreateStatus sets status on a create task request.
105func (h *Handler) setCreateStatus(
106	task *lunatask.CreateTaskRequest,
107	arguments map[string]any,
108) *mcp.CallToolResult {
109	statusVal, exists := arguments["status"]
110	if !exists || statusVal == nil {
111		return nil
112	}
113
114	status, ok := statusVal.(string)
115	if !ok {
116		result, _ := shared.ReportError("'status' must be a string")
117
118		return result
119	}
120
121	if status == "" {
122		return nil
123	}
124
125	if errResult := ValidateStatus(status); errResult != nil {
126		return errResult
127	}
128
129	task.Status = &status
130
131	return nil
132}
133
134// setCreateEstimate sets estimate on a create task request.
135func (h *Handler) setCreateEstimate(
136	task *lunatask.CreateTaskRequest,
137	arguments map[string]any,
138) *mcp.CallToolResult {
139	estimateArg, exists := arguments["estimate"]
140	if !exists || estimateArg == nil {
141		return nil
142	}
143
144	estimateVal, ok := estimateArg.(float64)
145	if !ok {
146		result, _ := shared.ReportError(
147			"Invalid type for 'estimate' argument: expected number.",
148		)
149
150		return result
151	}
152
153	estimate := int(estimateVal)
154
155	if errResult := ValidateEstimate(estimate); errResult != nil {
156		return errResult
157	}
158
159	task.Estimate = &estimate
160
161	return nil
162}
163
164// setCreateScheduledOn sets scheduled_on on a create task request.
165func (h *Handler) setCreateScheduledOn(
166	task *lunatask.CreateTaskRequest,
167	arguments map[string]any,
168) *mcp.CallToolResult {
169	scheduledOnArg, exists := arguments["scheduled_on"]
170	if !exists {
171		return nil
172	}
173
174	scheduledOnStr, ok := scheduledOnArg.(string)
175	if !ok {
176		result, _ := shared.ReportError(
177			"Invalid type for scheduled_on argument: expected string.",
178		)
179
180		return result
181	}
182
183	if scheduledOnStr == "" {
184		return nil
185	}
186
187	date, err := lunatask.ParseDate(scheduledOnStr)
188	if err != nil {
189		result, _ := shared.ReportError(fmt.Sprintf(
190			"Invalid format for scheduled_on: '%s'. Must be YYYY-MM-DD.",
191			scheduledOnStr,
192		))
193
194		return result
195	}
196
197	task.ScheduledOn = &date
198
199	return nil
200}
201
202// setCreateSource sets source fields on a create task request.
203func (h *Handler) setCreateSource(
204	task *lunatask.CreateTaskRequest,
205	arguments map[string]any,
206) {
207	if sourceArg, exists := arguments["source"].(string); exists && sourceArg != "" {
208		task.Source = &sourceArg
209	}
210
211	if sourceIDArg, exists := arguments["source_id"].(string); exists && sourceIDArg != "" {
212		task.SourceID = &sourceIDArg
213	}
214}
215
216// setUpdateNote sets note on an update task request.
217func (h *Handler) setUpdateNote(
218	payload *lunatask.UpdateTaskRequest,
219	arguments map[string]any,
220) *mcp.CallToolResult {
221	noteArg, exists := arguments["note"]
222	if !exists {
223		return nil
224	}
225
226	noteStr, ok := noteArg.(string)
227	if !ok && noteArg != nil {
228		result, _ := shared.ReportError(
229			"Invalid type for note argument: expected string.",
230		)
231
232		return result
233	}
234
235	if ok {
236		payload.Note = &noteStr
237	}
238
239	return nil
240}
241
242// setUpdateEstimate sets estimate on an update task request.
243func (h *Handler) setUpdateEstimate(
244	payload *lunatask.UpdateTaskRequest,
245	arguments map[string]any,
246) *mcp.CallToolResult {
247	estimateArg, exists := arguments["estimate"]
248	if !exists || estimateArg == nil {
249		return nil
250	}
251
252	estimateVal, ok := estimateArg.(float64)
253	if !ok {
254		result, _ := shared.ReportError(
255			"Invalid type for estimate argument: expected number.",
256		)
257
258		return result
259	}
260
261	estimate := int(estimateVal)
262
263	if errResult := ValidateEstimate(estimate); errResult != nil {
264		return errResult
265	}
266
267	payload.Estimate = &estimate
268
269	return nil
270}
271
272// setUpdatePriority sets priority on an update task request.
273func (h *Handler) setUpdatePriority(
274	payload *lunatask.UpdateTaskRequest,
275	arguments map[string]any,
276) *mcp.CallToolResult {
277	priorityArg, exists := arguments["priority"]
278	if !exists || priorityArg == nil {
279		return nil
280	}
281
282	priorityStr, ok := priorityArg.(string)
283	if !ok {
284		result, _ := shared.ReportError(
285			"Invalid type for 'priority' argument: expected string.",
286		)
287
288		return result
289	}
290
291	translated, errResult := ParsePriority(priorityStr)
292	if errResult != nil {
293		return errResult
294	}
295
296	payload.Priority = &translated
297
298	return nil
299}
300
301// setUpdateEisenhower sets eisenhower on an update task request.
302func (h *Handler) setUpdateEisenhower(
303	payload *lunatask.UpdateTaskRequest,
304	arguments map[string]any,
305) *mcp.CallToolResult {
306	eisenhowerArg, exists := arguments["eisenhower"]
307	if !exists || eisenhowerArg == nil {
308		return nil
309	}
310
311	eisenhowerStr, ok := eisenhowerArg.(string)
312	if !ok {
313		result, _ := shared.ReportError(
314			"Invalid type for 'eisenhower' argument: expected string.",
315		)
316
317		return result
318	}
319
320	translated, errResult := ParseEisenhower(eisenhowerStr)
321	if errResult != nil {
322		return errResult
323	}
324
325	payload.Eisenhower = &translated
326
327	return nil
328}
329
330// setUpdateMotivation sets motivation on an update task request.
331func (h *Handler) setUpdateMotivation(
332	payload *lunatask.UpdateTaskRequest,
333	arguments map[string]any,
334) *mcp.CallToolResult {
335	motivationArg, exists := arguments["motivation"]
336	if !exists {
337		return nil
338	}
339
340	motivationStr, ok := motivationArg.(string)
341	if !ok && motivationArg != nil {
342		result, _ := shared.ReportError(
343			"Invalid type for motivation argument: expected string.",
344		)
345
346		return result
347	}
348
349	if !ok {
350		return nil
351	}
352
353	if motivationStr != "" {
354		if errResult := ValidateMotivation(motivationStr); errResult != nil {
355			return errResult
356		}
357	}
358
359	payload.Motivation = &motivationStr
360
361	return nil
362}
363
364// setUpdateStatus sets status on an update task request.
365func (h *Handler) setUpdateStatus(
366	payload *lunatask.UpdateTaskRequest,
367	arguments map[string]any,
368) *mcp.CallToolResult {
369	statusArg, exists := arguments["status"]
370	if !exists {
371		return nil
372	}
373
374	statusStr, ok := statusArg.(string)
375	if !ok && statusArg != nil {
376		result, _ := shared.ReportError(
377			"Invalid type for status argument: expected string.",
378		)
379
380		return result
381	}
382
383	if !ok {
384		return nil
385	}
386
387	if statusStr != "" {
388		if errResult := ValidateStatus(statusStr); errResult != nil {
389			return errResult
390		}
391	}
392
393	payload.Status = &statusStr
394
395	return nil
396}
397
398// setUpdateScheduledOn sets scheduled_on on an update task request.
399func (h *Handler) setUpdateScheduledOn(
400	payload *lunatask.UpdateTaskRequest,
401	arguments map[string]any,
402) *mcp.CallToolResult {
403	scheduledOnArg, exists := arguments["scheduled_on"]
404	if !exists {
405		return nil
406	}
407
408	scheduledOnStr, ok := scheduledOnArg.(string)
409	if !ok && scheduledOnArg != nil {
410		result, _ := shared.ReportError(
411			"Invalid type for scheduled_on argument: expected string.",
412		)
413
414		return result
415	}
416
417	if !ok || scheduledOnStr == "" {
418		return nil
419	}
420
421	date, err := lunatask.ParseDate(scheduledOnStr)
422	if err != nil {
423		result, _ := shared.ReportError(fmt.Sprintf(
424			"Invalid format for scheduled_on: '%s'. Must be YYYY-MM-DD.",
425			scheduledOnStr,
426		))
427
428		return result
429	}
430
431	payload.ScheduledOn = &date
432
433	return nil
434}