mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
added basic php client generation for ts rcp services
This commit is contained in:
parent
f4e3c7dd66
commit
d09e436833
14
build.go
14
build.go
@ -7,12 +7,11 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"path/filepath"
|
|
||||||
|
|
||||||
"github.com/foomo/gotsrpc/config"
|
"github.com/foomo/gotsrpc/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -154,6 +153,17 @@ func Build(conf *config.Config, goPath string) {
|
|||||||
}
|
}
|
||||||
formatAndWrite(goRPCClientsCode, goRPCClientsFilename)
|
formatAndWrite(goRPCClientsCode, goRPCClientsFilename)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(target.PHPRPC) > 0 {
|
||||||
|
phpRPCClientsCode, goerr := RenderPHPRPCClients(services, target)
|
||||||
|
if goerr != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, " could not generate php rpc clients code in target", name, goerr)
|
||||||
|
os.Exit(4)
|
||||||
|
}
|
||||||
|
for filename, code := range phpRPCClientsCode {
|
||||||
|
updateCode(filename, code)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// spew.Dump(mappedTypeScript)
|
// spew.Dump(mappedTypeScript)
|
||||||
|
|||||||
@ -8,6 +8,11 @@ import (
|
|||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type PHPTarget struct {
|
||||||
|
Out string `yaml:"out"`
|
||||||
|
Namespace string `yaml:"namespace"`
|
||||||
|
}
|
||||||
|
|
||||||
type Target struct {
|
type Target struct {
|
||||||
Package string `yaml:"package"`
|
Package string `yaml:"package"`
|
||||||
Services map[string]string `yaml:"services"`
|
Services map[string]string `yaml:"services"`
|
||||||
@ -15,6 +20,7 @@ type Target struct {
|
|||||||
Out string `yaml:"out"`
|
Out string `yaml:"out"`
|
||||||
GoRPC []string `yaml:"gorpc"`
|
GoRPC []string `yaml:"gorpc"`
|
||||||
TSRPC []string `yaml:"tsrpc"`
|
TSRPC []string `yaml:"tsrpc"`
|
||||||
|
PHPRPC map[string]*PHPTarget `yaml:"phprpc"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Target) IsGoRPC(service string) bool {
|
func (t *Target) IsGoRPC(service string) bool {
|
||||||
@ -38,6 +44,18 @@ func (t *Target) IsTSRPC(service string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Target) IsPHPRPC(service string) bool {
|
||||||
|
if len(t.PHPRPC) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok := t.PHPRPC[service]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Target) GetPHPTarget(service string) *PHPTarget {
|
||||||
|
return t.PHPRPC[service]
|
||||||
|
}
|
||||||
|
|
||||||
type Mapping struct {
|
type Mapping struct {
|
||||||
GoPackage string `yaml:"-"`
|
GoPackage string `yaml:"-"`
|
||||||
Out string `yaml:"out"`
|
Out string `yaml:"out"`
|
||||||
|
|||||||
106
php.go
Normal file
106
php.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package gotsrpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/foomo/gotsrpc/config"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func renderPHPRPCServiceClients(service *Service, namespce string, g *code) error {
|
||||||
|
g.l(`<?php`)
|
||||||
|
g.l(`// this file was auto generated by gotsrpc https://github.com/foomo/gotsrpc`)
|
||||||
|
g.nl()
|
||||||
|
g.l(`namespace ` + namespce + `;`)
|
||||||
|
g.nl()
|
||||||
|
|
||||||
|
g.l(`class ` + service.Name + `Client`)
|
||||||
|
g.l(`{`)
|
||||||
|
g.ind(1)
|
||||||
|
|
||||||
|
// Variables
|
||||||
|
g.l(`public static $defaultOptions = ['http' => ['method' => 'POST', 'timeout' => 5, 'header' => 'Content-type: application/json']];`)
|
||||||
|
g.nl()
|
||||||
|
g.l(`private $options;`)
|
||||||
|
g.l(`private $endpoint;`)
|
||||||
|
g.nl()
|
||||||
|
|
||||||
|
// Constructor
|
||||||
|
g.l(`public function __constructor($endpoint, $options=null)`)
|
||||||
|
g.l(`{`)
|
||||||
|
g.ind(1)
|
||||||
|
g.l(`$this->endpoint = $endpoint;`)
|
||||||
|
g.l(`$this->options = (is_null($options)) ? self::$defaultOptions : $options;`)
|
||||||
|
g.ind(-1)
|
||||||
|
g.l(`}`)
|
||||||
|
g.nl()
|
||||||
|
|
||||||
|
// Service methods
|
||||||
|
for _, method := range service.Methods {
|
||||||
|
args := []string{}
|
||||||
|
params := []string{}
|
||||||
|
for _, a := range method.Args {
|
||||||
|
args = append(args, "$"+a.Name)
|
||||||
|
params = append(params, "$"+a.Name)
|
||||||
|
}
|
||||||
|
//rets := []string{}
|
||||||
|
//returns := []string{}
|
||||||
|
//for i, r := range method.Return {
|
||||||
|
// name := r.Name
|
||||||
|
// if len(name) == 0 {
|
||||||
|
// name = fmt.Sprintf("ret%s_%d", method.Name, i)
|
||||||
|
// }
|
||||||
|
// rets = append(rets, "&"+name)
|
||||||
|
// returns = append(returns, name+" "+r.Value.goType(aliases, fullPackageName))
|
||||||
|
//}
|
||||||
|
//returns = append(returns, "clientErr error")
|
||||||
|
//g.l(`func (c *` + clientName + `) ` + method.Name + `(` + strings.Join(params, ", ") + `) (` + strings.Join(returns, ", ") + `) {`)
|
||||||
|
//g.l(`args := []interface{}{` + strings.Join(args, ", ") + `}`)
|
||||||
|
//g.l(`reply := []interface{}{` + strings.Join(rets, ", ") + `}`)
|
||||||
|
//g.l(`clientErr = gotsrpc.CallClient(c.URL, c.EndPoint, "` + method.Name + `", args, reply)`)
|
||||||
|
//g.l(`return`)
|
||||||
|
//g.l(`}`)
|
||||||
|
//g.nl()
|
||||||
|
|
||||||
|
g.l(`public function ` + lcfirst(method.Name) + `(` + strings.Join(params, ", ") + `)`)
|
||||||
|
g.l(`{`)
|
||||||
|
g.ind(1)
|
||||||
|
g.l(`return $this->call('` + method.Name + `', [` + strings.Join(params, ", ") + `]);`)
|
||||||
|
g.ind(-1)
|
||||||
|
g.l(`}`)
|
||||||
|
g.nl()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Protected methods
|
||||||
|
g.l(`protected function call($method, array $request)`)
|
||||||
|
g.l(`{`)
|
||||||
|
g.ind(1)
|
||||||
|
g.l(`$options = $this->options;`)
|
||||||
|
g.l(`$options['http']['content'] = json_encode($request);`)
|
||||||
|
g.l(`$options['http']['content'] = json_encode($request);`)
|
||||||
|
g.l(`return json_decode(file_get_contents($this->endpoint . '/' . $method, false, stream_context_create($options)));`)
|
||||||
|
g.ind(-1)
|
||||||
|
g.l(`}`)
|
||||||
|
g.nl()
|
||||||
|
|
||||||
|
g.ind(-1)
|
||||||
|
g.l(`}`)
|
||||||
|
g.nl()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RenderPHPRPCClients(services map[string]*Service, config *config.Target) (code map[string]string, err error) {
|
||||||
|
code = map[string]string{}
|
||||||
|
for _, service := range services {
|
||||||
|
// Check if we should render this service as ts rcp
|
||||||
|
// Note: remove once there's a separate gorcp generator
|
||||||
|
if !config.IsPHPRPC(service.Name) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
target := config.GetPHPTarget(service.Name)
|
||||||
|
g := newCode(" ")
|
||||||
|
if err = renderPHPRPCServiceClients(service, target.Namespace, g); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
code[target.Out+"/"+service.Name+"Client.php"] = g.string()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user