templates

package
v0.1.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 27, 2023 License: Unlicense Imports: 0 Imported by: 0

Documentation

Index

Constants

View Source
const TEMPLATE = `// Code generated by jrpc. DO NOT EDIT.

package {{ .Package }}

import (
	"context"
	"errors"
	"fmt"

	"gfx.cafe/open/jrpc/contrib/jmux"
)


var _ = context.Background
var _ = errors.New
var _ = fmt.Sprintf




{{- range $name, $component := .Components.Schemas }}
    type {{camelCase $name}} {{template "schemaType" $component}}
{{- end }}

{{define "schemaType" -}}
    {{if not (eq .Ref "") -}}
        {{camelCase (refName .Ref)}}
    {{- else if or
        (not (eq (len .Properties) 0))
        (not (eq (len .OneOf) 0))
        (not (eq (len .AnyOf) 0))
        (not (eq (len .AllOf) 0))
    -}}
        struct {
            {{range $name, $property := .Properties -}}
                {{camelCase $name}} {{template "schemaType" $property}} ` + "`" + `json:"{{$name}}"` + "`" + `
            {{end -}}
            {{range $idx, $property := .OneOf -}}
                Option{{$idx}} {{template "schemaType" $property}}
            {{end -}}
            {{range $idx, $property := .AnyOf -}}
                Option{{$idx}} {{template "schemaType" $property}}
            {{end -}}
            {{range $idx, $property := .AllOf -}}
                Field{{$idx}} {{template "schemaType" $property}}
            {{end -}}
        }
    {{- else if eq .Type "array" -}}
        []{{template "schemaType" (index .Items 0)}}
    {{- else if not (eq (len .Enum) 0) -}}
        string
    {{- else -}}
        {{goType .Type}}
    {{- end}}
{{- end -}}

type GoOpenRPCHandler struct {
	Srv GoOpenRPCService
}
{{range .Methods -}}
    // {{.Summary}}
    func(h *RpcHandler) {{camelCase .Name}}(ctx context.Context,
        {{range .Params -}}{{camelCase .Name}} {{if not .Required}}*{{end}}{{template "schemaType" .Schema}},
        {{end -}}) ({{camelCase .Result.Name}} {{template "schemaType" .Result.Schema}}, err error) {
        handler := h.Fn{{camelCase .Name}}
        if handler == nil {
            err = errors.New("method {{.Name}} not implemented")
            return
        }
        return handler(ctx,
        {{range .Params -}}{{camelCase .Name}},
        {{end -}})
    }
{{end -}}


type GoOpenRPCService interface {
    {{range .Methods -}}
        // {{.Summary}}
        {{camelCase .Name}}(ctx context.Context,
            {{range .Params -}}{{camelCase .Name}} {{if not .Required}}*{{end}}{{template "schemaType" .Schema}},
            {{end -}}
            ) ({{camelCase .Result.Name}} {{template "schemaType" .Result.Schema}}, err error)
    {{end -}}
}



func (h *GoOpenRPCHandler) RouteRPC(r jmux.Router) {
    {{range .Methods -}}
        // {{.Summary}}
        {{if .Namespace}}
            if err := r.RegisterFunc("{{.Namespace}}_{{.MethodName}}", h.Srv.{{camelCase .Name}}); err != nil {
                panic(err)
            }
        {{else}}
            if err := r.RegisterFunc("{{.MethodName}}", h.Srv.{{camelCase .Name}}); err != nil {
                panic(err)
            }
        {{end}}
    {{end -}}
}

type RpcHandler struct {
{{range .Methods -}}
    // {{.Summary}}
  Fn{{camelCase .Name}} func(ctx context.Context,
        {{range .Params -}}{{camelCase .Name}} {{if not .Required}}*{{end}}{{template "schemaType" .Schema}},
        {{end -}})({{camelCase .Result.Name}} {{template "schemaType" .Result.Schema}}, err error)

{{end -}}
}
`

Variables

This section is empty.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL