Skip to content

Commit

Permalink
Improve templates, some minor package restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Apr 2, 2018
1 parent 9f60505 commit 498cd36
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 199 deletions.
28 changes: 5 additions & 23 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 0 additions & 31 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[[constraint]]
name = "github.com/Masterminds/sprig"
version = "2.14.1"
Expand All @@ -41,10 +14,6 @@
name = "github.com/pkg/errors"
version = "0.8.0"

[[constraint]]
name = "github.com/stretchr/testify"
version = "1.2.1"

[[constraint]]
name = "github.com/twitchtv/protogen"
version = "0.1.0"
Expand Down
2 changes: 1 addition & 1 deletion internal/gen/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/twirphp/protoc-gen-twirp_php/php"
"github.com/twirphp/protoc-gen-twirp_php/internal/php"
"github.com/twitchtv/protogen/typemap"
)

Expand Down
6 changes: 4 additions & 2 deletions internal/gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import (
"text/template"

"github.com/Masterminds/sprig"
"github.com/twirphp/protoc-gen-twirp_php/php"
"github.com/twirphp/protoc-gen-twirp_php/internal/php"
"github.com/twirphp/protoc-gen-twirp_php/internal/proto"
)

// TxtFuncMap wraps sprig.TxtFuncMap and adds some proto generation specific ones.
func TxtFuncMap(ctx *generatorContext) template.FuncMap {
funcMap := sprig.TxtFuncMap()

funcMap["protoComment"] = ProtoComment
funcMap["protoComment"] = proto.Comment
funcMap["protoFullName"] = proto.FullName

funcMap["phpNamespace"] = php.Namespace
funcMap["phpServiceName"] = php.ServiceName
Expand Down
File renamed without changes.
28 changes: 9 additions & 19 deletions internal/gen/template_proto.go → internal/proto/comment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gen
package proto

import (
"fmt"
"strconv"
"strings"

Expand All @@ -10,29 +9,29 @@ import (
"github.com/twitchtv/protogen/typemap"
)

// ProtoComment is a template wrapper for protocol buffer descriptor comments.
func ProtoComment(desc ...interface{}) (string, error) {
// Comment is a template wrapper for protocol buffer descriptor comments.
func Comment(desc ...interface{}) (string, error) {
switch len(desc) {
case 2:
file := desc[0].(*descriptor.FileDescriptorProto)
svc := desc[1].(*descriptor.ServiceDescriptorProto)

return ProtoServiceComment(file, svc), nil
return ServiceComment(file, svc), nil

case 3:
file := desc[0].(*descriptor.FileDescriptorProto)
svc := desc[1].(*descriptor.ServiceDescriptorProto)
method := desc[2].(*descriptor.MethodDescriptorProto)

return ProtoMethodComment(file, svc, method), nil
return MethodComment(file, svc, method), nil

default:
return "", errors.New("unexpected amount of arguments (expected 2 or 3, got " + strconv.Itoa(len(desc)))
}
}

// ProtoServiceComment extracts comments for a service.
func ProtoServiceComment(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto) string {
// ServiceComment extracts comments for a service.
func ServiceComment(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto) string {
reg := &typemap.Registry{}

comments, err := reg.ServiceComments(file, svc)
Expand All @@ -48,8 +47,8 @@ func ProtoServiceComment(file *descriptor.FileDescriptorProto, svc *descriptor.S
return text
}

// ProtoMethodComment extracts comments for a service method.
func ProtoMethodComment(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto) string {
// MethodComment extracts comments for a service method.
func MethodComment(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto) string {
reg := &typemap.Registry{}

comments, err := reg.MethodComments(file, svc, method)
Expand All @@ -64,12 +63,3 @@ func ProtoMethodComment(file *descriptor.FileDescriptorProto, svc *descriptor.Se

return text
}

// ProtoRelativeToPackage returns a type that is relative to the current file.
func ProtoRelativeToPackage(file *descriptor.FileDescriptorProto, typ string) string {
if file.Package == nil {
return typ
}

return strings.TrimPrefix(typ, fmt.Sprintf(".%s.", file.GetPackage()))
}
45 changes: 45 additions & 0 deletions internal/proto/name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package proto

import (
"errors"
"strconv"

"github.com/golang/protobuf/protoc-gen-go/descriptor"
)

// FullName is a template wrapper for protocol buffer descriptor names.
func FullName(desc ...interface{}) (string, error) {
switch len(desc) {
case 2:
file := desc[0].(*descriptor.FileDescriptorProto)
svc := desc[1].(*descriptor.ServiceDescriptorProto)

return ServiceFullName(file, svc), nil

case 3:
file := desc[0].(*descriptor.FileDescriptorProto)
svc := desc[1].(*descriptor.ServiceDescriptorProto)
method := desc[2].(*descriptor.MethodDescriptorProto)

return MethodFullName(file, svc, method), nil

default:
return "", errors.New("unexpected amount of arguments (expected 2 or 3, got " + strconv.Itoa(len(desc)))
}
}

// ServiceFullName creates a fully qualified name for the service.
func ServiceFullName(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto) string {
prefix := ""

if pkg := file.GetPackage(); pkg != "" {
prefix = pkg + "."
}

return prefix + svc.GetName()
}

// MethodFullName creates a fully qualified name for the service.
func MethodFullName(file *descriptor.FileDescriptorProto, svc *descriptor.ServiceDescriptorProto, method *descriptor.MethodDescriptorProto) string {
return ServiceFullName(file, svc) + "/" + method.GetName()
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func Main(in io.Reader, out io.Writer, templates string) error {
greq := &gen.Request{
CodeGeneratorRequest: req,
GlobalFiles: []string{
"global/Protocol.php",
"global/Server.php",
"global/TwirpServer.php",
"global/TwirpClient.php",
},
ServiceFiles: []string{
Expand Down
46 changes: 3 additions & 43 deletions templates/global/Server.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions templates/global/TwirpClient.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 498cd36

Please sign in to comment.