feat: update examples

This commit is contained in:
Kevin Franklin Kim 2022-03-03 07:46:49 +01:00
parent 331ffc053f
commit 9c5748892a
23 changed files with 254 additions and 261 deletions

View File

@ -31,7 +31,7 @@ build.debug:
## === Tools ===
EXAMPLES=basic errors nullable multi
EXAMPLES=basic errors nullable union
define examples
.PHONY: example.$(1)
example.$(1):

View File

@ -28,5 +28,5 @@ func main() {
_ = exec.Command("open", "http://127.0.0.1:3000").Run()
}()
_ = http.ListenAndServe("localhost:3000", mux)
panic(http.ListenAndServe("localhost:3000", mux))
}

View File

@ -1,167 +0,0 @@
package main
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/foomo/gotsrpc/v2"
"github.com/foomo/gotsrpc/v2/example/errors/handler/backend"
backendsvs "github.com/foomo/gotsrpc/v2/example/errors/service/backend"
)
func main() {
ctx := context.Background()
c := backendsvs.NewDefaultServiceGoTSRPCClient("http://localhost:3000")
{
fmt.Println("-------------------------")
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.Error(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Printf("%s\n", gotsrpcErr)
fmt.Printf("%q\n", gotsrpcErr)
fmt.Printf("%+v\n", gotsrpcErr)
}
}
{
fmt.Println("-------------------------")
scalar, err := c.Scalar(ctx)
if err != nil {
panic("client error should be nil")
} else if scalar == nil {
panic("service error should not be nil")
} else if scalar != nil {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
scalar, err := c.MultiScalar(ctx)
if err != nil {
panic("client error should be nil")
} else if scalar == nil {
panic("service error should not be nil")
} else if scalar != nil {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.WrappedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr.Error())
fmt.Printf("%+v\n", gotsrpcErr)
}
if errors.As(errors.Unwrap(serviceErr), &gotsrpcErr) {
fmt.Println(gotsrpcErr.Error())
}
}
{
fmt.Println("-------------------------")
var scalarErr *backend.ScalarError
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.ScalarError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Printf("%s\n", serviceErr)
fmt.Printf("%q\n", serviceErr)
fmt.Printf("%+v\n", serviceErr)
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr)
}
if errors.As(serviceErr, &scalarErr) {
fmt.Println(scalarErr)
}
}
{
fmt.Println("-------------------------")
var customErr *backend.CustomError
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.CustomError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Printf("%s\n", serviceErr)
fmt.Printf("%q\n", serviceErr)
fmt.Printf("%+v\n", serviceErr)
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr)
}
if errors.As(serviceErr, &customErr) {
fmt.Println(customErr)
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrTyped) {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedWrappedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrTyped) {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedCustomError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrCustom) {
fmt.Println("OK")
}
}
}

View File

@ -23,7 +23,7 @@ func (h *Handler) Simple(w http.ResponseWriter, r *http.Request) (e *frontend.Er
func (h *Handler) Multiple(w http.ResponseWriter, r *http.Request) (e *frontend.ErrMulti) {
return &frontend.ErrMulti{
ErrMultiA: frontend.ErrMultiAOne,
ErrMultiB: "",
A: frontend.ErrMultiAOne,
B: "",
}
}

View File

@ -1,11 +1,16 @@
package main
import (
"context"
"fmt"
"net/http"
"os/exec"
"strings"
"time"
"github.com/pkg/errors"
"github.com/foomo/gotsrpc/v2"
"github.com/foomo/gotsrpc/v2/example/errors/handler/backend"
"github.com/foomo/gotsrpc/v2/example/errors/handler/frontend"
backendsvs "github.com/foomo/gotsrpc/v2/example/errors/service/backend"
@ -32,11 +37,163 @@ func main() {
go func() {
time.Sleep(time.Second)
_ = exec.Command("open", "http://127.0.0.1:3000").Run()
call()
}()
go func() {
if err := http.ListenAndServe("localhost:3000", mux); err != nil {
panic(err)
}
}()
panic(http.ListenAndServe("localhost:3000", mux))
}
func call() {
ctx := context.Background()
c := backendsvs.NewDefaultServiceGoTSRPCClient("http://localhost:3000")
{
fmt.Println("-------------------------")
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.Error(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Printf("%s\n", gotsrpcErr)
fmt.Printf("%q\n", gotsrpcErr)
fmt.Printf("%+v\n", gotsrpcErr)
}
}
{
fmt.Println("-------------------------")
scalar, err := c.Scalar(ctx)
if err != nil {
panic("client error should be nil")
} else if scalar == nil {
panic("service error should not be nil")
} else if scalar != nil {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
scalar, err := c.MultiScalar(ctx)
if err != nil {
panic("client error should be nil")
} else if scalar == nil {
panic("service error should not be nil")
} else if scalar != nil {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.WrappedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr.Error())
fmt.Printf("%+v\n", gotsrpcErr)
}
if errors.As(errors.Unwrap(serviceErr), &gotsrpcErr) {
fmt.Println(gotsrpcErr.Error())
}
}
{
fmt.Println("-------------------------")
var scalarErr *backend.ScalarError
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.ScalarError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Printf("%s\n", serviceErr)
fmt.Printf("%q\n", serviceErr)
fmt.Printf("%+v\n", serviceErr)
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr)
}
if errors.As(serviceErr, &scalarErr) {
fmt.Println(scalarErr)
}
}
{
fmt.Println("-------------------------")
var customErr *backend.CustomError
var gotsrpcErr *gotsrpc.Error
serviceErr, err := c.CustomError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Printf("%s\n", serviceErr)
fmt.Printf("%q\n", serviceErr)
fmt.Printf("%+v\n", serviceErr)
}
if errors.As(serviceErr, &gotsrpcErr) {
fmt.Println(gotsrpcErr)
}
if errors.As(serviceErr, &customErr) {
fmt.Println(customErr)
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrTyped) {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedWrappedError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrTyped) {
fmt.Println("OK")
}
}
{
fmt.Println("-------------------------")
serviceErr, err := c.TypedCustomError(ctx)
if err != nil {
panic("client error should be nil")
} else if serviceErr == nil {
panic("service error should not be nil")
} else if serviceErr != nil {
fmt.Println("OK")
}
if errors.Is(serviceErr, backend.ErrCustom) {
fmt.Println("OK")
}
}
}

View File

@ -4,8 +4,8 @@ type ErrSimple string
type (
ErrMulti struct {
A ErrMultiA `json:"a,union"`
B ErrMultiB `json:"b,union"`
A ErrMultiA `json:"a,omitempty,union"`
B ErrMultiB `json:"b,omitempty,union"`
}
ErrMultiA string
ErrMultiB string

View File

@ -1,59 +0,0 @@
/* eslint:disable */
// hello commonjs - we need some imports - sorted in alphabetical order, by go package
import * as github_com_foomo_gotsrpc_v2_example_multi_service from './service-vo'; // ./client/src/service-vo.ts to ./client/src/service-vo.ts
// github.com/foomo/gotsrpc/v2/example/multi/service.InlineStruct
export interface InlineStruct extends github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructA , github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructB {
value:string;
}
// github.com/foomo/gotsrpc/v2/example/multi/service.InlineStructA
export interface InlineStructA {
valueA:string;
}
// github.com/foomo/gotsrpc/v2/example/multi/service.InlineStructB
export interface InlineStructB {
valueB:string;
}
// github.com/foomo/gotsrpc/v2/example/multi/service.InlineStructPtr
export interface InlineStructPtr extends Partial<github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructA> , Partial<github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructB> {
bug?:github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructB;
value:string;
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionString
export type UnionString = (typeof github_com_foomo_gotsrpc_v2_example_multi_service.UnionStringA) & (typeof github_com_foomo_gotsrpc_v2_example_multi_service.UnionStringB)
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStringA
export enum UnionStringA {
One = "one",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStringB
export enum UnionStringB {
Four = "four",
Three = "three",
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStruct
export type UnionStruct = github_com_foomo_gotsrpc_v2_example_multi_service.UnionStructA | github_com_foomo_gotsrpc_v2_example_multi_service.UnionStructB | undefined
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStructA
export interface UnionStructA {
kind:'UnionStructA';
value:github_com_foomo_gotsrpc_v2_example_multi_service.UnionStructAValueA;
bar:string;
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStructAValueA
export enum UnionStructAValueA {
One = "one",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStructAValueB
export enum UnionStructAValueB {
One = "one",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/multi/service.UnionStructB
export interface UnionStructB {
kind:'UnionStructB';
value:github_com_foomo_gotsrpc_v2_example_multi_service.UnionStructAValueB;
foo:string;
}
// end of common js

View File

@ -28,5 +28,5 @@ func main() {
_ = exec.Command("open", "http://127.0.0.1:3000").Run()
}()
_ = http.ListenAndServe("localhost:3000", mux)
panic(http.ListenAndServe("localhost:3000", mux))
}

View File

@ -1,22 +1,22 @@
/* eslint:disable */
// hello commonjs - we need some imports - sorted in alphabetical order, by go package
import * as github_com_foomo_gotsrpc_v2_example_multi_service from './service-vo'; // ./client/src/service-client.ts to ./client/src/service-vo.ts
import * as github_com_foomo_gotsrpc_v2_example_union_service from './service-vo'; // ./client/src/service-client.ts to ./client/src/service-vo.ts
export class ServiceClient {
public static defaultEndpoint = "/service";
constructor(
public transport:<T>(method: string, data?: any[]) => Promise<T>
) {}
async inlineStruct():Promise<github_com_foomo_gotsrpc_v2_example_multi_service.InlineStruct> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_multi_service.InlineStruct}>("InlineStruct", []))[0]
async inlineStruct():Promise<github_com_foomo_gotsrpc_v2_example_union_service.InlineStruct> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_union_service.InlineStruct}>("InlineStruct", []))[0]
}
async inlineStructPtr():Promise<github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructPtr> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_multi_service.InlineStructPtr}>("InlineStructPtr", []))[0]
async inlineStructPtr():Promise<github_com_foomo_gotsrpc_v2_example_union_service.InlineStructPtr> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_union_service.InlineStructPtr}>("InlineStructPtr", []))[0]
}
async unionString():Promise<github_com_foomo_gotsrpc_v2_example_multi_service.UnionString> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_multi_service.UnionString}>("UnionString", []))[0]
async unionString():Promise<github_com_foomo_gotsrpc_v2_example_union_service.UnionString> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_union_service.UnionString}>("UnionString", []))[0]
}
async unionStruct():Promise<github_com_foomo_gotsrpc_v2_example_multi_service.UnionStruct> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_multi_service.UnionStruct}>("UnionStruct", []))[0]
async unionStruct():Promise<github_com_foomo_gotsrpc_v2_example_union_service.UnionStruct> {
return (await this.transport<{0:github_com_foomo_gotsrpc_v2_example_union_service.UnionStruct}>("UnionStruct", []))[0]
}
}

View File

@ -0,0 +1,59 @@
/* eslint:disable */
// hello commonjs - we need some imports - sorted in alphabetical order, by go package
import * as github_com_foomo_gotsrpc_v2_example_union_service from './service-vo'; // ./client/src/service-vo.ts to ./client/src/service-vo.ts
// github.com/foomo/gotsrpc/v2/example/union/service.InlineStruct
export interface InlineStruct extends github_com_foomo_gotsrpc_v2_example_union_service.InlineStructA , github_com_foomo_gotsrpc_v2_example_union_service.InlineStructB {
value:string;
}
// github.com/foomo/gotsrpc/v2/example/union/service.InlineStructA
export interface InlineStructA {
valueA:string;
}
// github.com/foomo/gotsrpc/v2/example/union/service.InlineStructB
export interface InlineStructB {
valueB:string;
}
// github.com/foomo/gotsrpc/v2/example/union/service.InlineStructPtr
export interface InlineStructPtr extends Partial<github_com_foomo_gotsrpc_v2_example_union_service.InlineStructA> , Partial<github_com_foomo_gotsrpc_v2_example_union_service.InlineStructB> {
bug?:github_com_foomo_gotsrpc_v2_example_union_service.InlineStructB;
value:string;
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionString
export type UnionString = (typeof github_com_foomo_gotsrpc_v2_example_union_service.UnionStringA) & (typeof github_com_foomo_gotsrpc_v2_example_union_service.UnionStringB)
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStringA
export enum UnionStringA {
One = "one",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStringB
export enum UnionStringB {
Four = "four",
Three = "three",
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStruct
export type UnionStruct = github_com_foomo_gotsrpc_v2_example_union_service.UnionStructA | github_com_foomo_gotsrpc_v2_example_union_service.UnionStructB | undefined
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStructA
export interface UnionStructA {
kind:'UnionStructA';
value:github_com_foomo_gotsrpc_v2_example_union_service.UnionStructAValueA;
bar:string;
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStructAValueA
export enum UnionStructAValueA {
One = "one",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStructAValueB
export enum UnionStructAValueB {
One = "one",
Three = "three",
Two = "two",
}
// github.com/foomo/gotsrpc/v2/example/union/service.UnionStructB
export interface UnionStructB {
kind:'UnionStructB';
value:github_com_foomo_gotsrpc_v2_example_union_service.UnionStructAValueB;
foo:string;
}
// end of common js

View File

@ -1,4 +1,4 @@
module github.com/foomo/gotsrpc/v2/example/multi
module github.com/foomo/gotsrpc/v2/example/union
go 1.16

View File

@ -1,16 +1,16 @@
module:
name: github.com/foomo/gotsrpc/v2/example/multi
name: github.com/foomo/gotsrpc/v2/example/union
path: ./
targets:
multi:
union:
services:
/service: Service
package: github.com/foomo/gotsrpc/v2/example/multi/service
package: github.com/foomo/gotsrpc/v2/example/union/service
out: ./client/src/service-client.ts
tsrpc:
- Service
mappings:
github.com/foomo/gotsrpc/v2/example/multi/service:
github.com/foomo/gotsrpc/v2/example/union/service:
out: ./client/src/service-vo.ts

View File

@ -9,7 +9,7 @@ import (
"github.com/davecgh/go-spew/spew"
"github.com/foomo/gotsrpc/v2/example/multi/service"
"github.com/foomo/gotsrpc/v2/example/union/service"
)
func main() {
@ -26,15 +26,16 @@ func main() {
}
})
go exec.Command("open", "http://127.0.0.1:3000").Run()
go call()
go func() {
time.Sleep(time.Second)
_ = exec.Command("open", "http://127.0.0.1:3000").Run()
call()
}()
http.ListenAndServe("localhost:3000", mux)
panic(http.ListenAndServe("localhost:3000", mux))
}
func call() {
time.Sleep(time.Second)
c := service.NewDefaultServiceGoTSRPCClient("http://127.0.0.1:3000")
{

View File

@ -12,8 +12,8 @@ import (
)
func init() {
gotsrpc.MustRegisterUnionExt(UnionStruct{})
gotsrpc.MustRegisterUnionExt(UnionString{})
gotsrpc.MustRegisterUnionExt(UnionStruct{})
}
const (
@ -54,7 +54,7 @@ func (p *ServiceGoTSRPCProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)
callStats := gotsrpc.GetStatsForRequest(r)
if callStats != nil {
callStats.Func = funcName
callStats.Package = "github.com/foomo/gotsrpc/v2/example/multi/service"
callStats.Package = "github.com/foomo/gotsrpc/v2/example/union/service"
callStats.Service = "Service"
}
switch funcName {

View File

@ -20,6 +20,6 @@ func (h *Handler) UnionString(w http.ResponseWriter, r *http.Request) (e UnionSt
}
func (h *Handler) UnionStruct(w http.ResponseWriter, r *http.Request) (e UnionStruct) {
//return UnionStruct{UnionStructA: UnionStructA{Kind: "UnionStructA", Value: UnionStructAValueAOne}}
// return UnionStruct{UnionStructA: UnionStructA{Kind: "UnionStructA", Value: UnionStructAValueAOne}}
return UnionStruct{B: &UnionStructB{Kind: "UnionStructB", Value: UnionStructAValueBOne}}
}

View File

@ -31,7 +31,9 @@ type (
type (
UnionString struct {
A *UnionStringA `json:"a,omitempty,union"`
B *UnionStringB `json:"b,omitempty,union"`
c string
}
UnionStringA string
UnionStringB string