Skip to content

Commit

Permalink
Merge pull request kubernetes#22850 from wojtek-t/refactor_assmble_file
Browse files Browse the repository at this point in the history
Auto commit by PR queue bot
  • Loading branch information
k8s-merge-robot committed Mar 12, 2016
2 parents 3b20881 + 0db2012 commit ec0c0d7
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 80 deletions.
4 changes: 2 additions & 2 deletions cmd/libs/go2idl/deepcopy-gen/generators/deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Packages(_ *generator.Context, arguments *args.GeneratorArgs) generator.Pac
return false
}
// Also, filter out private types.
if strings.ToLower(t.Name.Name[:1]) == t.Name.Name[:1] {
if namer.IsPrivateGoName(t.Name.Name) {
return false
}
return true
Expand Down Expand Up @@ -164,7 +164,7 @@ func (g *genDeepCopy) copyableWithinPackage(t *types.Type) bool {
// We won't be able to access private fields.
// Thus, this type cannot have private fields.
for _, member := range t.Members {
if strings.ToLower(member.Name[:1]) == member.Name[:1] {
if namer.IsPrivateGoName(member.Name) {
return false
}
// TODO: This is a temporary hack, to make avoid generating function
Expand Down
30 changes: 20 additions & 10 deletions cmd/libs/go2idl/generator/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ func (c *Context) ExecutePackages(outDir string, packages Packages) error {
return nil
}

type golangFileType struct{}
type DefaultFileType struct {
Format func([]byte) ([]byte, error)
Assemble func(io.Writer, *File)
}

func (ft golangFileType) AssembleFile(f *File, pathname string) error {
func (ft DefaultFileType) AssembleFile(f *File, pathname string) error {
log.Printf("Assembling file %q", pathname)
destFile, err := os.Create(pathname)
if err != nil {
Expand All @@ -69,12 +72,12 @@ func (ft golangFileType) AssembleFile(f *File, pathname string) error {

b := &bytes.Buffer{}
et := NewErrorTracker(b)
ft.assemble(et, f)
ft.Assemble(et, f)
if et.Error() != nil {
return et.Error()
}
if formatted, err := format.Source(b.Bytes()); err != nil {
err = fmt.Errorf("unable to run gofmt on %q (%v).", pathname, err)
if formatted, err := ft.Format(b.Bytes()); err != nil {
err = fmt.Errorf("unable to format file %q (%v).", pathname, err)
// Write the file anyway, so they can see what's going wrong and fix the generator.
if _, err2 := destFile.Write(b.Bytes()); err2 != nil {
return err2
Expand All @@ -86,18 +89,18 @@ func (ft golangFileType) AssembleFile(f *File, pathname string) error {
}
}

func (ft golangFileType) VerifyFile(f *File, pathname string) error {
func (ft DefaultFileType) VerifyFile(f *File, pathname string) error {
log.Printf("Verifying file %q", pathname)
friendlyName := filepath.Join(f.PackageName, f.Name)
b := &bytes.Buffer{}
et := NewErrorTracker(b)
ft.assemble(et, f)
ft.Assemble(et, f)
if et.Error() != nil {
return et.Error()
}
formatted, err := format.Source(b.Bytes())
formatted, err := ft.Format(b.Bytes())
if err != nil {
return fmt.Errorf("unable to gofmt the output for %q: %v", friendlyName, err)
return fmt.Errorf("unable to format the output for %q: %v", friendlyName, err)
}
existing, err := ioutil.ReadFile(pathname)
if err != nil {
Expand All @@ -121,7 +124,7 @@ func (ft golangFileType) VerifyFile(f *File, pathname string) error {
return fmt.Errorf("output for %q differs; first existing/expected diff: \n %q\n %q", friendlyName, string(eDiff), string(fDiff))
}

func (ft golangFileType) assemble(w io.Writer, f *File) {
func assembleGolangFile(w io.Writer, f *File) {
w.Write(f.Header)
fmt.Fprintf(w, "package %v\n\n", f.PackageName)

Expand Down Expand Up @@ -155,6 +158,13 @@ func (ft golangFileType) assemble(w io.Writer, f *File) {
w.Write(f.Body.Bytes())
}

func NewGolangFile() *DefaultFileType {
return &DefaultFileType{
Format: format.Source,
Assemble: assembleGolangFile,
}
}

// format should be one line only, and not end with \n.
func addIndentHeaderComment(b *bytes.Buffer, format string, args ...interface{}) {
if b.Len() > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/libs/go2idl/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func NewContext(b *parser.Builder, nameSystems namer.NameSystems, canonicalOrder
Namers: namer.NameSystems{},
Universe: u,
FileTypes: map[string]FileType{
GolangFileType: golangFileType{},
GolangFileType: NewGolangFile(),
},
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/libs/go2idl/go-to-protobuf/protobuf/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func Run(g *Generator) {
"public",
)
c.Verify = g.Common.VerifyOnly
c.FileTypes["protoidl"] = protoIDLFileType{}
c.FileTypes["protoidl"] = NewProtoFile()

if err != nil {
log.Fatalf("Failed making a context: %v", err)
Expand Down
77 changes: 12 additions & 65 deletions cmd/libs/go2idl/go-to-protobuf/protobuf/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ limitations under the License.
package protobuf

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"reflect"
"sort"
"strconv"
Expand Down Expand Up @@ -231,7 +227,7 @@ func (b bodyGen) doStruct(sw *generator.SnippetWriter) error {
if len(b.t.Name.Name) == 0 {
return nil
}
if isPrivateGoName(b.t.Name.Name) {
if namer.IsPrivateGoName(b.t.Name.Name) {
return nil
}

Expand Down Expand Up @@ -525,7 +521,7 @@ func membersToFields(locator ProtobufLocator, t *types.Type, localPackage types.
fields := []protoField{}

for _, m := range t.Members {
if isPrivateGoName(m.Name) {
if namer.IsPrivateGoName(m.Name) {
// skip private fields
continue
}
Expand Down Expand Up @@ -561,7 +557,7 @@ func membersToFields(locator ProtobufLocator, t *types.Type, localPackage types.
}
}
if len(field.Name) == 0 {
field.Name = strings.ToLower(m.Name[:1]) + m.Name[1:]
field.Name = namer.IL(m.Name)
}

if field.Map && field.Repeated {
Expand All @@ -573,7 +569,7 @@ func membersToFields(locator ProtobufLocator, t *types.Type, localPackage types.
if !field.Nullable {
field.Extras["(gogoproto.nullable)"] = "false"
}
if (field.Type.Name.Name == "bytes" && field.Type.Name.Package == "") || (field.Repeated && field.Type.Name.Package == "" && isPrivateGoName(field.Type.Name.Name)) {
if (field.Type.Name.Name == "bytes" && field.Type.Name.Package == "") || (field.Repeated && field.Type.Name.Package == "" && namer.IsPrivateGoName(field.Type.Name.Name)) {
delete(field.Extras, "(gogoproto.nullable)")
}
if field.Name != m.Name {
Expand Down Expand Up @@ -627,61 +623,12 @@ func genComment(out io.Writer, comment, indent string) {
}
}

type protoIDLFileType struct{}

func (ft protoIDLFileType) AssembleFile(f *generator.File, pathname string) error {
log.Printf("Assembling IDL file %q", pathname)
destFile, err := os.Create(pathname)
if err != nil {
return err
}
defer destFile.Close()

b := &bytes.Buffer{}
et := generator.NewErrorTracker(b)
ft.assemble(et, f)
if et.Error() != nil {
return et.Error()
}

// TODO: is there an IDL formatter?
_, err = destFile.Write(b.Bytes())
return err
func formatProtoFile(source []byte) ([]byte, error) {
// TODO; Is there any protobuf formatter?
return source, nil
}

func (ft protoIDLFileType) VerifyFile(f *generator.File, pathname string) error {
log.Printf("Verifying IDL file %q", pathname)
friendlyName := filepath.Join(f.PackageName, f.Name)
b := &bytes.Buffer{}
et := generator.NewErrorTracker(b)
ft.assemble(et, f)
if et.Error() != nil {
return et.Error()
}
formatted := b.Bytes()
existing, err := ioutil.ReadFile(pathname)
if err != nil {
return fmt.Errorf("unable to read file %q for comparison: %v", friendlyName, err)
}
if bytes.Compare(formatted, existing) == 0 {
return nil
}
// Be nice and find the first place where they differ
i := 0
for i < len(formatted) && i < len(existing) && formatted[i] == existing[i] {
i++
}
eDiff, fDiff := existing[i:], formatted[i:]
if len(eDiff) > 100 {
eDiff = eDiff[:100]
}
if len(fDiff) > 100 {
fDiff = fDiff[:100]
}
return fmt.Errorf("output for %q differs; first existing/expected diff: \n %q\n %q", friendlyName, string(eDiff), string(fDiff))
}

func (ft protoIDLFileType) assemble(w io.Writer, f *generator.File) {
func assembleProtoFile(w io.Writer, f *generator.File) {
w.Write(f.Header)

fmt.Fprint(w, "syntax = 'proto2';\n\n")
Expand Down Expand Up @@ -709,9 +656,9 @@ func (ft protoIDLFileType) assemble(w io.Writer, f *generator.File) {
w.Write(f.Body.Bytes())
}

func isPrivateGoName(name string) bool {
if len(name) == 0 {
return true
func NewProtoFile() *generator.DefaultFileType {
return &generator.DefaultFileType{
Format: formatProtoFile,
Assemble: assembleProtoFile,
}
return strings.ToLower(name[:1]) == name[:1]
}
3 changes: 2 additions & 1 deletion cmd/libs/go2idl/go-to-protobuf/protobuf/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"k8s.io/kubernetes/cmd/libs/go2idl/generator"
"k8s.io/kubernetes/cmd/libs/go2idl/namer"
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

Expand Down Expand Up @@ -124,7 +125,7 @@ func assignGoTypeToProtoPackage(p *protobufPackage, t *types.Type, local, global

local[t.Name] = p
for _, m := range t.Members {
if isPrivateGoName(m.Name) {
if namer.IsPrivateGoName(m.Name) {
continue
}
field := &protoField{}
Expand Down
5 changes: 5 additions & 0 deletions cmd/libs/go2idl/namer/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"k8s.io/kubernetes/cmd/libs/go2idl/types"
)

// Returns whether a name is a private Go name.
func IsPrivateGoName(name string) bool {
return len(name) == 0 || strings.ToLower(name[:1]) == name[:1]
}

// NewPublicNamer is a helper function that returns a namer that makes
// CamelCase names. See the NameStrategy struct for an explanation of the
// arguments to this constructor.
Expand Down

0 comments on commit ec0c0d7

Please sign in to comment.