Skip to content

Commit

Permalink
Merge: src/doc/commands: use filters
Browse files Browse the repository at this point in the history
This PR allows the DocCommands to use ModelFilter.

With this, one can setup the filter used by a command from:
* The commands programmatic Nit interface
   ~~~nit
   var filter = new ModelFilter(accept_test = false)
   var cmd = new CmdParents(model, main, filter, mentity_name = "MyEntity")
   ~~~
* The string interface through the CommandParser
   ~~~raw
   [[parents: MyEntity | no-test]]
   ~~~
* The HTTP interface
   ~~~raw
   /children/MyEntity?no-test=true
   ~~~

Pull-Request: #2722
Reviewed-by: Jean Privat <[email protected]>
  • Loading branch information
privat committed Jun 26, 2018
2 parents 6ad8628 + 8865593 commit 469e5f2
Show file tree
Hide file tree
Showing 19 changed files with 588 additions and 239 deletions.
2 changes: 1 addition & 1 deletion src/doc/api/api_docdown.nit
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import commands::commands_docdown
redef class NitwebConfig
# Specific Markdown processor to use within Nitweb
var md_processor: MarkdownProcessor is lazy do
var parser = new CommandParser(model, mainmodule, modelbuilder, catalog, filter)
var parser = new CommandParser(model, mainmodule, modelbuilder, catalog)
var proc = new CmdMarkdownProcessor(parser)
proc.decorator = new CmdDecorator(model)
return proc
Expand Down
7 changes: 7 additions & 0 deletions src/doc/commands/commands_base.nit
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ abstract class DocCommand
#
# Warnings are generally used to distinguish empty list or mdoc from no data at all.
fun init_command: CmdMessage do return new CmdSuccess

# Return a new filter for that command execution.
fun cmd_filter: ModelFilter do
var filter = self.filter
if filter == null then return new ModelFilter
return new ModelFilter.from(filter)
end
end

# Command message
Expand Down
8 changes: 5 additions & 3 deletions src/doc/commands/commands_catalog.nit
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ class CmdCatalogSearch
if query == null then return new ErrorNoQuery
sorter = null

var filter = self.filter
var index = model.index

# lookup by name prefix
var matches = index.find_by_name_prefix(query).uniq.
var matches = index.find_by_name_prefix(query, filter).uniq.
sort(lname_sorter, name_sorter, kind_sorter)
matches = matches.rerank.sort(vis_sorter, score_sorter)

# lookup by tags
var malus = matches.length
if catalog.tag2proj.has_key(query) then
for mpackage in catalog.tag2proj[query] do
if filter != null and not filter.accept_mentity(mpackage) then continue
matches.add new IndexMatch(mpackage, malus)
malus += 1
end
Expand All @@ -64,7 +66,7 @@ class CmdCatalogSearch
# lookup by full_name prefix
malus = matches.length
var full_matches = new IndexMatches
for match in index.find_by_full_name_prefix(query).
for match in index.find_by_full_name_prefix(query, filter).
sort(lfname_sorter, fname_sorter) do
match.score += 1
full_matches.add match
Expand All @@ -74,7 +76,7 @@ class CmdCatalogSearch
# lookup by similarity
malus = matches.length
var sim_matches = new IndexMatches
for match in index.find_by_similarity(query).sort(score_sorter, lname_sorter, name_sorter) do
for match in index.find_by_similarity(query, filter).sort(score_sorter, kind_sorter, lname_sorter, name_sorter) do
if match.score > query.length then break
match.score += 1
sim_matches.add match
Expand Down
112 changes: 96 additions & 16 deletions src/doc/commands/commands_http.nit
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,8 +63,10 @@ end

redef class CmdList
redef fun http_init(req) do
limit = req.int_arg("l")
page = req.int_arg("p")
var opt_limit = req.int_arg("l")
if opt_limit != null then limit = opt_limit
var opt_page = req.int_arg("p")
if opt_page != null then page = opt_page
return super
end
end
Expand Down Expand Up @@ -78,31 +104,38 @@ end

redef class CmdComment
redef fun http_init(req) do
full_doc = req.bool_arg("full_doc") or else true
fallback = req.bool_arg("fallback") or else true
format = req.string_arg("format") or else "raw"
var opt_full_doc = req.bool_arg("full_doc")
if opt_full_doc != null then full_doc = opt_full_doc
var opt_fallback = req.bool_arg("fallback")
if opt_fallback != null then fallback = opt_fallback
var opt_format = req.string_arg("format")
if opt_format != null then format = opt_format
return super
end
end

redef class CmdEntityLink
redef fun http_init(req) do
text = req.string_arg("text")
title = req.string_arg("title")
var opt_text = req.string_arg("text")
if opt_text != null then text = opt_text
var opt_title = req.string_arg("title")
if opt_title != null then title = opt_title
return super
end
end

redef class CmdAncestors
redef fun http_init(req) do
parents = req.bool_arg("parents") or else true
var opt_parents = req.bool_arg("parents")
if opt_parents != null then parents = opt_parents
return super
end
end

redef class CmdDescendants
redef fun http_init(req) do
children = req.bool_arg("children") or else true
var opt_children = req.bool_arg("children")
if opt_children != null then children = opt_children
return super
end
end
Expand All @@ -121,14 +154,16 @@ end

redef class CmdModelEntities
redef fun http_init(req) do
kind = req.string_arg("kind") or else "all"
var opt_kind = req.string_arg("kind")
if opt_kind != null then kind = opt_kind
return super
end
end

redef class CmdCode
redef fun http_init(req) do
format = req.string_arg("format") or else "raw"
var opt_format = req.string_arg("format")
if opt_format != null then format = opt_format
return super
end
end
Expand All @@ -140,7 +175,8 @@ redef class CmdEntityCode
if name != null then name = name.from_percent_encoding
mentity_name = name

format = req.string_arg("format") or else "raw"
var opt_format = req.string_arg("format")
if opt_format != null then format = opt_format
return init_command
end
end
Expand All @@ -149,15 +185,18 @@ end

redef class CmdGraph
redef fun http_init(req) do
format = req.string_arg("format") or else "dot"
var opt_format = req.string_arg("format")
if opt_format != null then format = opt_format
return super
end
end

redef class CmdInheritanceGraph
redef fun http_init(req) do
pdepth = req.int_arg("pdepth")
cdepth = req.int_arg("cdepth")
var opt_pdepth = req.int_arg("pdepth")
if opt_pdepth != null then pdepth = opt_pdepth
var opt_cdepth = req.int_arg("cdepth")
if opt_cdepth != null then cdepth = opt_cdepth
return super
end
end
Expand All @@ -181,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
Loading

0 comments on commit 469e5f2

Please sign in to comment.