Skip to content

Commit

Permalink
start adding support for services
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmossas committed Sep 10, 2024
1 parent 22ab669 commit 17188aa
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 24 deletions.
53 changes: 44 additions & 9 deletions languages/go/go-server/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ func rpc[TParams, TResponse, TContext any](app *App[TContext], options *RpcOptio
rpcSchema.Http.Path = app.Options.RpcRoutePrefix + options.Path
}
if len(options.Description) > 0 {
rpcSchema.Http.Description = &options.Description
rpcSchema.Http.Description = Some(options.Description)
}
if options.IsDeprecated {
rpcSchema.Http.IsDeprecated = &options.IsDeprecated
rpcSchema.Http.IsDeprecated = Some(options.IsDeprecated)
}
}
params := reflect.TypeOf(handler).In(0)
Expand Down Expand Up @@ -362,7 +362,7 @@ func RpcWithOptions[TParams, TResponse, TContext any](app *App[TContext], option
rpc(app, &options, handler)
}

func RegisterDef[TContent any](app *App[TContent], input any) {
func RegisterDef[TContext any](app *App[TContext], input any) {
def, err := ToTypeDef(input, app.Options.KeyCasing)
if err != nil {
panic(err)
Expand All @@ -373,31 +373,66 @@ func RegisterDef[TContent any](app *App[TContent], input any) {
})
}

func RegisterService[TContext any](app *App[TContext], input Service) {

}

type Service struct {
Name string
Procedures map[string]RpcDef
Definitions map[string]TypeDef
Procedures []__orderedMapEntry__[RpcDef]
Definitions []__orderedMapEntry__[TypeDef]
rawProcedures []any
}

func NewService(name string) Service {
return Service{
Name: name,
Procedures: map[string]RpcDef{},
Definitions: map[string]TypeDef{},
Procedures: []__orderedMapEntry__[RpcDef]{},
Definitions: []__orderedMapEntry__[TypeDef]{},
rawProcedures: []any{},
}
}

func ServiceRpc[TParams, TResponse, TContext any](service *Service, handler func(TParams, TContext) (TResponse, RpcError)) {
if service.Procedures == nil {
service.Procedures = make(map[string]RpcDef)
service.Procedures = []__orderedMapEntry__[RpcDef]{}
}
def, defErr := ToRpcDef(handler, ArriHttpRpcOptions{})
if defErr != nil {
panic(defErr)
}
service.rawProcedures = append(service.rawProcedures, handler)
rpcName := rpcNameFromFunctionName(GetFunctionName(handler))
service.Procedures[rpcName] = *def
__updateAOrderedMap__(service.Procedures, __orderedMapEntry__[RpcDef]{
Key: rpcName,
Value: *def,
})
}

func ServiceRpcWithOptions[TParams, TResponse, TContext any](service *Service, options RpcOptions, handler func(TParams, TContext) (TResponse, RpcError)) {
if service.Procedures == nil {
service.Procedures = []__orderedMapEntry__[RpcDef]{}
}
def, defErr := ToRpcDef(handler, ArriHttpRpcOptions{})
if defErr != nil {
panic(defErr)
}
if len(options.Method) > 0 {
def.Http.Method = strings.ToLower(options.Method)
}
if len(options.Path) > 0 {
def.Http.Path = options.Path
}
if len(options.Description) > 0 {
def.Http.Description = Some(options.Description)
}
if options.IsDeprecated {
def.Http.IsDeprecated = Some(true)
}
service.rawProcedures = append(service.rawProcedures, handler)
rpcName := rpcNameFromFunctionName(GetFunctionName(handler))
service.Procedures = __updateAOrderedMap__(service.Procedures, __orderedMapEntry__[RpcDef]{
Key: rpcName,
Value: *def,
})
}
30 changes: 15 additions & 15 deletions languages/go/go-server/app_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const (
type HttpMethod = string

type HttpRpcDef struct {
Path string `key:"path"`
Method HttpMethod `key:"method"`
IsEventStream *bool `key:"isEventStream"`
Params *string `key:"params"`
Response *string `key:"response"`
Description *string `key:"description"`
IsDeprecated *bool `key:"isDeprecated"`
Path string `key:"path"`
Method HttpMethod `key:"method"`
IsEventStream Option[bool] `key:"isEventStream"`
Params Option[string] `key:"params"`
Response Option[string] `key:"response"`
Description Option[string] `key:"description"`
IsDeprecated Option[bool] `key:"isDeprecated"`
}

type ArriHttpRpcOptions struct {
Expand All @@ -61,8 +61,8 @@ func ToRpcDef(value interface{}, options ArriHttpRpcOptions) (*RpcDef, error) {
if valueKind != reflect.Func {
return nil, errors.ErrUnsupported
}
params := valueType.In(0).Name()
response := valueType.Out(0).Elem().Name()
params := Some(valueType.In(0).Name())
response := Some(valueType.Out(0).Elem().Name())
path := "/" + strcase.ToKebab(fnName)
if len(options.Path) > 0 {
path = options.Path
Expand All @@ -71,20 +71,20 @@ func ToRpcDef(value interface{}, options ArriHttpRpcOptions) (*RpcDef, error) {
if len(options.Method) > 0 {
method = options.Method
}
var description *string = nil
var description = None[string]()
if len(options.Description) > 0 {
description = &options.Description
description = Some(options.Description)
}
var isDeprecated *bool = nil
var isDeprecated = None[bool]()
if options.IsDeprecated {
isDeprecated = &options.IsDeprecated
isDeprecated = Some(options.IsDeprecated)
}
return &RpcDef{
Http: &HttpRpcDef{
Path: path,
Method: method,
Params: &params,
Response: &response,
Params: params,
Response: response,
Description: description,
IsDeprecated: isDeprecated,
},
Expand Down

0 comments on commit 17188aa

Please sign in to comment.