protogen, cmd/protoc-gen-go: initial commit

Package protogen provides support for writing protoc plugins.
A "plugin" in this case is a program run by protoc to generate output.

The protoc-gen-go command is a protoc plugin to generate Go code.

cmd/protoc-gen-go/golden_test.go is mostly a straight copy from
the golden test in github.com/golang/protobuf.

Change-Id: I332d0df1e4b60bb8cd926320b8721e16b99a4b71
Reviewed-on: https://go-review.googlesource.com/130175
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/protogen/protogen.go b/protogen/protogen.go
new file mode 100644
index 0000000..b10edad
--- /dev/null
+++ b/protogen/protogen.go
@@ -0,0 +1,224 @@
+// Copyright 2018 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package protogen provides support for writing protoc plugins.
+//
+// Plugins for protoc, the Protocol Buffers Compiler, are programs which read
+// a CodeGeneratorRequest protocol buffer from standard input and write a
+// CodeGeneratorResponse protocol buffer to standard output. This package
+// provides support for writing plugins which generate Go code.
+package protogen
+
+import (
+	"bytes"
+	"fmt"
+	"io/ioutil"
+	"os"
+	"path/filepath"
+	"strings"
+
+	"github.com/golang/protobuf/proto"
+	descpb "github.com/golang/protobuf/protoc-gen-go/descriptor"
+	pluginpb "github.com/golang/protobuf/protoc-gen-go/plugin"
+)
+
+// Run executes a function as a protoc plugin.
+//
+// It reads a CodeGeneratorRequest message from os.Stdin, invokes the plugin
+// function, and writes a CodeGeneratorResponse message to os.Stdout.
+//
+// If a failure occurs while reading or writing, Run prints an error to
+// os.Stderr and calls os.Exit(1).
+func Run(f func(*Plugin) error) {
+	if err := run(f); err != nil {
+		fmt.Fprintf(os.Stderr, "%s: %v\n", filepath.Base(os.Args[0]), err)
+		os.Exit(1)
+	}
+}
+
+func run(f func(*Plugin) error) error {
+	in, err := ioutil.ReadAll(os.Stdin)
+	if err != nil {
+		return err
+	}
+	req := &pluginpb.CodeGeneratorRequest{}
+	if err := proto.Unmarshal(in, req); err != nil {
+		return err
+	}
+	gen, err := New(req)
+	if err != nil {
+		return err
+	}
+	if err := f(gen); err != nil {
+		// Errors from the plugin function are reported by setting the
+		// error field in the CodeGeneratorResponse.
+		//
+		// In contrast, errors that indicate a problem in protoc
+		// itself (unparsable input, I/O errors, etc.) are reported
+		// to stderr.
+		gen.Error(err)
+	}
+	resp := gen.Response()
+	out, err := proto.Marshal(resp)
+	if err != nil {
+		return err
+	}
+	if _, err := os.Stdout.Write(out); err != nil {
+		return err
+	}
+	return nil
+}
+
+// A Plugin is a protoc plugin invocation.
+type Plugin struct {
+	// Request is the CodeGeneratorRequest provided by protoc.
+	Request *pluginpb.CodeGeneratorRequest
+
+	// Files is the set of files to generate and everything they import.
+	// Files appear in topological order, so each file appears before any
+	// file that imports it.
+	Files       []*File
+	filesByName map[string]*File
+
+	packageImportPath string // Go import path of the package we're generating code for.
+
+	genFiles []*GeneratedFile
+	err      error
+}
+
+// New returns a new Plugin.
+func New(req *pluginpb.CodeGeneratorRequest) (*Plugin, error) {
+	gen := &Plugin{
+		Request:     req,
+		filesByName: make(map[string]*File),
+	}
+
+	// TODO: Figure out how to pass parameters to the generator.
+	for _, param := range strings.Split(req.GetParameter(), ",") {
+		var value string
+		if i := strings.Index(param, "="); i >= 0 {
+			value = param[i+1:]
+			param = param[0:i]
+		}
+		switch param {
+		case "":
+			// Ignore.
+		case "import_prefix":
+			// TODO
+		case "import_path":
+			gen.packageImportPath = value
+		case "paths":
+			// TODO
+		case "plugins":
+			// TODO
+		case "annotate_code":
+			// TODO
+		default:
+			if param[0] != 'M' {
+				return nil, fmt.Errorf("unknown parameter %q", param)
+			}
+			// TODO
+		}
+	}
+
+	for _, fdesc := range gen.Request.ProtoFile {
+		f := newFile(gen, fdesc)
+		name := f.Desc.GetName()
+		if gen.filesByName[name] != nil {
+			return nil, fmt.Errorf("duplicate file name: %q", name)
+		}
+		gen.Files = append(gen.Files, f)
+		gen.filesByName[name] = f
+	}
+	for _, name := range gen.Request.FileToGenerate {
+		f, ok := gen.FileByName(name)
+		if !ok {
+			return nil, fmt.Errorf("no descriptor for generated file: %v", name)
+		}
+		f.Generate = true
+	}
+	return gen, nil
+}
+
+// Error records an error in code generation. The generator will report the
+// error back to protoc and will not produce output.
+func (gen *Plugin) Error(err error) {
+	if gen.err == nil {
+		gen.err = err
+	}
+}
+
+// Response returns the generator output.
+func (gen *Plugin) Response() *pluginpb.CodeGeneratorResponse {
+	resp := &pluginpb.CodeGeneratorResponse{}
+	if gen.err != nil {
+		resp.Error = proto.String(gen.err.Error())
+		return resp
+	}
+	for _, gf := range gen.genFiles {
+		resp.File = append(resp.File, &pluginpb.CodeGeneratorResponse_File{
+			Name:    proto.String(gf.path),
+			Content: proto.String(string(gf.Content())),
+		})
+	}
+	return resp
+}
+
+// FileByName returns the file with the given name.
+func (gen *Plugin) FileByName(name string) (f *File, ok bool) {
+	f, ok = gen.filesByName[name]
+	return f, ok
+}
+
+// A File is a .proto source file.
+type File struct {
+	// TODO: Replace with protoreflect.FileDescriptor.
+	Desc *descpb.FileDescriptorProto
+
+	// Generate is true if the generator should generate code for this file.
+	Generate bool
+}
+
+func newFile(gen *Plugin, p *descpb.FileDescriptorProto) *File {
+	return &File{
+		Desc: p,
+	}
+}
+
+// A GeneratedFile is a generated file.
+type GeneratedFile struct {
+	path string
+	buf  bytes.Buffer
+}
+
+// NewGeneratedFile creates a new generated file with the given path.
+func (gen *Plugin) NewGeneratedFile(path string) *GeneratedFile {
+	g := &GeneratedFile{
+		path: path,
+	}
+	gen.genFiles = append(gen.genFiles, g)
+	return g
+}
+
+// P prints a line to the generated output. It converts each parameter to a
+// string following the same rules as fmt.Print. It never inserts spaces
+// between parameters.
+//
+// TODO: .meta file annotations.
+func (g *GeneratedFile) P(v ...interface{}) {
+	for _, x := range v {
+		fmt.Fprint(&g.buf, x)
+	}
+	fmt.Fprintln(&g.buf)
+}
+
+// Write implements io.Writer.
+func (g *GeneratedFile) Write(p []byte) (n int, err error) {
+	return g.buf.Write(p)
+}
+
+// Content returns the contents of the generated file.
+func (g *GeneratedFile) Content() []byte {
+	return g.buf.Bytes()
+}