// tryParse attempts to parse the input, and verifies that it matches // the FileDescriptorProto represented in text format. func tryParse(t *testing.T, input, output string) { want := new(pb.FileDescriptorProto) if err := proto.UnmarshalText(output, want); err != nil { t.Fatalf("Test failure parsing a wanted proto: %v", err) } p := newParser("-", input) f := new(ast.File) if pe := p.readFile(f); pe != nil { t.Errorf("Failed parsing input: %v", pe) return } fset := &ast.FileSet{Files: []*ast.File{f}} if err := resolveSymbols(fset); err != nil { t.Errorf("Resolving symbols: %v", err) return } fds, err := gendesc.Generate(fset) if err != nil { t.Errorf("Generating FileDescriptorSet: %v", err) return } if n := len(fds.File); n != 1 { t.Errorf("Generated %d FileDescriptorProtos, want 1", n) return } got := fds.File[0] if !proto.Equal(got, want) { t.Errorf("Mismatch!\nGot:\n%v\nWant:\n%v", got, want) } }
func main() { flag.Usage = usage flag.Parse() if *helpShort || *helpLong || flag.NArg() == 0 { flag.Usage() os.Exit(1) } fs, err := parser.ParseFiles(flag.Args(), strings.Split(*importPath, ",")) if err != nil { fatalf("%v", err) } fds, err := gendesc.Generate(fs) if err != nil { fatalf("Failed generating descriptors: %v", err) } if *descriptorOnly { proto.MarshalText(os.Stdout, fds) os.Exit(0) } //fmt.Println("-----") //proto.MarshalText(os.Stdout, fds) //fmt.Println("-----") // Prepare request. cgRequest := &plugin.CodeGeneratorRequest{ FileToGenerate: flag.Args(), ProtoFile: fds.File, } if *params != "" { cgRequest.Parameter = params } buf, err := proto.Marshal(cgRequest) if err != nil { fatalf("Failed marshaling CG request: %v", err) } // Find plugin. pluginPath := fullPath(*pluginBinary, strings.Split(os.Getenv("PATH"), ":")) if pluginPath == "" { fatalf("Failed finding plugin binary %q", *pluginBinary) } // Run the plugin subprocess. cmd := &exec.Cmd{ Path: pluginPath, Stdin: bytes.NewBuffer(buf), Stderr: os.Stderr, } buf, err = cmd.Output() if err != nil { fatalf("Failed running plugin: %v", err) } // Parse the response. cgResponse := new(plugin.CodeGeneratorResponse) if err = proto.Unmarshal(buf, cgResponse); err != nil { fatalf("Failed unmarshaling CG response: %v", err) } // TODO: check cgResponse.Error for _, f := range cgResponse.File { // TODO: If f.Name is nil, the content should be appended to the previous file. if f.Name == nil || f.Content == nil { fatalf("Malformed CG response") } if err := ioutil.WriteFile(*f.Name, []byte(*f.Content), 0644); err != nil { fatalf("Failed writing output file: %v", err) } } }