# JsonDeserializer specific for Github objects.
class GithubDeserializer
super JsonDeserializer
private var pattern_base = "https://api.github.com"
# Url patterns to class names
var url_patterns: Map[Regex, String] is lazy do
var map = new HashMap[Regex, String]
map["{pattern_base}/users/[^/]*$".to_re] = "User"
map["{pattern_base}/repos/[^/]*/[^/]*$".to_re] = "Repo"
map["{pattern_base}/repos/[^/]*/[^/]*/labels/[^/]+$".to_re] = "Label"
map["{pattern_base}/repos/[^/]*/[^/]*/milestones/[0-9]+$".to_re] = "Milestone"
map["{pattern_base}/repos/[^/]*/[^/]*/issues/[0-9]+$".to_re] = "Issue"
map["{pattern_base}/repos/[^/]*/[^/]*/issues/comments/[0-9]+$".to_re] = "IssueComment"
map["{pattern_base}/repos/[^/]*/[^/]*/issues/events/[0-9]+$".to_re] = "IssueEvent"
map["{pattern_base}/repos/[^/]*/[^/]*/pulls/[0-9]+$".to_re] = "PullRequest"
map["{pattern_base}/repos/[^/]*/[^/]*/pulls/comments/[0-9]+$".to_re] = "PullComment"
map["{pattern_base}/repos/[^/]*/[^/]*/comments/[0-9]+$".to_re] = "CommitComment"
map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+$".to_re] = "Commit"
map["{pattern_base}/repos/[^/]*/[^/]*/commits/[a-f0-9]+/status$".to_re] = "CommitStatus"
map["{pattern_base}/repos/[^/]*/[^/]*/statuses/[a-f0-9]+$".to_re] = "RepoStatus"
return map
end
# Match `url` property in object to a class name
fun url_heuristic(raw: Map[String, nullable Object]): nullable String do
if not raw.has_key("url") then return null
var url = raw["url"].as(String)
for re, class_name in url_patterns do
if url.has(re) then return class_name
end
return null
end
redef fun class_name_heuristic(raw) do
# Try with url
var class_name = url_heuristic(raw)
if class_name != null then return class_name
# print raw.serialize_to_json(true, true) # debug
# Use properties heuristics
if raw.has_key("name") and raw.has_key("commit") then
return "Branch"
else if raw.has_key("total_count") and raw.has_key("items") then
return "SearchResults"
else if raw.has_key("total") and raw.has_key("weeks") then
return "ContributorStats"
else if raw.has_key("a") and raw.has_key("d") and raw.has_key("c") then
return "ContributorWeek"
end
return null
end
redef fun deserialize_class(name) do
if name == "Issue" then
var issue = super.as(Issue)
if path.last.has_key("pull_request") then
issue.is_pull_request = true
end
return issue
else if name == "Commit" then
var commit = super.as(Commit)
var git_commit = commit.commit
if git_commit != null then commit.message = git_commit.message
return commit
end
return super
end
end
lib/github/api.nit:1075,1--1146,3