forked from bufbuild/protoc-gen-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harness.go
58 lines (46 loc) · 1.06 KB
/
harness.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
package main
import (
"io/ioutil"
"log"
"os"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/lyft/protoc-gen-validate/tests/harness"
_ "github.com/lyft/protoc-gen-validate/tests/harness/cases/go"
)
func main() {
b, err := ioutil.ReadAll(os.Stdin)
checkErr(err)
tc := new(harness.TestCase)
checkErr(proto.Unmarshal(b, tc))
da := new(ptypes.DynamicAny)
checkErr(ptypes.UnmarshalAny(tc.Message, da))
msg := da.Message.(interface {
Validate() error
})
checkValid(msg.Validate())
}
func checkValid(err error) {
if err == nil {
resp(&harness.TestResult{Valid: true})
} else {
resp(&harness.TestResult{Reason: err.Error()})
}
}
func checkErr(err error) {
if err == nil {
return
}
resp(&harness.TestResult{
Error: true,
Reason: err.Error(),
})
}
func resp(result *harness.TestResult) {
if b, err := proto.Marshal(result); err != nil {
log.Fatalf("could not marshal response: %v", err)
} else if _, err = os.Stdout.Write(b); err != nil {
log.Fatalf("could not write response: %v", err)
}
os.Exit(0)
}