-
Notifications
You must be signed in to change notification settings - Fork 8
/
genprotoservice.go
65 lines (50 loc) · 1.52 KB
/
genprotoservice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package scaffold
import (
"context"
"os"
"path/filepath"
"strings"
"text/template"
"namespacelabs.dev/foundation/internal/fnfs"
"namespacelabs.dev/foundation/schema"
)
const (
protoServiceFileName = "service.proto"
)
type GenProtoServiceOpts struct {
Name string
Framework schema.Framework
}
func CreateProtoScaffold(ctx context.Context, fsfs fnfs.ReadWriteFS, loc fnfs.Location, opts GenProtoServiceOpts) error {
parts := strings.Split(loc.RelPath, string(os.PathSeparator))
return createProtoScaffold(ctx, fsfs, loc.Rel(protoServiceFileName), protoServiceTmpl, protoServiceTmplOptions{
Name: opts.Name,
Package: strings.Join(parts, "."),
GoPackage: filepath.Join(append([]string{loc.ModuleName}, parts...)...),
Framework: opts.Framework.String(),
})
}
type protoServiceTmplOptions struct {
Name string
Package string
GoPackage string
Framework string
}
// TODO clean up template once we run a proto formatter.
var protoServiceTmpl = template.Must(template.New(protoServiceFileName).Parse(`syntax = "proto3";
package {{.Package}};{{if eq .Framework "GO"}}
option go_package = "{{.GoPackage}}";{{end}}
service {{.Name}} {
// TODO change to desired RPC methods
rpc Echo(EchoRequest) returns (EchoResponse);
}
message EchoRequest {
string text = 1;
}
message EchoResponse {
string text = 1;
}
`))