Procházet zdrojové kódy

r1: is_valid_mode — the three identical TASK_MODES loops (edit_task, the daemon's task-create callback, the queue create action) collapse into the one validator beside TASK_MODES.

clanker před 1 měsícem
rodič
revize
4ac6ab1368

+ 1 - 8
src/Api/SaddleApi.vala

@@ -624,14 +624,7 @@ namespace ClankerSaddle.Api {
                         "A mandate is required (heredoc body of `task create`)");
                 }
                 string mode = EntryJson.get_string_or(body, "mode", "implement");
-                bool mode_valid = false;
-                foreach (string candidate in TASK_MODES) {
-                    if (mode == candidate) {
-                        mode_valid = true;
-                        break;
-                    }
-                }
-                if (!mode_valid) {
+                if (!is_valid_mode(mode)) {
                     return SaddleUpdateEndpoint.json_error(400,
                         @"Unknown mode '$(mode)' — valid modes: "
                         + string.joinv(", ", TASK_MODES));

+ 10 - 0
src/Services/GoalService.vala

@@ -50,6 +50,16 @@ namespace ClankerSaddle.Services {
     /** Valid work modes for human/goal-created tasks. */
     public const string[] TASK_MODES = { "implement", "investigate", "review", "test" };
 
+    /** Whether `mode` is one of {@link TASK_MODES} (the create/edit validation). */
+    public bool is_valid_mode(string mode) {
+        foreach (string candidate in TASK_MODES) {
+            if (mode == candidate) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     /** The priority every goal-run internal task dispatches at. */
     public const int64 GOAL_RUN_PRIORITY = 3;
 

+ 1 - 8
src/Services/TaskService.vala

@@ -409,14 +409,7 @@ namespace ClankerSaddle.Services {
                     }
                 }
                 if (mode != null && mode.length > 0) {
-                    bool mode_valid = false;
-                    foreach (string candidate in TASK_MODES) {
-                        if (mode == candidate) {
-                            mode_valid = true;
-                            break;
-                        }
-                    }
-                    if (!mode_valid) {
+                    if (!is_valid_mode(mode)) {
                         throw new Error(Quark.from_string("clanker-saddle"), 79,
                             @"Unknown mode '$(mode)' — valid modes: " + string.joinv(", ", TASK_MODES));
                     }

+ 1 - 8
src/actions/TaskActions.vala

@@ -85,14 +85,7 @@ namespace ClankerSaddle {
                 if (mode.length == 0) {
                     mode = "implement";
                 }
-                bool mode_valid = false;
-                foreach (string candidate in TASK_MODES) {
-                    if (mode == candidate) {
-                        mode_valid = true;
-                        break;
-                    }
-                }
-                if (!mode_valid) {
+                if (!is_valid_mode(mode)) {
                     return directives().notify("error",
                         @"Unknown mode '$(mode)'").navigate("/tasks");
                 }