/
main.go
77 lines (63 loc) · 1.93 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
pgs "github.com/lyft/protoc-gen-star/v2"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
"github.com/cerbos/protoc-gen-jsonschema/internal/common"
"github.com/cerbos/protoc-gen-jsonschema/internal/module"
)
const (
debugPrintRequest = "PGJS_DEBUG_REQUEST"
requestPath = "internal/test/testdata/code_generator_request.pb.bin"
)
func main() {
if _, exists := os.LookupEnv(debugPrintRequest); exists {
if err := printRequest(requestPath); err != nil {
log.Fatalf("failed to print request: %s", err.Error())
}
}
reqFile, err := os.Open(requestPath)
if err != nil {
log.Fatalf("failed to open code generator request file: %s", err.Error())
}
resBytes := &bytes.Buffer{}
pgs.Init(
pgs.DebugEnv(common.DebugEnv),
pgs.ProtocInput(reqFile),
pgs.ProtocOutput(resBytes),
).RegisterModule(module.New()).Render()
res := &pluginpb.CodeGeneratorResponse{}
if err := proto.Unmarshal(resBytes.Bytes(), res); err != nil {
log.Fatalf("failed to unmarshal code generator response: %s", err.Error())
}
for _, f := range res.File {
log.Printf("%s\n", *f.Name)
log.Printf("\n%s", *f.Content)
log.Printf("--------------------------------------\n")
}
}
func printRequest(path string) error {
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open code generator request file: %w", err)
}
reqBytes, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("failed to read code generator request bytes: %w", err)
}
req := &pluginpb.CodeGeneratorRequest{}
if err := proto.Unmarshal(reqBytes, req); err != nil {
return fmt.Errorf("failed to unmarshal code generator response: %w", err)
}
log.Printf("%s", protojson.Format(req))
log.Printf("--------------------------------------\n")
return nil
}