3
0

upd(messaging): update codegen, add spec to codegen

This commit is contained in:
Tit Petric
2019-02-05 15:11:52 +01:00
parent e6434e502b
commit ba5413b583
3 changed files with 185 additions and 120 deletions

View File

@@ -22,9 +22,9 @@ function permissions {
CGO_ENABLED=0 go build -o ./build/gen-permissions codegen/v2/permissions.go
fi
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rules.OperationGroup" -input sam/types/permissions/1-organisation.json -output sam/types/organisation.perms.gen.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rules.OperationGroup" -input sam/types/permissions/2-team.json -output sam/types/team.perms.gen.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rules.OperationGroup" -input sam/types/permissions/3-channel.json -output sam/types/channel.perms.gen.go
./build/gen-permissions -package types -function "func (c *Organisation) Permissions() []rules.OperationGroup" -input messaging/types/permissions/1-organisation.json -output messaging/types/organisation.perms.gen.go
./build/gen-permissions -package types -function "func (c *Team) Permissions() []rules.OperationGroup" -input messaging/types/permissions/2-team.json -output messaging/types/team.perms.gen.go
./build/gen-permissions -package types -function "func (c *Channel) Permissions() []rules.OperationGroup" -input messaging/types/permissions/3-channel.json -output messaging/types/channel.perms.gen.go
green "OK"
}
@@ -42,14 +42,14 @@ function types {
./build/gen-type-set --with-primary-key=false --types ModuleField,RecordValue \
--output crm/types/type.other.gen.go
./build/gen-type-set --types MessageAttachment --output sam/types/attachment.gen.go
./build/gen-type-set --with-resources=true --types Channel --resource-type "types.Resource" --imports "github.com/crusttech/crust/system/types" --output sam/types/channel.gen.go
./build/gen-type-set --with-primary-key=false --types ChannelMember --output sam/types/channel_member.gen.go
./build/gen-type-set --with-primary-key=false --types Command,CommandParam --output sam/types/command.gen.go
./build/gen-type-set --types Mention --output sam/types/mention.gen.go
./build/gen-type-set --types MessageFlag --output sam/types/message_flag.gen.go
./build/gen-type-set --types Message --output sam/types/message.gen.go
./build/gen-type-set --with-primary-key=false --types Unread --output sam/types/unread.gen.go
./build/gen-type-set --types MessageAttachment --output messaging/types/attachment.gen.go
./build/gen-type-set --with-resources=true --types Channel --resource-type "types.Resource" --imports "github.com/crusttech/crust/system/types" --output messaging/types/channel.gen.go
./build/gen-type-set --with-primary-key=false --types ChannelMember --output messaging/types/channel_member.gen.go
./build/gen-type-set --with-primary-key=false --types Command,CommandParam --output messaging/types/command.gen.go
./build/gen-type-set --types Mention --output messaging/types/mention.gen.go
./build/gen-type-set --types MessageFlag --output messaging/types/message_flag.gen.go
./build/gen-type-set --types Message --output messaging/types/message.gen.go
./build/gen-type-set --with-primary-key=false --types Unread --output messaging/types/unread.gen.go
./build/gen-type-set --types User --output system/types/user.gen.go
./build/gen-type-set --with-resources=true --resource-type "Resource" --types Team --output system/types/team.gen.go
@@ -88,21 +88,29 @@ function files {
files
_PWD=$PWD
SPECS=$(find $PWD -name 'spec.json' | xargs -n1 dirname)
for SPEC in $SPECS; do
yellow "> spec $SPEC"
cd $SPEC && rm -rf spec && /usr/bin/env spec && cd $_PWD
green "OK"
done
for SPEC in $SPECS; do
SRC=$(basename $SPEC)
if [ -d "codegen/$SRC" ]; then
yellow "> README $SRC"
codegen/codegen.php $SRC
green "OK"
function specs {
yellow "> specs"
if [ ! -f "build/gen-spec" ]; then
CGO_ENABLED=0 go build -o ./build/gen-spec codegen/v2/spec.go
fi
done
_PWD=$PWD
SPECS=$(find $PWD -name 'spec.json' | xargs -n1 dirname)
for SPEC in $SPECS; do
yellow "> spec $SPEC"
cd $SPEC && rm -rf spec && ../../build/gen-spec && cd $_PWD
green "OK"
done
for SPEC in $SPECS; do
SRC=$(basename $SPEC)
if [ -d "codegen/$SRC" ]; then
yellow "> README $SRC"
codegen/codegen.php $SRC
green "OK"
fi
done
}
specs
gofmt

View File

@@ -1,94 +0,0 @@
#!/usr/bin/env php
<?php
error_reporting(E_ALL^E_NOTICE);
include("docs/vendor/autoload.php");
function capitalize($name) {
$names = explode("/", $name);
return implode("", array_map("ucfirst", $names));
}
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_paths(array(__DIR__ . "/"));
$tpl->set_compile_location("/tmp", true);
$tpl->add_default("newline", "\n");
$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 (is_array($api['struct'])) {
$name = ucfirst($api['interface']);
$filename = str_replace("..", ".", strtolower($name) . "." . $type . ".go");
$tpl->load("http_$type.tpl");
$tpl->assign("parsers", array(
"uint64" => "parseUInt64",
"bool" => "parseBool",
));
$tpl->assign("package", $api['package']);
$tpl->assign("name", $name);
$tpl->assign("self", strtolower(substr($name, 0, 1)));
$tpl->assign("api", $api);
$tpl->assign("structs", $api['struct']);
$imports = array();
foreach ($api['struct'] as $struct) {
if (isset($struct['imports']))
foreach ($struct['imports'] as $import) {
$imports[] = $import;
}
}
$tpl->assign("imports", $imports);
$tpl->assign("calls", $api['apis']);
$contents = str_replace("\n\n}", "\n}", $tpl->get());
$save = true;
if ($type === "" && file_exists($filename)) {
$save = false;
}
if ($save) {
file_put_contents($filename, $contents);
}
}
}
}
foreach (array("routes") as $type) {
$name = ucfirst($api['interface']);
$filename = str_replace("..", ".", $type . ".go");
$tpl->load("http_$type.tpl");
$tpl->assign("package", reset($apis)['package']);
$tpl->assign("apis", $apis);
$contents = $tpl->get();
file_put_contents($filename, $contents);
}
// camel case to snake case
function decamel($input) {
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
}
return implode('_', $ret);
}

151
codegen/v2/spec.go Normal file
View File

@@ -0,0 +1,151 @@
package main
import (
"fmt"
"log"
"os"
"strings"
"encoding/json"
"io/ioutil"
"github.com/davecgh/go-spew/spew"
)
type SpecFile []*SpecEntry
type SpecEntry struct {
Title string
Description string
Protocol string
Authentication []string
Entrypoint string
Path string
Struct interface{}
Parameters map[string]interface{}
APIs []*SpecAPI
}
func (s *SpecEntry) toOutFile() OutFile {
file := OutFile{
APIs: []*OutFileAPI{},
}
s.applyToOutFile(&file)
return file
}
func (s *SpecEntry) applyToOutFile(o *OutFile) {
// reset title/interface/path to spec data
o.Title = s.Title
o.Description = s.Description
o.Parameters = s.Parameters
o.Interface = strings.ToUpper(s.Entrypoint[0:1]) + s.Entrypoint[1:]
o.Path = s.Path
if o.Path == "" {
o.Path = "/" + s.Entrypoint
}
o.Struct = s.Struct
o.Protocol = s.Protocol
o.Authentication = s.Authentication
namedAPIs := o.NamedAPIs()
for _, val := range s.APIs {
path := val.Path
if path == "" {
path = "/" + val.Name
}
// add new API calls
call, ok := namedAPIs[val.Name]
if !ok {
o.APIs = append(o.APIs, &OutFileAPI{
Name: val.Name,
Method: val.Method,
Title: val.Title,
Path: path,
Parameters: val.Parameters,
})
} else {
// update title/method/path of existing APIs
call.Name = val.Name
call.Title = val.Title
call.Method = val.Method
call.Path = path
if val.Parameters != nil {
call.Parameters = val.Parameters
}
}
}
}
type SpecAPI struct {
Name string
Method string
Title string
Path string
Parameters map[string]interface{}
}
type OutFile struct {
Title string
Description string `json:",omitempty"`
Interface string
Struct interface{}
Parameters map[string]interface{}
Protocol string
Authentication []string
Path string
APIs []*OutFileAPI
}
func (o *OutFile) NamedAPIs() map[string]*OutFileAPI {
apis := map[string]*OutFileAPI{}
for _, api := range o.APIs {
apis[api.Name] = api
}
return apis
}
type OutFileAPI struct {
Name string
Method string
Title string
Description string `json:",omitempty"`
Path string
Parameters map[string]interface{}
}
func main() {
debug := false
raw, err := ioutil.ReadFile("./spec.json")
if err != nil {
log.Fatal(err)
}
var spec SpecFile
json.Unmarshal(raw, &spec)
os.Mkdir("./spec", 0755)
for _, val := range spec {
filename := val.Entrypoint + ".json"
var file OutFile
contents, err := ioutil.ReadFile("./" + filename)
if err != nil {
file = val.toOutFile()
} else {
err = json.Unmarshal(contents, &file)
if err != nil {
log.Fatal("Error parsing ", filename, ": ", err)
}
val.applyToOutFile(&file)
}
raw, _ := json.MarshalIndent(file, "", " ")
ioutil.WriteFile("./spec/"+filename, raw, 0644)
fmt.Println(filename)
}
if debug {
spew.Dump(spec)
}
}