-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (55 loc) · 1.66 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
// Copyright 2016 Michal Witkowski. All Rights Reserved.
// See LICENSE for licensing terms.
package main
import (
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
validator_plugin "github.com/mwitkow/go-proto-validators/plugin"
)
func main() {
gen := generator.New()
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
gen.Error(err, "reading input")
}
if err := proto.Unmarshal(data, gen.Request); err != nil {
gen.Error(err, "parsing input proto")
}
if len(gen.Request.FileToGenerate) == 0 {
gen.Fail("no files to generate")
}
useGogoImport := false
// Match parsing algorithm from Generator.CommandLineParameters
for _, parameter := range strings.Split(gen.Request.GetParameter(), ",") {
kvp := strings.SplitN(parameter, "=", 2)
// We only care about key-value pairs where the key is "gogoimport"
if len(kvp) != 2 || kvp[0] != "gogoimport" {
continue
}
useGogoImport, err = strconv.ParseBool(kvp[1])
if err != nil {
gen.Error(err, "parsing gogoimport option")
}
}
gen.CommandLineParameters(gen.Request.GetParameter())
gen.WrapTypes()
gen.SetPackageNames()
gen.BuildTypeNameMap()
gen.GeneratePlugin(validator_plugin.NewPlugin(useGogoImport))
for i := 0; i < len(gen.Response.File); i++ {
gen.Response.File[i].Name = proto.String(strings.Replace(*gen.Response.File[i].Name, ".pb.go", ".validator.pb.go", -1))
}
// Send back the results.
data, err = proto.Marshal(gen.Response)
if err != nil {
gen.Error(err, "failed to marshal output proto")
}
_, err = os.Stdout.Write(data)
if err != nil {
gen.Error(err, "failed to write output proto")
}
}