Skip to content

Commit

Permalink
fixed output issues, not where the file outputs to
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkBrand committed Dec 11, 2013
1 parent 7ea973d commit 7960379
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 33 deletions.
43 changes: 36 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
include $(GOROOT)/src/Make.$(GOARCH)
# Go support for Protocol Buffers - Google's data interchange format
#
# Copyright 2010 The Go Authors. All rights reserved.
# http://code.google.com/p/goprotobuf/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

TARG=path/to/example
GOFILES=\
test.pb.go\
other.go
all: install

include $(GOROOT)/src/Make.pkg
include $(GOROOT)/src/pkg/code.google.com/p/goprotobuf/Make.protobuf
test:
go build && ./protoc-gen-PBCF < ./test_protocol_buffer/sample.proto

install:
go install
47 changes: 31 additions & 16 deletions descriptor/FormattedString.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ func (this *FileDescriptorProto) FormattedGoString(depth int) string {

var s string

counter := 0

// the package
if len(this.GetPackage()) > 0 {
s += `package ` + this.GetPackage() + "\n"

counter += 1
}

// For each import
if len(this.GetDependency()) > 0 && len(this.GetPackage()) > 0 {
if len(this.GetDependency()) > 0 && counter > 0 {
s += "\n"
}
if len(this.GetDependency()) > 0 {
Expand All @@ -63,6 +67,8 @@ func (this *FileDescriptorProto) FormattedGoString(depth int) string {

s += `import "` + imp + `";` + "\n"
}

counter += 1
}

// For each extend
Expand All @@ -71,53 +77,62 @@ func (this *FileDescriptorProto) FormattedGoString(depth int) string {
extendGroups[ext.GetExtendee()] = extendGroups[ext.GetExtendee()] + ext.FormattedGoString(depth+1) + ";\n"

}
if len(extendGroups) > 0 {
if len(extendGroups) > 0 && counter > 0 {
s += "\n"
}
for i := range extendGroups {
group := extendGroups[i]
s += getIndentation(depth) + `extend ` + strings.Replace(i, ".", "", 1) + " {\n"
s += group
s += getIndentation(depth) + "}\n"

counter += 1
}

// Field Options
// File Options
options := this.GetOptions()
if options != nil && len(options.ExtensionMap()) > 0 {
s += "\n"

s += getFormattedOptionsFromExtensionMap(options.ExtensionMap(), -1, false)

counter += 1
}

// Enums
if len(this.GetEnumType()) > 0 {
if len(this.GetEnumType()) > 0 && counter > 0 {
s += "\n"
}
for _, enum := range this.GetEnumType() {
s += enum.FormattedGoString(depth)
s += "\n"

counter += 1
}

// Messages
if len(this.GetMessageType()) > 0 {
if len(this.GetMessageType()) > 0 && counter > 0 {
s += "\n"
}
for _, message := range this.GetMessageType() {
s += message.FormattedGoString(depth, false)
s += "\n"

counter += 1
}

// Services
if len(this.GetService()) > 0 && counter > 0 {
s += "\n"
}
for _, service := range this.GetService() {
s += service.FormattedGoString(depth)
s += "\n"

counter += 1
}

// SourceCodeInfo
source := this.GetSourceCodeInfo()
for _, sourceLoc := range source.GetLocation() {
s += sourceLoc.String()
// TODO comments
}

return s
}
Expand All @@ -135,9 +150,9 @@ func (this *DescriptorProto) FormattedGoString(depth int, isGroup bool) string {

// Message Header
if isGroup {
s += getIndentation(depth) + `group ` + fmt.Sprintf("%v", this.GetName()) + ` {`
s += getIndentation(depth) + `group ` + this.GetName() + ` {`
} else {
s += getIndentation(depth) + `message ` + fmt.Sprintf("%v", this.GetName()) + ` {`
s += getIndentation(depth) + `message ` + this.GetName() + ` {`
}

// Extension Range
Expand Down Expand Up @@ -239,14 +254,14 @@ func (this *FieldDescriptorProto) FormattedGoString(depth int) string {
return "nil"
}
var s string
s += getIndentation(depth) + fmt.Sprintf("%v", fieldDescriptorProtoLabel_StringValue(*this.Label))
s += getIndentation(depth) + fieldDescriptorProtoLabel_StringValue(*this.Label)
// If referencing a message
if *this.Type == FieldDescriptorProto_TYPE_MESSAGE || *this.Type == FieldDescriptorProto_TYPE_ENUM {
s += ` ` + strings.Replace(this.GetTypeName(), ".", "", 1)
} else {
s += ` ` + fmt.Sprintf("%v", fieldDescriptorProtoType_StringValue(*this.Type))
s += ` ` + fieldDescriptorProtoType_StringValue(*this.Type)
}
s += ` ` + fmt.Sprintf("%v", this.GetName()) + ` = ` + fmt.Sprintf("%v", this.GetNumber())
s += ` ` + this.GetName() + ` = ` + fmt.Sprintf("%v", this.GetNumber())

// OPTIONS
options := this.GetOptions()
Expand Down Expand Up @@ -301,7 +316,7 @@ func (this *EnumDescriptorProto) FormattedGoString(depth int) string {
return "nil"
}
var s string
s += getIndentation(depth) + `enum ` + fmt.Sprintf("%v", this.GetName()) + ` {`
s += getIndentation(depth) + `enum ` + this.GetName() + ` {`

// Options
options := this.GetOptions()
Expand Down
27 changes: 20 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package main

import (
proto "code.google.com/p/gogoprotobuf/proto"
"fmt"
parser "github.com/DirkBrand/protoc-gen-PBCF/parser"
plugin "github.com/DirkBrand/protoc-gen-PBCF/plugin"
"io/ioutil"
"os"
"path"
//"strings"
)

func main() {
Expand All @@ -17,18 +18,19 @@ func main() {
Request := new(plugin.CodeGeneratorRequest) // The input.
Response := new(plugin.CodeGeneratorResponse) // The output.

os.Stdout.Write(data)
if err != nil {
Response.Error = proto.String("reading input")
} else {

if err = proto.Unmarshal(data, Request); err != nil {
Response.Error = proto.String("parsing input proto")
}
if len(Request.FileToGenerate) == 0 {
if len(Request.GetFileToGenerate()) == 0 {
Response.Error = proto.String("No files to generate")
}

formattedFiles := make(map[string]string, len(Request.GetFileToGenerate()))
formattedFiles := make(map[string]string)

for _, fileToGen := range Request.GetFileToGenerate() {
for _, protoFile := range Request.GetProtoFile() {
Expand All @@ -54,11 +56,22 @@ func main() {
if err2 != nil {
Response.Error = proto.String(err2.Error())
} else {

Response.File[i] = new(plugin.CodeGeneratorResponse_File)

Response.File[i].Name = proto.String(`fixed_` + fileName)
// Adds `_fixed`
//fileName = strings.Split(fileName, ".")[0] + "_fixed." + strings.Split(fileName, ".")[1]

ext := path.Ext(fileName)
if ext == ".proto" || ext == ".protodevel" {
fileName = path.Base(fileName)
}
fileName += ".pb.go"

Response.File[i].Name = proto.String(fileName)
Response.File[i].Content = proto.String(formatFile)

//os.Stderr.Write([]byte(Response.File[i].GetName()))
//os.Stderr.Write([]byte(Response.File[i].GetContent()))
i += 1

}
Expand All @@ -67,11 +80,11 @@ func main() {
// Send back the results.
data, err = proto.Marshal(Response)
if err != nil {
fmt.Println("failed to marshal output proto")
os.Stderr.Write([]byte("failed to marshal output proto"))
}
_, err = os.Stdout.Write(data)
if err != nil {
fmt.Println("failed to write output proto")
os.Stderr.Write([]byte("failed to write output proto"))
}

}
Expand Down
42 changes: 42 additions & 0 deletions plugin/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Go support for Protocol Buffers - Google's data interchange format
#
# Copyright 2010 The Go Authors. All rights reserved.
# http://code.google.com/p/goprotobuf/
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Not stored here, but plugin.proto is in http://code.google.com/p/protobuf
# at protobuf-2.3.0/src/google/protobuf/compiler/plugin.proto
# Also we need to fix an import.
regenerate:
(cd ../../../../../ && protoc --gogo_out=. ./code.google.com/p/gogoprotobuf/protoc-gen-gogo/plugin/plugin.proto)

restore:
cp plugin.pb.golden plugin.pb.go

preserve:
cp plugin.pb.go plugin.pb.golden
Loading

0 comments on commit 7960379

Please sign in to comment.