-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (53 loc) · 1.09 KB
/
main.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
package main
import (
"bufio"
"fmt"
"io"
"os"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/swuecho/sqlc-fs/internal/plugin"
)
func ProtoToJSON(msg proto.Message) (string, error) {
marshaler := &jsonpb.Marshaler{OrigName: true}
jsonstr, err := marshaler.MarshalToString(msg)
if err != nil {
return "", err
}
return jsonstr, nil
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "error generating F#: %s", err)
os.Exit(2)
}
}
func run() error {
var req plugin.CodeGenRequest
reqBlob, err := io.ReadAll(os.Stdin)
if err != nil {
return err
}
if err := req.UnmarshalVT(reqBlob); err != nil {
return err
}
// convert protobuf message to JSON
jsonStr, _ := ProtoToJSON(&req)
_ = os.WriteFile("output.json", []byte(jsonStr), 0644)
resp, err := Generate(&req)
if err != nil {
return err
}
respBlob, err := resp.MarshalVT()
if err != nil {
return err
}
w := bufio.NewWriter(os.Stdout)
if _, err := w.Write(respBlob); err != nil {
return err
}
if err := w.Flush(); err != nil {
return err
}
return nil
}