mirror of
https://github.com/foomo/gotsrpc.git
synced 2025-10-16 12:35:35 +00:00
nearing 0.1 with build file
This commit is contained in:
parent
df6b3449b1
commit
8d6f30e6b4
120
build.go
Normal file
120
build.go
Normal file
@ -0,0 +1,120 @@
|
||||
package gotsrpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/gotsrpc/config"
|
||||
)
|
||||
|
||||
func Build(conf *config.Config, goPath string) {
|
||||
mappedTypeScript := map[string]map[string]*code{}
|
||||
for name, target := range conf.Targets {
|
||||
fmt.Fprintln(os.Stderr, "building target", name)
|
||||
longPackageName := target.Package
|
||||
longPackageNameParts := strings.Split(longPackageName, "/")
|
||||
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]
|
||||
services, structs, err := Read(goPath, longPackageName, target.Services)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, " an error occured while trying to understand your code", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
ts, err := RenderTypeScriptServices(services, conf.Mappings, target.TypeScriptModule)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, " could not generate ts code", err)
|
||||
os.Exit(3)
|
||||
}
|
||||
|
||||
// fmt.Fprintln(os.Stdout, ts)
|
||||
updateErr := updateCode(target.Out, ts)
|
||||
if updateErr != nil {
|
||||
fmt.Fprintln(os.Stderr, " could not write service file", target.Out, updateErr)
|
||||
os.Exit(3)
|
||||
}
|
||||
err = RenderStructsToPackages(structs, conf.Mappings, mappedTypeScript)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "struct gen err for target", name, err)
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
gocode, goerr := RenderGo(services, packageName)
|
||||
if goerr != nil {
|
||||
fmt.Fprintln(os.Stderr, " could not generate go code in target", name, goerr)
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
formattedGoBytes, formattingError := format.Source([]byte(gocode))
|
||||
if formattingError == nil {
|
||||
gocode = string(formattedGoBytes)
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, " could not format go code", formattingError)
|
||||
}
|
||||
|
||||
writeErr := ioutil.WriteFile(goFilename, []byte(gocode), 0644)
|
||||
if writeErr != nil {
|
||||
fmt.Fprintln(os.Stderr, " could not write go source to file", writeErr)
|
||||
os.Exit(5)
|
||||
}
|
||||
}
|
||||
|
||||
for goPackage, mappedStructsMap := range mappedTypeScript {
|
||||
mapping, ok := conf.Mappings[goPackage]
|
||||
if !ok {
|
||||
fmt.Fprintln(os.Stderr, "reverse mapping error in struct generation for package", goPackage)
|
||||
os.Exit(6)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, "building structs for go package", goPackage, "to ts module", mapping.TypeScriptModule, "in file", mapping.Out)
|
||||
moduleCode := newCode().l("module " + mapping.TypeScriptModule + "{").ind(1)
|
||||
structNames := []string{}
|
||||
|
||||
for structName := range mappedStructsMap {
|
||||
structNames = append(structNames, structName)
|
||||
}
|
||||
sort.Strings(structNames)
|
||||
for _, structName := range structNames {
|
||||
structCode := mappedStructsMap[structName]
|
||||
moduleCode.app(structCode.ind(-1).l("").string())
|
||||
}
|
||||
moduleCode.ind(-1).l("}")
|
||||
updateErr := updateCode(mapping.Out, moduleCode.string())
|
||||
if updateErr != nil {
|
||||
fmt.Fprintln(os.Stderr, " failed to update code in", mapping.Out, updateErr)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func updateCode(file string, code string) error {
|
||||
if len(file) > 0 {
|
||||
if file[0] == '~' {
|
||||
home := os.Getenv("HOME")
|
||||
if len(home) == 0 {
|
||||
return errors.New("could not resolve home dir")
|
||||
}
|
||||
file = path.Join(home, file[1:])
|
||||
}
|
||||
}
|
||||
oldCode, _ := ioutil.ReadFile(file)
|
||||
if string(oldCode) != code {
|
||||
fmt.Println(" update file", file)
|
||||
return ioutil.WriteFile(file, []byte(code), 0644)
|
||||
}
|
||||
fmt.Println(" update file not necessary - unchanged", file)
|
||||
return nil
|
||||
}
|
||||
@ -4,11 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"go/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/foomo/gotsrpc"
|
||||
"github.com/foomo/gotsrpc/config"
|
||||
@ -20,17 +16,17 @@ func jsonDump(v interface{}) {
|
||||
}
|
||||
func usage() {
|
||||
fmt.Println("Usage")
|
||||
fmt.Println(os.Args[0], " path/to/build-config.yml [target, [target], ...]")
|
||||
fmt.Println(os.Args[0], " path/to/build-config.yml")
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
func main() {
|
||||
flag.Parse()
|
||||
args := flag.Args()
|
||||
if len(args) < 1 {
|
||||
if len(args) != 1 {
|
||||
usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
gotsrpc.ReaderTrace = true
|
||||
gotsrpc.ReaderTrace = false
|
||||
goPath := os.Getenv("GOPATH")
|
||||
|
||||
if len(goPath) == 0 {
|
||||
@ -40,86 +36,8 @@ func main() {
|
||||
|
||||
conf, err := config.LoadConfigFile(args[0])
|
||||
if err != nil {
|
||||
fmt.Println(os.Stderr, "config load error")
|
||||
fmt.Fprintln(os.Stderr, "config load error, could not load config from", args[0])
|
||||
os.Exit(2)
|
||||
}
|
||||
fmt.Println(conf, err)
|
||||
buildTargets := map[string]*config.Target{}
|
||||
if len(args) > 1 {
|
||||
for _, target := range args[1:] {
|
||||
fmt.Println(os.Stderr, "will build target", target)
|
||||
_, ok := conf.Targets[target]
|
||||
if !ok {
|
||||
fmt.Println(os.Stderr, "invalid target has to be one of:")
|
||||
for existingTarget := range conf.Targets {
|
||||
fmt.Println(os.Stderr, " ", existingTarget)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
buildTargets[target] = conf.Targets[target]
|
||||
}
|
||||
} else {
|
||||
fmt.Println(os.Stderr, "will build all targets in config")
|
||||
buildTargets = conf.Targets
|
||||
}
|
||||
fmt.Println(os.Stderr, buildTargets)
|
||||
|
||||
for name, target := range buildTargets {
|
||||
fmt.Println(os.Stderr, "building target", name)
|
||||
longPackageName := target.Package
|
||||
longPackageNameParts := strings.Split(longPackageName, "/")
|
||||
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]
|
||||
services, structs, err := gotsrpc.Read(goPath, longPackageName, target.Services)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "an error occured while trying to understand your code", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
ts, err := gotsrpc.RenderTypeScriptServices(services, conf.Mappings, target.TypeScriptModule)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "could not generate ts code", err)
|
||||
os.Exit(3)
|
||||
}
|
||||
|
||||
fmt.Println(os.Stdout, ts)
|
||||
|
||||
mappedCode, err := gotsrpc.RenderStructsToPackages(structs, conf.Mappings)
|
||||
if err != nil {
|
||||
fmt.Println("struct gen err", err)
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
for tsModule, code := range mappedCode {
|
||||
fmt.Println("-----------------", tsModule, "--------------------")
|
||||
fmt.Println(code)
|
||||
}
|
||||
|
||||
gocode, goerr := gotsrpc.RenderGo(services, packageName)
|
||||
if goerr != nil {
|
||||
fmt.Fprintln(os.Stderr, "could not generate go code", goerr)
|
||||
os.Exit(4)
|
||||
}
|
||||
|
||||
formattedGoBytes, formattingError := format.Source([]byte(gocode))
|
||||
if formattingError == nil {
|
||||
gocode = string(formattedGoBytes)
|
||||
} else {
|
||||
fmt.Fprintln(os.Stderr, "could not format go code", formattingError)
|
||||
}
|
||||
|
||||
writeErr := ioutil.WriteFile(goFilename, []byte(gocode), 0644)
|
||||
if writeErr != nil {
|
||||
fmt.Fprintln(os.Stderr, "could not write go source to file", writeErr)
|
||||
os.Exit(5)
|
||||
}
|
||||
}
|
||||
|
||||
gotsrpc.Build(conf, goPath)
|
||||
}
|
||||
|
||||
@ -28,7 +28,7 @@ type Config struct {
|
||||
|
||||
func LoadConfigFile(file string) (conf *Config, err error) {
|
||||
yamlBytes, readErr := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
if readErr != nil {
|
||||
err = readErr
|
||||
return
|
||||
}
|
||||
|
||||
2
go.go
2
go.go
@ -117,7 +117,7 @@ func renderServiceProxies(services []*Service, packageName string, g *code) erro
|
||||
for _, s := range services {
|
||||
for _, m := range s.Methods {
|
||||
extractImports(m.Args)
|
||||
extractImports(m.Return)
|
||||
// extractImports(m.Return)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -2,6 +2,7 @@ package gotsrpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"reflect"
|
||||
@ -175,8 +176,18 @@ func Read(goPath string, packageName string, serviceNames []string) (services []
|
||||
|
||||
func collectStructs(goPath string, structs map[string]*Struct) error {
|
||||
scannedPackages := map[string]map[string]*Struct{}
|
||||
missingStructs := func() []string {
|
||||
missing := []string{}
|
||||
for name, strct := range structs {
|
||||
if strct == nil {
|
||||
missing = append(missing, name)
|
||||
}
|
||||
}
|
||||
return missing
|
||||
}
|
||||
lastNumMissing := len(missingStructs())
|
||||
for structsPending(structs) {
|
||||
trace("pending", len(structs))
|
||||
trace("pending", missingStructs())
|
||||
for fullName, strct := range structs {
|
||||
if strct != nil {
|
||||
continue
|
||||
@ -207,7 +218,13 @@ func collectStructs(goPath string, structs map[string]*Struct) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
newNumMissingStructs := len(missingStructs())
|
||||
if newNumMissingStructs > 0 && newNumMissingStructs == lastNumMissing {
|
||||
return errors.New(fmt.Sprintln("could not resolve at least one of the following structs", missingStructs()))
|
||||
}
|
||||
lastNumMissing = newNumMissingStructs
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
122
test.js
Normal file
122
test.js
Normal file
@ -0,0 +1,122 @@
|
||||
var GoTSRPC;
|
||||
(function (GoTSRPC) {
|
||||
function call(endPoint, method, args, success, err) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.withCredentials = true;
|
||||
request.open('POST', endPoint + "/" + encodeURIComponent(method), true);
|
||||
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
||||
request.send(JSON.stringify(args));
|
||||
request.onload = function () {
|
||||
if (request.status == 200) {
|
||||
try {
|
||||
var data = JSON.parse(request.responseText);
|
||||
success.apply(null, data);
|
||||
}
|
||||
catch (e) {
|
||||
err(request);
|
||||
}
|
||||
}
|
||||
else {
|
||||
err(request);
|
||||
}
|
||||
};
|
||||
request.onerror = function () {
|
||||
err(request);
|
||||
};
|
||||
}
|
||||
GoTSRPC.call = call;
|
||||
})(GoTSRPC || (GoTSRPC = {}));
|
||||
var MZG;
|
||||
(function (MZG) {
|
||||
var Services;
|
||||
(function (Services) {
|
||||
var Order;
|
||||
(function (Order) {
|
||||
var ServiceClient = (function () {
|
||||
function ServiceClient(endPoint) {
|
||||
if (endPoint === void 0) { endPoint = "/service"; }
|
||||
this.endPoint = endPoint;
|
||||
}
|
||||
ServiceClient.prototype.validateOrder = function (success, err) {
|
||||
GoTSRPC.call(this.endPoint, "ValidateOrder", [], success, err);
|
||||
};
|
||||
ServiceClient.prototype.confirmOrder = function (success, err) {
|
||||
GoTSRPC.call(this.endPoint, "ConfirmOrder", [], success, err);
|
||||
};
|
||||
ServiceClient.prototype.flushPaymentInfo = function (success, err) {
|
||||
GoTSRPC.call(this.endPoint, "FlushPaymentInfo", [], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setPositionSize = function (oldItemID, newItemID, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetPositionSize", [oldItemID, newItemID], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setPositionCount = function (itemID, count, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetPositionCount", [itemID, count], success, err);
|
||||
};
|
||||
ServiceClient.prototype.getOrder = function (success, err) {
|
||||
GoTSRPC.call(this.endPoint, "GetOrder", [], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setBillingAddress = function (addressId, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetBillingAddress", [addressId], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setShippingAddress = function (addressId, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetShippingAddress", [addressId], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setBilling = function (billing, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetBilling", [billing], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setShipping = function (shipping, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetShipping", [shipping], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setPayment = function (payment, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetPayment", [payment], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setPage = function (page, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetPage", [page], success, err);
|
||||
};
|
||||
ServiceClient.prototype.getOrderSummary = function (success, err) {
|
||||
GoTSRPC.call(this.endPoint, "GetOrderSummary", [], success, err);
|
||||
};
|
||||
ServiceClient.defaultInst = new ServiceClient;
|
||||
return ServiceClient;
|
||||
}());
|
||||
Order.ServiceClient = ServiceClient;
|
||||
})(Order = Services.Order || (Services.Order = {}));
|
||||
})(Services = MZG.Services || (MZG.Services = {}));
|
||||
})(MZG || (MZG = {}));
|
||||
// ----------------- MZG.Services --------------------
|
||||
var MZG;
|
||||
(function (MZG) {
|
||||
var Services;
|
||||
(function (Services) {
|
||||
var Profile;
|
||||
(function (Profile) {
|
||||
var ServiceClient = (function () {
|
||||
function ServiceClient(endPoint) {
|
||||
if (endPoint === void 0) { endPoint = "/service"; }
|
||||
this.endPoint = endPoint;
|
||||
}
|
||||
ServiceClient.prototype.validateLogin = function (email, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "ValidateLogin", [email], success, err);
|
||||
};
|
||||
ServiceClient.prototype.newCustomer = function (email, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "NewCustomer", [email], success, err);
|
||||
};
|
||||
ServiceClient.prototype.newGuestCustomer = function (email, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "NewGuestCustomer", [email], success, err);
|
||||
};
|
||||
ServiceClient.prototype.addShippingAddress = function (address, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "AddShippingAddress", [address], success, err);
|
||||
};
|
||||
ServiceClient.prototype.addBillingAddress = function (address, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "AddBillingAddress", [address], success, err);
|
||||
};
|
||||
ServiceClient.prototype.setProfileData = function (profileData, success, err) {
|
||||
GoTSRPC.call(this.endPoint, "SetProfileData", [profileData], success, err);
|
||||
};
|
||||
ServiceClient.defaultInst = new ServiceClient;
|
||||
return ServiceClient;
|
||||
}());
|
||||
Profile.ServiceClient = ServiceClient;
|
||||
})(Profile = Services.Profile || (Services.Profile = {}));
|
||||
})(Services = MZG.Services || (MZG.Services = {}));
|
||||
})(MZG || (MZG = {}));
|
||||
143
test.ts
Normal file
143
test.ts
Normal file
@ -0,0 +1,143 @@
|
||||
module GoTSRPC {
|
||||
export function call(endPoint:string, method:string, args:any[], success:any, err:any) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.withCredentials = true;
|
||||
request.open('POST', endPoint + "/" + encodeURIComponent(method), true);
|
||||
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
||||
request.send(JSON.stringify(args));
|
||||
request.onload = function() {
|
||||
if (request.status == 200) {
|
||||
try {
|
||||
var data = JSON.parse(request.responseText);
|
||||
success.apply(null, data);
|
||||
} catch(e) {
|
||||
err(request);
|
||||
}
|
||||
} else {
|
||||
err(request);
|
||||
}
|
||||
};
|
||||
request.onerror = function() {
|
||||
err(request);
|
||||
};
|
||||
}
|
||||
}
|
||||
module MZG.Services.Order {
|
||||
export class ServiceClient {
|
||||
static defaultInst = new ServiceClient;
|
||||
constructor(public endPoint:string = "/service") { }
|
||||
validateOrder(success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "ValidateOrder", [], success, err);
|
||||
}
|
||||
confirmOrder(success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "ConfirmOrder", [], success, err);
|
||||
}
|
||||
flushPaymentInfo(success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "FlushPaymentInfo", [], success, err);
|
||||
}
|
||||
setPositionSize(oldItemID:string, newItemID:string, success:(order:MZG.Services.Order.Order, err:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetPositionSize", [oldItemID, newItemID], success, err);
|
||||
}
|
||||
setPositionCount(itemID:string, count:number, success:(order:MZG.Services.Order.Order, err:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetPositionCount", [itemID, count], success, err);
|
||||
}
|
||||
getOrder(success:(order:MZG.Services.Order.Order, err:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "GetOrder", [], success, err);
|
||||
}
|
||||
setBillingAddress(addressId:string, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetBillingAddress", [addressId], success, err);
|
||||
}
|
||||
setShippingAddress(addressId:string, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetShippingAddress", [addressId], success, err);
|
||||
}
|
||||
setBilling(billing:MZG.Services.Order.Billing, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetBilling", [billing], success, err);
|
||||
}
|
||||
setShipping(shipping:MZG.Services.Order.Shipping, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetShipping", [shipping], success, err);
|
||||
}
|
||||
setPayment(payment:MZG.Services.Order.Payment, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetPayment", [payment], success, err);
|
||||
}
|
||||
setPage(page:string, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetPage", [page], success, err);
|
||||
}
|
||||
getOrderSummary(success:(summary:MZG.Services.Order.OrderSummary, err:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "GetOrderSummary", [], success, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
OrderSummary &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.OrderSummary export interface OrderSummary { numberOfItems:number; subTotal:string; promotionAmount:string; hasPromotions:boolean; vat:string; total:string; totalRappen:string; shippingCosts:string; hasShippingCosts:boolean; amountUntilFreeShipping:string; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
OrderPosition &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.OrderPosition export interface OrderPosition { id:string; name:string; brand:string; imageURL:string; size:string; style:string; quantity:number; pricePerUnit:any; price:any; availability:any; sizes:any[]; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
AddressFormValue &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.AddressFormValue export interface AddressFormValue { salutation:string; firstName:string; title:string; lastName:string; company:string; street:string; streetNumber:string; zip:string; city:string; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
Billing &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.Billing export interface Billing { newAddress?:MZG.Services.Order.AddressFormValue; tacAgree:boolean; email:string; phone:string; birthday:string; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
PaymentInfo &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.PaymentInfo export interface PaymentInfo { method:string; status:string; feedback:string; complete:boolean; amount:string; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
Session &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.Session export interface Session { billing?:MZG.Services.Order.Billing; shipping?:MZG.Services.Order.Shipping; payment?:MZG.Services.Order.Payment; page:string; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
Order &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.Order export interface Order { id:string; positions:MZG.Services.Order.OrderPosition[]; summary?:MZG.Services.Order.OrderSummary; paymentInfo:MZG.Services.Order.PaymentInfo[]; session?:MZG.Services.Order.Session; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
Shipping &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.Shipping export interface Shipping { useBilling:boolean; newAddress?:MZG.Services.Order.AddressFormValue; }] 1}
|
||||
// ----------------- MZG.Services.Order --------------------
|
||||
Payment &{ [ // git.bestbytes.net/Project-Globus-Services/services/order.Payment export interface Payment { paymentType:string; coupons:string[]; }] 1}
|
||||
// ----------------- MZG.Services --------------------
|
||||
ServiceError &{ [ // git.bestbytes.net/Project-Globus-Services/services.ServiceError export interface ServiceError { error:string; }] 1}
|
||||
module GoTSRPC {
|
||||
export function call(endPoint:string, method:string, args:any[], success:any, err:any) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.withCredentials = true;
|
||||
request.open('POST', endPoint + "/" + encodeURIComponent(method), true);
|
||||
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
|
||||
request.send(JSON.stringify(args));
|
||||
request.onload = function() {
|
||||
if (request.status == 200) {
|
||||
try {
|
||||
var data = JSON.parse(request.responseText);
|
||||
success.apply(null, data);
|
||||
} catch(e) {
|
||||
err(request);
|
||||
}
|
||||
} else {
|
||||
err(request);
|
||||
}
|
||||
};
|
||||
request.onerror = function() {
|
||||
err(request);
|
||||
};
|
||||
}
|
||||
}
|
||||
module MZG.Services.Profile {
|
||||
export class ServiceClient {
|
||||
static defaultInst = new ServiceClient;
|
||||
constructor(public endPoint:string = "/service") { }
|
||||
validateLogin(email:string, success:(valid:boolean) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "ValidateLogin", [email], success, err);
|
||||
}
|
||||
newCustomer(email:string, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "NewCustomer", [email], success, err);
|
||||
}
|
||||
newGuestCustomer(email:string, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "NewGuestCustomer", [email], success, err);
|
||||
}
|
||||
addShippingAddress(address:MZG.Services.Profile.AddressFormValue, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "AddShippingAddress", [address], success, err);
|
||||
}
|
||||
addBillingAddress(address:MZG.Services.Profile.AddressFormValue, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "AddBillingAddress", [address], success, err);
|
||||
}
|
||||
setProfileData(profileData:MZG.Services.Profile.ProfileData, success:(ret:MZG.Services.ServiceError) => void, err:(request:XMLHttpRequest) => void) {
|
||||
GoTSRPC.call(this.endPoint, "SetProfileData", [profileData], success, err);
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----------------- MZG.Services --------------------
|
||||
ServiceError &{ [ // git.bestbytes.net/Project-Globus-Services/services.ServiceError export interface ServiceError { error:string; }] 1}
|
||||
// ----------------- MZG.Services.Profile --------------------
|
||||
AddressFormValue &{ [ // git.bestbytes.net/Project-Globus-Services/services/profile.AddressFormValue export interface AddressFormValue { salutation:string; firstName:string; title:string; lastName:string; company:string; street:string; streetNumber:string; zip:string; city:string; }] 1}
|
||||
// ----------------- MZG.Services.Profile --------------------
|
||||
ProfileData &{ [ // git.bestbytes.net/Project-Globus-Services/services/profile.ProfileData export interface ProfileData { tacAgree:boolean; PhoneMobile:string; Birthday:string; }] 1}
|
||||
@ -108,19 +108,18 @@ func getTypesFromAstType(ident *ast.Ident) (structType string, scalarType Scalar
|
||||
return
|
||||
}
|
||||
|
||||
func readAstType(v *Value, fieldIdent *ast.Ident) {
|
||||
_, scalarType := getTypesFromAstType(fieldIdent)
|
||||
func readAstType(v *Value, fieldIdent *ast.Ident, fileImports fileImportSpecMap) {
|
||||
structType, scalarType := getTypesFromAstType(fieldIdent)
|
||||
v.ScalarType = scalarType
|
||||
|
||||
// if len(structType) > 0 {
|
||||
// v.StructType = &StructType{
|
||||
// Name: structType,
|
||||
// //Package: fieldIdent.String(),
|
||||
// }
|
||||
// trace("----------------->", fieldIdent)
|
||||
// } else {
|
||||
v.GoScalarType = fieldIdent.Name
|
||||
// }
|
||||
if len(structType) > 0 {
|
||||
v.StructType = &StructType{
|
||||
Name: structType,
|
||||
Package: fileImports.getPackagePath(""),
|
||||
}
|
||||
} else {
|
||||
v.GoScalarType = fieldIdent.Name
|
||||
}
|
||||
}
|
||||
|
||||
func readAstStarExpr(v *Value, starExpr *ast.StarExpr, fileImports fileImportSpecMap) {
|
||||
@ -192,7 +191,7 @@ func (v *Value) loadExpr(expr ast.Expr, fileImports fileImportSpecMap) {
|
||||
v.Array = &Array{Value: &Value{}}
|
||||
switch reflect.ValueOf(fieldArray.Elt).Type().String() {
|
||||
case "*ast.Ident":
|
||||
readAstType(v.Array.Value, fieldArray.Elt.(*ast.Ident))
|
||||
readAstType(v.Array.Value, fieldArray.Elt.(*ast.Ident), fileImports)
|
||||
case "*ast.StarExpr":
|
||||
readAstStarExpr(v.Array.Value, fieldArray.Elt.(*ast.StarExpr), fileImports)
|
||||
case "*ast.ArrayType":
|
||||
@ -207,7 +206,7 @@ func (v *Value) loadExpr(expr ast.Expr, fileImports fileImportSpecMap) {
|
||||
}
|
||||
case "*ast.Ident":
|
||||
fieldIdent := expr.(*ast.Ident)
|
||||
readAstType(v, fieldIdent)
|
||||
readAstType(v, fieldIdent, fileImports)
|
||||
case "*ast.StarExpr":
|
||||
// a pointer on sth
|
||||
readAstStarExpr(v, expr.(*ast.StarExpr), fileImports)
|
||||
|
||||
@ -129,20 +129,21 @@ func renderService(service *Service, mappings config.TypeScriptMappings, ts *cod
|
||||
ts.l("}")
|
||||
return nil
|
||||
}
|
||||
func RenderStructsToPackages(structs map[string]*Struct, mappings config.TypeScriptMappings) (mappedTypeScript map[string]string, err error) {
|
||||
mappedTypeScript = map[string]string{}
|
||||
codeMap := map[string]*code{}
|
||||
func RenderStructsToPackages(structs map[string]*Struct, mappings config.TypeScriptMappings, mappedTypeScript map[string]map[string]*code) (err error) {
|
||||
|
||||
codeMap := map[string]map[string]*code{}
|
||||
for _, mapping := range mappings {
|
||||
codeMap[mapping.GoPackage] = newCode().l("module " + mapping.TypeScriptModule + " {").ind(1)
|
||||
codeMap[mapping.GoPackage] = map[string]*code{} //newCode().l("module " + mapping.TypeScriptModule + " {").ind(1)
|
||||
}
|
||||
for name, str := range structs {
|
||||
if str == nil {
|
||||
err = errors.New("could not resolve: " + name)
|
||||
return
|
||||
}
|
||||
ts, ok := codeMap[str.Package]
|
||||
codeMap[str.Package][str.Name] = newCode().ind(1)
|
||||
ts, ok := codeMap[str.Package][str.Name]
|
||||
if !ok {
|
||||
err = errors.New("missing code mapping for: " + str.Package)
|
||||
err = errors.New("missing code mapping for go package : " + str.Package + " => you have to add a mapping from this go package to a TypeScript module in your build-config.yml in the mappings section")
|
||||
return
|
||||
}
|
||||
err = renderStruct(str, mappings, ts)
|
||||
@ -151,9 +152,16 @@ func RenderStructsToPackages(structs map[string]*Struct, mappings config.TypeScr
|
||||
}
|
||||
}
|
||||
for _, mapping := range mappings {
|
||||
mappedTypeScript[mapping.TypeScriptModule] = codeMap[mapping.GoPackage].ind(-1).l("}").string()
|
||||
for structName, structCode := range codeMap[mapping.GoPackage] {
|
||||
_, ok := mappedTypeScript[mapping.GoPackage]
|
||||
if !ok {
|
||||
mappedTypeScript[mapping.GoPackage] = map[string]*code{}
|
||||
}
|
||||
mappedTypeScript[mapping.GoPackage][structName] = structCode
|
||||
}
|
||||
//.ind(-1).l("}").string()
|
||||
}
|
||||
return
|
||||
return nil
|
||||
}
|
||||
func RenderTypeScriptServices(services []*Service, mappings config.TypeScriptMappings, tsModuleName string) (typeScript string, err error) {
|
||||
ts := newCode()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user