From e91015181dc0acab2dacd35d4cf5f270159382cd Mon Sep 17 00:00:00 2001 From: Tit Petric Date: Sun, 1 Jul 2018 00:38:39 +0200 Subject: [PATCH] update generator, sort routes by alphabet --- sam/_gen.php | 38 ++++++++++++++++++++++++++------------ sam/routes.go | 34 +++++++++++++++++----------------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/sam/_gen.php b/sam/_gen.php index 6f5da7a3b..67d36e671 100755 --- a/sam/_gen.php +++ b/sam/_gen.php @@ -5,24 +5,32 @@ error_reporting(E_ALL^E_NOTICE); include("docs/vendor/autoload.php"); +function array_change_key_case_recursive($arr) { + return array_map(function ($item) { + if (is_array($item)) { + $item = array_change_key_case_recursive($item); + } + return $item; + }, array_change_key_case($arr)); +} + $tpl = new Monotek\MiniTPL\Template; $tpl->set_compile_location("/tmp", true); $tpl->add_default("newline", "\n"); -$apis = json_decode(file_get_contents("docs/src/spec.json"), true); -foreach ($apis as $k => $v) { - foreach ($v['apis'] as $kk => $vv) { - $vv['path'] = "/" . $vv['name']; - $v['apis'][$kk] = $vv; - } - $v['path'] = "/" . $v['entrypoint']; - $apis[$k] = $v; -} +$api_files = glob("docs/src/spec/*.json"); +$apis = array_map(function($filename) { + return array_change_key_case_recursive(json_decode(file_get_contents($filename), true)); +}, $api_files); + +usort($apis, function($a, $b) { + return strcmp($a['interface'], $b['interface']); +}); foreach (array("structs", "handlers", "interfaces", "request", "") as $type) { foreach ($apis as $api) { if (!empty($api['struct'])) { - $name = ucfirst($api['entrypoint']); + $name = ucfirst($api['interface']); $filename = str_replace("..", ".", strtolower($name) . "." . $type . ".go"); $tpl->load("http_$type.tpl"); @@ -37,13 +45,19 @@ foreach (array("structs", "handlers", "interfaces", "request", "") as $type) { $tpl->assign("calls", $api['apis']); $contents = $tpl->get(); - file_put_contents($filename, $contents); + $save = true; + if ($type === "" && file_exists($filename)) { + $save = false; + } + if ($save) { + file_put_contents($filename, $contents); + } } } } foreach (array("routes") as $type) { - $name = ucfirst($api['entrypoint']); + $name = ucfirst($api['interface']); $filename = str_replace("..", ".", $type . ".go"); $tpl->load("http_$type.tpl"); diff --git a/sam/routes.go b/sam/routes.go index 86a908e7f..e108ae05c 100644 --- a/sam/routes.go +++ b/sam/routes.go @@ -5,27 +5,12 @@ import ( ) func MountRoutes(r chi.Router) { - organisation := OrganisationHandlers{}.new() - team := TeamHandlers{}.new() channel := ChannelHandlers{}.new() message := MessageHandlers{}.new() + organisation := OrganisationHandlers{}.new() + team := TeamHandlers{}.new() user := UserHandlers{}.new() websocket := WebsocketHandlers{}.new() - r.Route("/organisation", func(r chi.Router) { - r.Post("/edit", organisation.Edit) - r.Delete("/remove", organisation.Remove) - r.Get("/read", organisation.Read) - r.Get("/search", organisation.Search) - r.Post("/archive", organisation.Archive) - }) - r.Route("/team", func(r chi.Router) { - r.Post("/edit", team.Edit) - r.Delete("/remove", team.Remove) - r.Get("/read", team.Read) - r.Get("/search", team.Search) - r.Post("/archive", team.Archive) - r.Post("/move", team.Move) - }) r.Route("/channel", func(r chi.Router) { r.Post("/edit", channel.Edit) r.Delete("/remove", channel.Remove) @@ -44,6 +29,21 @@ func MountRoutes(r chi.Router) { r.Post("/pin", message.Pin) r.Post("/flag", message.Flag) }) + r.Route("/organisation", func(r chi.Router) { + r.Post("/edit", organisation.Edit) + r.Delete("/remove", organisation.Remove) + r.Get("/read", organisation.Read) + r.Get("/search", organisation.Search) + r.Post("/archive", organisation.Archive) + }) + r.Route("/team", func(r chi.Router) { + r.Post("/edit", team.Edit) + r.Delete("/remove", team.Remove) + r.Get("/read", team.Read) + r.Get("/search", team.Search) + r.Post("/archive", team.Archive) + r.Post("/move", team.Move) + }) r.Route("/user", func(r chi.Router) { r.Post("/login", user.Login) r.Get("/search", user.Search)