Пример #1
0
var clientTemplate = generator.NewTemplate("localoperator-gen.go",
	`// Code generated by protoc-gen-operatorlocal
package {{.LocalPackageName}}

import (
	"golang.org/x/net/context"
	"google.golang.org/grpc"

	{{- range .Services}}
		"{{.ImportPath}}"
	{{- end}}
)

{{- range .Services}}
type {{.Name}}Client struct {
	server {{.PackageName}}.{{.Name}}Server
}
{{end}}

{{- range .Services}}
func New{{.Name}}Client(server {{.PackageName}}.{{.Name}}Server) {{.PackageName}}.{{.Name}}Client {
	return &{{.Name}}Client{server: server}
}
{{end}}

{{- range .Services}}
{{- $svc := .Name }}
{{- $pkg := .PackageName }}
{{- range .Methods }}
func (c *{{$svc}}Client) {{camelCase .Name}}(
	ctx context.Context,
	in *{{$pkg}}.{{.Input}},
	opts ...grpc.CallOption,
) (*{{$pkg}}.{{.Output}}, error) {
	return c.{{.Name}}(ctx, in)
}
{{ end }}
{{ end }}`)
Пример #2
0
var template = generator.NewTemplate("builder-gen.go",
	`// Code generated by protoc-gen-operatord
package main

import (
	"fmt"

	"github.com/sr/operator"
	"golang.org/x/net/context"
	"google.golang.org/grpc"

{{range $k, $v := .Imports}}
	{{$k}} "{{$v}}"
{{end}}
)

func invoker(ctx context.Context, conn *grpc.ClientConn, req *operator.Request, pkg string) error {
{{- range .Services}}
	{{- $pkg := .Package }}
	{{- $svc := .Name }}
	if req.Call.Service == fmt.Sprintf("%s.{{.Name}}", pkg) {
	{{- range .Methods }}
		if req.Call.Method == "{{.Name}}" {
			client := {{$pkg}}.New{{$svc}}Client(conn)
			_, err := client.{{.Name}}(
				ctx,
				&{{$pkg}}.{{.Input}}{
					Request: req,
					{{- range .Arguments}}
					{{inputField .Name}}: req.Call.Args["{{.Name}}"],
					{{- end}}
				},
			)
			return err
		}
	{{- end }}
	}
{{- end }}
	return fmt.Errorf("no such service: `+"`"+"%s %s"+"`"+`", req.Call.Service, req.Call.Method)
}
`)
Пример #3
0
var mainTemplate = generator.NewTemplate("main-gen.go",
	`// Code generated by protoc-gen-operatorcmd
package main

import (
	"flag"
	"fmt"
	"io"
	"os"

	"github.com/sr/operator"
	"golang.org/x/net/context"

{{range $k, $v := .Imports}}
	{{$k}} "{{$v}}"
{{end}}
)

const program = "operatorctl"

var cmd = operator.NewCommand(
	program,
	[]operator.ServiceCommand{
{{- range .Services}}
		{
{{- $pkg := .Package }}
{{- $svc := .Name }}
			Name:     "{{serviceName .Name}}",
			Synopsis: `+"`"+`{{ .Description }}`+"`"+`,
			Methods: []operator.MethodCommand{
	{{- range .Methods}}
				{
					Name: "{{methodName .Name}}",
					Synopsis: `+"`"+`{{.Description}}`+"`"+`,
					Flags: []*flag.Flag{
					{{- range .Arguments }}
						{
							Name:  "{{argName .Name}}",
							Usage: "{{.Description}}",
						},
					{{- end }}
					},
					Run: func(ctx *operator.CommandContext) (string, error) {
			{{- range .Arguments}}
						{{.Name}} := ctx.Flags.String("{{flagName .Name}}", "", "")
			{{- end}}
						if err := ctx.Flags.Parse(ctx.Args); err != nil {
							return "", err
						}
						conn, err := ctx.GetConn()
						if err != nil {
							return "", err
						}
						defer conn.Close()
						client := {{$pkg}}.New{{$svc}}Client(conn)
						resp, err := client.{{.Name}}(
							context.Background(),
							&{{$pkg}}.{{.Input}}{
								Request: ctx.Request,
								{{- range .Arguments}}
								{{inputField .Name}}: *{{.Name}},
								{{- end}}
							},
						)
						if err != nil {
							return "", err
						}
						return resp.Message, nil
					},
				},
	{{- end }}
			},
		},
{{end}}
	},
)

func main() {
	status, output := cmd.Run(os.Args)
	if status != 0 {
		if _, err := fmt.Fprintf(os.Stderr, "%s: %s\n", program, output); err != nil {
			panic(err)
		}
	} else {
		if _, err := io.WriteString(os.Stdout, output+"\n"); err != nil {
			panic(err)
		}
	}
	os.Exit(status)
}`)