mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
ts fixes, better stderr and stdout handling
This commit is contained in:
parent
395af28344
commit
0d1b229b46
@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
func jsonDump(v interface{}) {
|
func jsonDump(v interface{}) {
|
||||||
jsonBytes, err := json.MarshalIndent(v, "", " ")
|
jsonBytes, err := json.MarshalIndent(v, "", " ")
|
||||||
fmt.Println(err, string(jsonBytes))
|
fmt.Fprintln(os.Stderr, err, string(jsonBytes))
|
||||||
}
|
}
|
||||||
func usage() {
|
func usage() {
|
||||||
fmt.Println("Usage")
|
fmt.Println("Usage")
|
||||||
@ -27,7 +27,7 @@ func main() {
|
|||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
if len(*flagTsModule) == 0 {
|
if len(*flagTsModule) == 0 {
|
||||||
fmt.Println("missing ts module")
|
fmt.Fprintln(os.Stderr, "missing ts module")
|
||||||
}
|
}
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
@ -39,33 +39,40 @@ func main() {
|
|||||||
goPath := os.Getenv("GOPATH")
|
goPath := os.Getenv("GOPATH")
|
||||||
|
|
||||||
if len(goPath) == 0 {
|
if len(goPath) == 0 {
|
||||||
fmt.Println("GOPATH not set")
|
fmt.Fprintln(os.Stderr, "GOPATH not set")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
longPackageName := args[0]
|
longPackageName := args[0]
|
||||||
longPackageNameParts := strings.Split(longPackageName, "/")
|
longPackageNameParts := strings.Split(longPackageName, "/")
|
||||||
goFilename := path.Join(goPath, "src", longPackageName, "gotsrpc.go")
|
goFilename := path.Join(goPath, "src", longPackageName, "gotsrpc.go")
|
||||||
|
|
||||||
|
_, err := os.Stat(goFilename)
|
||||||
|
if err == nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "removing existing", goFilename)
|
||||||
|
os.Remove(goFilename)
|
||||||
|
}
|
||||||
|
|
||||||
packageName := longPackageNameParts[len(longPackageNameParts)-1]
|
packageName := longPackageNameParts[len(longPackageNameParts)-1]
|
||||||
services, structs, err := gotsrpc.Read(goPath, longPackageName, args[1:])
|
services, structs, err := gotsrpc.Read(goPath, longPackageName, args[1:])
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("an error occured", err)
|
fmt.Fprintln(os.Stderr, "an error occured", err)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
jsonDump(services)
|
//jsonDump(services)
|
||||||
jsonDump(structs)
|
//jsonDump(structs)
|
||||||
|
|
||||||
ts, err := gotsrpc.RenderTypeScript(services, structs, *flagTsModule)
|
ts, err := gotsrpc.RenderTypeScript(services, structs, *flagTsModule)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("could not generate ts code", err)
|
fmt.Fprintln(os.Stderr, "could not generate ts code", err)
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(ts)
|
fmt.Println(ts)
|
||||||
|
|
||||||
gocode, goerr := gotsrpc.RenderGo(services, packageName)
|
gocode, goerr := gotsrpc.RenderGo(services, packageName)
|
||||||
if goerr != nil {
|
if goerr != nil {
|
||||||
fmt.Println("could not generate go code", goerr)
|
fmt.Fprintln(os.Stderr, "could not generate go code", goerr)
|
||||||
os.Exit(4)
|
os.Exit(4)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,15 +80,14 @@ func main() {
|
|||||||
if formattingError == nil {
|
if formattingError == nil {
|
||||||
gocode = string(formattedGoBytes)
|
gocode = string(formattedGoBytes)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("could not format go code", formattingError)
|
fmt.Fprintln(os.Stderr, "could not format go code", formattingError)
|
||||||
}
|
}
|
||||||
|
|
||||||
writeErr := ioutil.WriteFile(goFilename, []byte(gocode), 0644)
|
writeErr := ioutil.WriteFile(goFilename, []byte(gocode), 0644)
|
||||||
if writeErr != nil {
|
if writeErr != nil {
|
||||||
fmt.Println("could not write go source to file", writeErr)
|
fmt.Fprintln(os.Stderr, "could not write go source to file", writeErr)
|
||||||
os.Exit(5)
|
os.Exit(5)
|
||||||
|
|
||||||
}
|
}
|
||||||
fmt.Println(goFilename, gocode)
|
//fmt.Println(goFilename, gocode)
|
||||||
//gotsrpc.ReadFile("/Users/jan/go/src/github.com/foomo/gotsrpc/demo/demo.go", []string{"Service"})
|
//gotsrpc.ReadFile("/Users/jan/go/src/github.com/foomo/gotsrpc/demo/demo.go", []string{"Service"})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,53 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type ServiceGoTSRPCProxy struct {
|
||||||
|
EndPoint string
|
||||||
|
service *Service
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewServiceGoTSRPCProxy(service *Service, endpoint string) *ServiceGoTSRPCProxy {
|
||||||
|
return &ServiceGoTSRPCProxy{
|
||||||
|
EndPoint: endpoint,
|
||||||
|
service: service,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP exposes your service
|
||||||
|
func (p *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
gotsrpc.ErrorMethodNotAllowed(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var args []interface{}
|
||||||
|
switch gotsrpc.GetCalledFunc(r, p.EndPoint) {
|
||||||
|
case "Hello":
|
||||||
|
args = []interface{}{""}
|
||||||
|
err := gotsrpc.LoadArgs(args, r)
|
||||||
|
if err != nil {
|
||||||
|
gotsrpc.ErrorCouldNotLoadArgs(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
helloReply, helloErr := p.service.Hello(args[0].(string))
|
||||||
|
gotsrpc.Reply([]interface{}{helloReply, helloErr}, w)
|
||||||
|
return
|
||||||
|
case "NothingInNothinOut":
|
||||||
|
p.service.NothingInNothinOut()
|
||||||
|
gotsrpc.Reply([]interface{}{}, w)
|
||||||
|
return
|
||||||
|
case "ExtractAddress":
|
||||||
|
args = []interface{}{&Person{}}
|
||||||
|
err := gotsrpc.LoadArgs(args, r)
|
||||||
|
if err != nil {
|
||||||
|
gotsrpc.ErrorCouldNotLoadArgs(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
extractAddressAddr, extractAddressE := p.service.ExtractAddress(args[0].(*Person))
|
||||||
|
gotsrpc.Reply([]interface{}{extractAddressAddr, extractAddressE}, w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type FooGoTSRPCProxy struct {
|
type FooGoTSRPCProxy struct {
|
||||||
EndPoint string
|
EndPoint string
|
||||||
service *Foo
|
service *Foo
|
||||||
@ -38,50 +85,3 @@ func (p *FooGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServiceGoTSRPCProxy struct {
|
|
||||||
EndPoint string
|
|
||||||
service *Service
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServiceGoTSRPCProxy(service *Service, endpoint string) *ServiceGoTSRPCProxy {
|
|
||||||
return &ServiceGoTSRPCProxy{
|
|
||||||
EndPoint: endpoint,
|
|
||||||
service: service,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeHTTP exposes your service
|
|
||||||
func (p *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != "POST" {
|
|
||||||
gotsrpc.ErrorMethodNotAllowed(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var args []interface{}
|
|
||||||
switch gotsrpc.GetCalledFunc(r, p.EndPoint) {
|
|
||||||
case "ExtractAddress":
|
|
||||||
args = []interface{}{&Person{}}
|
|
||||||
err := gotsrpc.LoadArgs(args, r)
|
|
||||||
if err != nil {
|
|
||||||
gotsrpc.ErrorCouldNotLoadArgs(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
extractAddressAddr, extractAddressE := p.service.ExtractAddress(args[0].(*Person))
|
|
||||||
gotsrpc.Reply([]interface{}{extractAddressAddr, extractAddressE}, w)
|
|
||||||
return
|
|
||||||
case "Hello":
|
|
||||||
args = []interface{}{""}
|
|
||||||
err := gotsrpc.LoadArgs(args, r)
|
|
||||||
if err != nil {
|
|
||||||
gotsrpc.ErrorCouldNotLoadArgs(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
helloReply, helloErr := p.service.Hello(args[0].(string))
|
|
||||||
gotsrpc.Reply([]interface{}{helloReply, helloErr}, w)
|
|
||||||
return
|
|
||||||
case "NothingInNothinOut":
|
|
||||||
p.service.NothingInNothinOut()
|
|
||||||
gotsrpc.Reply([]interface{}{}, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,40 +1 @@
|
|||||||
package demo_test
|
package demo_test
|
||||||
|
|
||||||
import (
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/foomo/gotsrpc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ServiceGoTSRPCProxy struct {
|
|
||||||
EndPoint string
|
|
||||||
service *Service
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewServiceGoTSRPCProxy(service *Service, endpoint string) *ServiceGoTSRPCProxy {
|
|
||||||
return &ServiceGoTSRPCProxy{
|
|
||||||
EndPoint: endpoint,
|
|
||||||
service: service,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sp *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != "POST" {
|
|
||||||
gotsrpc.ErrorMethodNotAllowed(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
switch gotsrpc.GetCalledFunc(r, sp.EndPoint) {
|
|
||||||
case "Hello":
|
|
||||||
args := []interface{}{""}
|
|
||||||
err := gotsrpc.LoadArgs(args, r)
|
|
||||||
if err != nil {
|
|
||||||
gotsrpc.ErrorCouldNotLoadArgs(w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
helloReply, helloErr := sp.service.Hello(args[0].(string))
|
|
||||||
gotsrpc.Reply([]interface{}{helloReply, helloErr}, w)
|
|
||||||
return
|
|
||||||
default:
|
|
||||||
gotsrpc.ErrorFuncNotFound(w)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
9
go.go
9
go.go
@ -87,18 +87,25 @@ func renderServiceProxies(services []*Service, packageName string, g *code) erro
|
|||||||
g.l(`
|
g.l(`
|
||||||
type ` + proxyName + ` struct {
|
type ` + proxyName + ` struct {
|
||||||
EndPoint string
|
EndPoint string
|
||||||
|
allowOrigin []string
|
||||||
service *` + service.Name + `
|
service *` + service.Name + `
|
||||||
}
|
}
|
||||||
|
|
||||||
func New` + proxyName + `(service *` + service.Name + `, endpoint string) *` + proxyName + ` {
|
func New` + proxyName + `(service *` + service.Name + `, endpoint string, allowOrigin []string) *` + proxyName + ` {
|
||||||
return &` + proxyName + `{
|
return &` + proxyName + `{
|
||||||
EndPoint: endpoint,
|
EndPoint: endpoint,
|
||||||
|
allowOrigin : allowOrigin,
|
||||||
service: service,
|
service: service,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP exposes your service
|
// ServeHTTP exposes your service
|
||||||
func (p *` + proxyName + `) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (p *` + proxyName + `) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
for _, origin := range p.allowOrigin {
|
||||||
|
w.Header().Add("Access-Control-Allow-Origin", origin)
|
||||||
|
}
|
||||||
|
|
||||||
if r.Method != "POST" {
|
if r.Method != "POST" {
|
||||||
gotsrpc.ErrorMethodNotAllowed(w)
|
gotsrpc.ErrorMethodNotAllowed(w)
|
||||||
return
|
return
|
||||||
|
|||||||
@ -70,7 +70,7 @@ func parsePackage(goPath string, packageName string) (pkg *ast.Package, err erro
|
|||||||
strippedPackageName := packageNameParts[len(packageNameParts)-1]
|
strippedPackageName := packageNameParts[len(packageNameParts)-1]
|
||||||
foundPackages := []string{}
|
foundPackages := []string{}
|
||||||
for pkgName, pkg := range pkgs {
|
for pkgName, pkg := range pkgs {
|
||||||
fmt.Println("pkgName", pkgName)
|
//fmt.Println("pkgName", pkgName)
|
||||||
if pkgName == strippedPackageName {
|
if pkgName == strippedPackageName {
|
||||||
return pkg, nil
|
return pkg, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package gotsrpc
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"go/ast"
|
"go/ast"
|
||||||
"reflect"
|
"reflect"
|
||||||
)
|
)
|
||||||
@ -27,7 +26,7 @@ func readServiceFile(file *ast.File, services []*Service) error {
|
|||||||
starExpr := firstReceiverField.Type.(*ast.StarExpr)
|
starExpr := firstReceiverField.Type.(*ast.StarExpr)
|
||||||
if "*ast.Ident" == reflect.ValueOf(starExpr.X).Type().String() {
|
if "*ast.Ident" == reflect.ValueOf(starExpr.X).Type().String() {
|
||||||
ident := starExpr.X.(*ast.Ident)
|
ident := starExpr.X.(*ast.Ident)
|
||||||
fmt.Println(" on sth:", ident.Name)
|
trace(" on sth:", ident.Name)
|
||||||
|
|
||||||
service, ok := findService(ident.Name)
|
service, ok := findService(ident.Name)
|
||||||
|
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package gotsrpc
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
"go/ast"
|
||||||
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -23,7 +24,7 @@ func readStructs(pkg *ast.Package) (structs map[string]*Struct, err error) {
|
|||||||
|
|
||||||
func trace(args ...interface{}) {
|
func trace(args ...interface{}) {
|
||||||
if ReaderTrace {
|
if ReaderTrace {
|
||||||
fmt.Println(args...)
|
fmt.Fprintln(os.Stderr, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +129,7 @@ func readAstArrayType(v *Value, arrayType *ast.ArrayType) {
|
|||||||
case "*ast.StarExpr":
|
case "*ast.StarExpr":
|
||||||
readAstStarExpr(v, arrayType.Elt.(*ast.StarExpr))
|
readAstStarExpr(v, arrayType.Elt.(*ast.StarExpr))
|
||||||
default:
|
default:
|
||||||
fmt.Println("array type elt", reflect.ValueOf(arrayType.Elt).Type().String())
|
trace("array type elt", reflect.ValueOf(arrayType.Elt).Type().String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,7 +156,7 @@ func readAstSelectorExpr(v *Value, selectorExpr *ast.SelectorExpr) {
|
|||||||
Name: selectorExpr.Sel.Name,
|
Name: selectorExpr.Sel.Name,
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
fmt.Println("selectorExpr.Sel !?", selectorExpr.X, reflect.ValueOf(selectorExpr.X).Type().String())
|
trace("selectorExpr.Sel !?", selectorExpr.X, reflect.ValueOf(selectorExpr.X).Type().String())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +184,7 @@ func (v *Value) loadExpr(expr ast.Expr) {
|
|||||||
}
|
}
|
||||||
readAstMapType(v.Array.Value.Map, fieldArray.Elt.(*ast.MapType))
|
readAstMapType(v.Array.Value.Map, fieldArray.Elt.(*ast.MapType))
|
||||||
default:
|
default:
|
||||||
fmt.Println("---------------------> array of", reflect.ValueOf(fieldArray.Elt).Type().String())
|
trace("---------------------> array of", reflect.ValueOf(fieldArray.Elt).Type().String())
|
||||||
}
|
}
|
||||||
case "*ast.Ident":
|
case "*ast.Ident":
|
||||||
fieldIdent := expr.(*ast.Ident)
|
fieldIdent := expr.(*ast.Ident)
|
||||||
@ -240,9 +241,9 @@ func readFieldList(fieldList []*ast.Field) (fields []*Field) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func extractStructs(file *ast.File, structs map[string]*Struct) error {
|
func extractStructs(file *ast.File, structs map[string]*Struct) error {
|
||||||
for _, imp := range file.Imports {
|
//for _, imp := range file.Imports {
|
||||||
fmt.Println("import", imp.Name, imp.Path)
|
// fmt.Println("import", imp.Name, imp.Path)
|
||||||
}
|
//}
|
||||||
for name, obj := range file.Scope.Objects {
|
for name, obj := range file.Scope.Objects {
|
||||||
//fmt.Println(name, obj.Kind, obj.Data)
|
//fmt.Println(name, obj.Kind, obj.Data)
|
||||||
if obj.Kind == ast.Typ && obj.Decl != nil {
|
if obj.Kind == ast.Typ && obj.Decl != nil {
|
||||||
|
|||||||
@ -35,6 +35,9 @@ func (v *Value) tsType() string {
|
|||||||
func renderStruct(str *Struct, ts *code) error {
|
func renderStruct(str *Struct, ts *code) error {
|
||||||
ts.l("export interface " + str.Name + " {").ind(1)
|
ts.l("export interface " + str.Name + " {").ind(1)
|
||||||
for _, f := range str.Fields {
|
for _, f := range str.Fields {
|
||||||
|
if f.JSONInfo != nil && f.JSONInfo.Ignore {
|
||||||
|
continue
|
||||||
|
}
|
||||||
ts.app(f.tsName())
|
ts.app(f.tsName())
|
||||||
if f.Value.IsPtr {
|
if f.Value.IsPtr {
|
||||||
ts.app("?")
|
ts.app("?")
|
||||||
@ -88,11 +91,11 @@ func renderService(service *Service, ts *code) error {
|
|||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
ts.app(", ")
|
ts.app(", ")
|
||||||
}
|
}
|
||||||
ts.app("success(" + strings.Join(retArgs, ", ") + ") => void")
|
ts.app("success:(" + strings.Join(retArgs, ", ") + ") => void")
|
||||||
ts.app(", err:(request:XMLHttpRequest) => void) {").nl()
|
ts.app(", err:(request:XMLHttpRequest) => void) {").nl()
|
||||||
ts.ind(1)
|
ts.ind(1)
|
||||||
// generic framework call
|
// generic framework call
|
||||||
ts.l("GoTSRPC.call(this.endPoint, \"" + method.Name + "\"), [" + strings.Join(callArgs, ", ") + "], success, err);")
|
ts.l("GoTSRPC.call(this.endPoint, \"" + method.Name + "\", [" + strings.Join(callArgs, ", ") + "], success, err);")
|
||||||
ts.ind(-1)
|
ts.ind(-1)
|
||||||
ts.app("}")
|
ts.app("}")
|
||||||
ts.nl()
|
ts.nl()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user