Skip to content

Instantly share code, notes, and snippets.

@sputn1ck
Last active May 28, 2020 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sputn1ck/e57420046622a2b15dd249ba4b0e7155 to your computer and use it in GitHub Desktop.
Save sputn1ck/e57420046622a2b15dd249ba4b0e7155 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"github.com/jhump/protoreflect/desc/protoparse"
"gopkg.in/yaml.v2"
"io/ioutil"
)
func main() {
var protoPath, ymlPath string
flag.StringVar(&protoPath, "proto", "", "path to protofile")
flag.StringVar(&ymlPath, "service", "", "path to service definitions file")
flag.Parse()
if protoPath == "" || ymlPath == "" {
fmt.Printf("usage: proto-service-validator -proto='path/to/proto.proto' -service='path/to/service.yml'")
return
}
parser := protoparse.Parser{}
desc, err := parser.ParseFiles(protoPath)
if err != nil {
fmt.Printf("error parsing proto %v", err)
return
}
serviceDefinition := &ServiceDefinition{}
ymlBytes, err := ioutil.ReadFile(ymlPath)
if err != nil {
fmt.Printf("error loading yml file %v", err)
return
}
err = yaml.Unmarshal(ymlBytes, serviceDefinition)
if err != nil {
fmt.Printf("error parsing service file %v", err)
return
}
fqns := make(map[string]bool)
for _, srv := range desc[0].GetServices() {
for _, method := range srv.GetMethods() {
fqns[method.GetFullyQualifiedName()] = false
}
}
for _, rule := range serviceDefinition.HTTP.Rules {
if _, ok := fqns[rule.Selector]; ok {
fqns[rule.Selector] = true
}
}
for k, v := range fqns {
if !v {
fmt.Printf("\n WARNING %s is not defined in the service definitions", k)
}
}
}
type ServiceDefinition struct {
HTTP struct {
Rules []struct {
Selector string `yaml:"selector"`
} `yaml:"rules"`
} `yaml:"http"`
}
// example foo.proto
//syntax = "proto3";
//
//package foobar;
//
//service Foo{
//rpc Bar(BarRequest) returns (BarResponse);
//rpc Baz(BazRequest) returns (BazResponse);
//}
//
//message BarRequest {
//}
//
//message BarResponse {
//}
//
//message BazRequest {
//}
//
//message BazResponse {
//
//}
// example foo_service.yml
//http:
// rules:
// - selector: foobar.Foo.Bar
// get: "/v1/foo/bar"
// output
// WARNING foobar.Foo.Baz is not defined in the service definitions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment