1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Base classes used by `nitweb`.
18 import model
::model_views
19 import model
::model_json
22 import popcorn
::pop_config
23 import popcorn
::pop_repos
24 import popcorn
::pop_json
30 redef fun default_db_name
do return "nitweb"
35 # MModule used to flatten model.
36 var mainmodule
: MModule
38 # Modelbuilder used to access sources.
39 var modelbuilder
: ModelBuilder
41 # The JSON API does not filter anything by default.
43 # So we can cache the model view.
44 var view
: ModelView is lazy
do
45 var view
= new ModelView(model
)
46 view
.min_visibility
= private_visibility
47 view
.include_fictive
= true
48 view
.include_empty_doc
= true
49 view
.include_attribute
= true
50 view
.include_test
= true
55 # Specific handler for the nitweb API.
56 abstract class APIHandler
60 var config
: NitwebConfig
62 # Find the MEntity ` with `full_name`.
63 fun find_mentity
(model
: ModelView, full_name
: nullable String): nullable MEntity do
64 if full_name
== null then return null
65 return model
.mentity_by_full_name
(full_name
.from_percent_encoding
)
68 # Try to load the mentity from uri with `/:id`.
70 # Send 400 if `:id` is null.
71 # Send 404 if no entity is found.
72 # Return null in both cases.
73 fun mentity_from_uri
(req
: HttpRequest, res
: HttpResponse): nullable MEntity do
74 var id
= req
.param
("id")
76 res
.api_error
(400, "Expected mentity full name")
79 var mentity
= find_mentity
(config
.view
, id
)
80 if mentity
== null then
81 res
.api_error
(404, "MEntity `{id}` not found")
87 # A Rooter dedicated to APIHandlers.
92 var config
: NitwebConfig
95 redef class HttpResponse
97 # Return an HTTP error response with `status`
99 # Like the rest of the API, errors are formated as JSON:
101 # { "status": 404, "message": "Not found" }
103 fun api_error
(status
: Int, message
: String) do
104 json
(new APIError(status
, message
), status
)
107 # Write data as JSON and set the right content type header.
108 fun raw_json
(json
: nullable String, status
: nullable Int) do
109 header
["Content-Type"] = media_types
["json"].as(not null)
118 # An error returned by the API.
120 # Can be serialized to json.
127 # Response error message
131 # Fullname representation that can be used to build decorated links
133 # * MPackage: `mpackage_name`
134 # * MGroup: `(mpackage_name::)mgroup_name`
136 super Array[nullable NSEntity]
140 redef fun to_s
do return self.join
("")
141 redef fun serialize_to
(v
) do to_a
.serialize_to
(v
)
144 # Something that goes in a Namespace
147 # * a `NSToken` for tokens like `::`, `>` and `$`
148 # * a `MSRef` for references to mentities
153 # A reference to a MEntity that can be rendered as a link.
155 # We do not reuse `MEntityRef` ref since NSRef can be found in a ref and create
161 # The mentity to link to/
164 redef fun core_serialize_to
(v
) do
165 v
.serialize_attribute
("web_url", mentity
.web_url
)
166 v
.serialize_attribute
("api_url", mentity
.api_url
)
167 v
.serialize_attribute
("name", mentity
.name
)
171 # A namespace token representation
173 # Used for namespace tokens like `::`, `>` and `$`
180 # URL to `self` within the web interface.
181 fun web_url
: String do return "/doc/" / full_name
183 # URL to `self` within the JSON api.
184 fun api_url
: String do return "/api/entity/" / full_name
186 redef fun core_serialize_to
(v
) do
188 v
.serialize_attribute
("namespace", namespace
)
189 v
.serialize_attribute
("web_url", web_url
)
190 v
.serialize_attribute
("api_url", api_url
)
193 # Return `self.full_name` as an object that can be serialized to json.
194 fun namespace
: nullable Namespace do return null
196 # Return a new NSRef to `self`.
197 fun to_ns_ref
: NSRef do return new NSRef(self)
200 redef class MEntityRef
201 redef fun core_serialize_to
(v
) do
203 v
.serialize_attribute
("namespace", mentity
.namespace
)
204 v
.serialize_attribute
("web_url", mentity
.web_url
)
205 v
.serialize_attribute
("api_url", mentity
.api_url
)
206 v
.serialize_attribute
("name", mentity
.name
)
207 v
.serialize_attribute
("mdoc", mentity
.mdoc_or_fallback
)
208 v
.serialize_attribute
("visibility", mentity
.visibility
.to_s
)
209 v
.serialize_attribute
("modifiers", mentity
.collect_modifiers
)
210 v
.serialize_attribute
("class_name", mentity
.class_name
)
211 var mentity
= self.mentity
212 if mentity
isa MMethod then
213 v
.serialize_attribute
("msignature", mentity
.intro
.msignature
)
214 else if mentity
isa MMethodDef then
215 v
.serialize_attribute
("msignature", mentity
.msignature
)
216 else if mentity
isa MVirtualTypeProp then
217 v
.serialize_attribute
("bound", to_mentity_ref
(mentity
.intro
.bound
))
218 else if mentity
isa MVirtualTypeDef then
219 v
.serialize_attribute
("bound", to_mentity_ref
(mentity
.bound
))
221 v
.serialize_attribute
("location", mentity
.location
)
227 # Add doc down processing
228 redef fun core_serialize_to
(v
) do
229 v
.serialize_attribute
("html_synopsis", html_synopsis
.write_to_string
)
234 redef fun namespace
do return new Namespace.from
([to_ns_ref
])
238 redef fun namespace
do
241 return new Namespace.from
([to_ns_ref
, ">": nullable NSEntity])
243 return new Namespace.from
([p
.namespace
, to_ns_ref
, ">": nullable NSEntity])
248 redef fun namespace
do
249 var mgroup
= self.mgroup
250 if mgroup
== null then
251 return new Namespace.from
([to_ns_ref
])
253 return new Namespace.from
([mgroup
.mpackage
.to_ns_ref
, "::", to_ns_ref
: nullable NSEntity])
256 private fun ns_for
(visibility
: MVisibility): nullable Namespace do
257 if visibility
<= private_visibility
then return namespace
258 var mgroup
= self.mgroup
259 if mgroup
== null then return namespace
260 return mgroup
.mpackage
.namespace
265 redef fun namespace
do
266 return new Namespace.from
([intro_mmodule
.ns_for
(visibility
), "::", to_ns_ref
: nullable NSEntity])
270 redef class MClassDef
271 redef fun namespace
do
273 return new Namespace.from
([mmodule
.ns_for
(mclass
.visibility
), "$", to_ns_ref
: nullable NSEntity])
274 else if mclass
.intro_mmodule
.mpackage
!= mmodule
.mpackage
then
275 return new Namespace.from
([mmodule
.namespace
, "$", mclass
.namespace
: nullable NSEntity])
276 else if mclass
.visibility
> private_visibility
then
277 return new Namespace.from
([mmodule
.namespace
, "$", mclass
.to_ns_ref
: nullable NSEntity])
279 return new Namespace.from
([mmodule
.full_name
, "$::", mclass
.intro_mmodule
.to_ns_ref
: nullable NSEntity])
282 redef fun web_url
do return "{mclass.web_url}/lin#{full_name}"
285 redef class MProperty
286 redef fun namespace
do
287 if intro_mclassdef
.is_intro
then
288 return new Namespace.from
([intro_mclassdef
.mmodule
.ns_for
(visibility
), "::", intro_mclassdef
.mclass
.to_ns_ref
, "::", to_ns_ref
: nullable NSEntity])
290 return new Namespace.from
([intro_mclassdef
.mmodule
.namespace
, "::", intro_mclassdef
.mclass
.to_ns_ref
, "::", to_ns_ref
: nullable NSEntity])
296 redef fun namespace
do
297 var res
= new Namespace
298 res
.add mclassdef
.namespace
301 if mclassdef
.mclass
== mproperty
.intro_mclassdef
.mclass
then
304 if mclassdef
.mmodule
.mpackage
!= mproperty
.intro_mclassdef
.mmodule
.mpackage
then
305 res
.add mproperty
.intro_mclassdef
.mmodule
.ns_for
(mproperty
.visibility
)
307 else if mproperty
.visibility
<= private_visibility
then
308 if mclassdef
.mmodule
.namespace_for
(mclassdef
.mclass
.visibility
) != mproperty
.intro_mclassdef
.mmodule
.mpackage
then
310 res
.add mproperty
.intro_mclassdef
.mmodule
.to_ns_ref
314 if mclassdef
.mclass
!= mproperty
.intro_mclassdef
.mclass
then
315 res
.add mproperty
.intro_mclassdef
.to_ns_ref
323 redef fun web_url
do return "{mproperty.web_url}/lin#{full_name}"
326 redef class MClassType
327 redef var web_url
= mclass
.web_url
is lazy
330 redef class MNullableType
331 redef var web_url
= mtype
.web_url
is lazy
334 redef class MParameterType
335 redef var web_url
= mclass
.web_url
is lazy
338 redef class MVirtualType
339 redef var web_url
= mproperty
.web_url
is lazy
342 redef class POSetElement[E
]
345 redef fun core_serialize_to
(v
) do
346 assert self isa POSetElement[MEntity]
347 v
.serialize_attribute
("direct_greaters", to_mentity_refs
(direct_greaters
))
348 v
.serialize_attribute
("direct_smallers", to_mentity_refs
(direct_smallers
))