cmd/commands_http: parse filter options from HttpRequest
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 15 May 2018 17:57:20 +0000 (13:57 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Thu, 21 Jun 2018 00:45:30 +0000 (20:45 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/doc/commands/commands_http.nit
src/doc/commands/tests/test_commands_http.nit

index 6d0e6ae..cc26f7f 100644 (file)
@@ -24,7 +24,31 @@ import nitcorn::vararg_routes
 
 redef class DocCommand
        # Init the command from an HTTPRequest
-       fun http_init(req: HttpRequest): CmdMessage do return init_command
+       fun http_init(req: HttpRequest): CmdMessage do
+               var filter = cmd_filter
+               var opt_vis = req.visibility_arg("min-visibility")
+               if opt_vis != null then filter.min_visibility = opt_vis
+               var opt_fictive = req.bool_arg("no-fictive")
+               if opt_fictive != null then filter.accept_fictive = not opt_fictive
+               var opt_test = req.bool_arg("no-test")
+               if opt_test != null then filter.accept_test = not opt_test
+               var opt_redef = req.bool_arg("no-redef")
+               if opt_redef != null then filter.accept_redef = not opt_redef
+               var opt_extern = req.bool_arg("no-extern")
+               if opt_extern != null then filter.accept_extern = not opt_extern
+               var opt_example = req.bool_arg("no-example")
+               if opt_example != null then filter.accept_example = not opt_example
+               var opt_attr = req.bool_arg("no-attribute")
+               if opt_attr != null then filter.accept_attribute = not opt_attr
+               var opt_doc = req.bool_arg("no-empty-doc")
+               if opt_doc != null then filter.accept_empty_doc = not opt_doc
+               var opt_inh = req.mentity_arg(model, "inherit")
+               if opt_inh != null then filter.accept_inherited = opt_inh
+               var opt_match = req.string_arg("match")
+               if opt_match != null then filter.accept_full_name = opt_match
+               self.filter = filter
+               return init_command
+       end
 end
 
 redef class CmdEntity
@@ -196,3 +220,44 @@ redef class CmdCatalogPerson
                return super
        end
 end
+
+# Util
+
+redef class HttpRequest
+
+       # Map String visiblity name to MVisibility object
+       var allowed_visibility: HashMap[String, MVisibility] is lazy do
+               var res = new HashMap[String, MVisibility]
+               res["public"] = public_visibility
+               res["protected"] = protected_visibility
+               res["private"] = private_visibility
+               return res
+       end
+
+       # Get arg as a MVisibility
+       #
+       # Return `null` if no option with that `key` or if the value is not in
+       # `allowed_visibility`.
+       fun visibility_arg(key: String): nullable MVisibility do
+               var value = string_arg(key)
+               if value == null then return null
+               if not allowed_visibility.keys.has(key) then return null
+               return allowed_visibility[value]
+       end
+
+       # Get arg as a MEntity
+       #
+       # Lookup first by `MEntity::full_name` then by `MEntity::name`.
+       # Return `null` if the mentity name does not exist or return a conflict.
+       private fun mentity_arg(model: Model, key: String): nullable MEntity do
+               var value = string_arg(key)
+               if value == null or value.is_empty then return null
+
+               var mentity = model.mentity_by_full_name(value)
+               if mentity != null then return mentity
+
+               var mentities = model.mentities_by_name(value)
+               if mentities.is_empty or mentities.length > 1 then return null
+               return mentities.first
+       end
+end
index b009b7f..a08854c 100644 (file)
@@ -157,6 +157,14 @@ class TestCommandsHttp
                assert cmd.results.as(not null).length == 0
        end
 
+       fun test_cmd_http_ancestors_with_filter_match is test do
+               var req = new_request("/test_prog::Warrior?match=Object")
+               var cmd = new CmdAncestors(test_model, test_main)
+               var res = cmd.http_init(req)
+               assert res isa CmdSuccess
+               assert cmd.results.as(not null).length == 1
+       end
+
        # CmdSearch
 
        fun test_cmd_http_search is test do
@@ -192,6 +200,14 @@ class TestCommandsHttp
                assert res isa WarningNoFeatures
        end
 
+       fun test_cmd_http_features_with_filter_inherited is test do
+               var req = new_request("/test_prog::TestGame?inherited=TestGame")
+               var cmd = new CmdFeatures(test_model)
+               var res = cmd.http_init(req)
+               assert res isa CmdSuccess
+               assert cmd.results.as(not null).length == 3
+       end
+
        # CmdLinearization
 
        fun test_cmd_http_lin is test do