Skip to content

Commit e6bb4f2

Browse files
committed
Attemp to extract some common functionality
1 parent c7960b5 commit e6bb4f2

File tree

12 files changed

+136
-173
lines changed

12 files changed

+136
-173
lines changed

go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/tclem/twirp-haskell
2+
3+
go 1.12
4+
5+
require (
6+
github.com/gogo/protobuf v1.2.1
7+
github.com/golang/protobuf v1.3.1
8+
github.com/pkg/errors v0.8.1 // indirect
9+
github.com/twitchtv/protogen v0.1.0
10+
)

go.sum

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
2+
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
3+
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
4+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5+
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
6+
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
7+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
8+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
9+
github.com/twitchtv/protogen v0.1.0 h1:x1CB37xjXLJRL97rOYVVqlNot0f9GVYQ4PRlbDEB58U=
10+
github.com/twitchtv/protogen v0.1.0/go.mod h1:jNxPwuzmhx8IkYq4Z5Qx6PWty1cQZZW9UV7kMhMvb+E=
11+
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

internal/gen/haskell/common.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package haskell
2+
3+
import (
4+
"github.com/golang/protobuf/proto"
5+
"github.com/golang/protobuf/protoc-gen-go/descriptor"
6+
)
7+
8+
func GetHaskellPackageOption(file *descriptor.FileDescriptorProto) string {
9+
ex, _ := proto.GetExtension(file.Options, E_HaskellPackage)
10+
11+
if ex != nil {
12+
asString := *ex.(*string)
13+
return asString
14+
}
15+
return ""
16+
}

internal/gen/haskell/haskell.pb.go

+72
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/twirp/protobuf/haskell.proto renamed to internal/gen/haskell/haskell.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ syntax = "proto3";
22

33
import "google/protobuf/descriptor.proto";
44

5-
package twirp.protobuf.haskell;
5+
package twirp.haskell;
66

77
option go_package = "haskell";
88

protoc-gen-haskell/haskell.pb.go

-74
This file was deleted.

protoc-gen-haskell/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/golang/protobuf/proto"
2020
"github.com/golang/protobuf/protoc-gen-go/descriptor"
2121
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
22+
"github.com/tclem/twirp-haskell/internal/gen/haskell"
2223
"github.com/twitchtv/protogen/typemap"
2324
)
2425

@@ -78,7 +79,7 @@ func (g *generator) generateHaskellCode(file *descriptor.FileDescriptorProto) st
7879
print(b, "import Proto3.Suite.JSONPB as JSONPB")
7980
print(b, "import Proto3.Wire (at, oneof)")
8081

81-
ex, _ := proto.GetExtension(file.Options, E_Imports)
82+
ex, _ := proto.GetExtension(file.Options, haskell.E_Imports)
8283
if ex != nil {
8384
asString := *ex.(*string)
8485
imports := strings.Split(asString, ";")
@@ -454,7 +455,7 @@ func printToFromJSONInstances(b *bytes.Buffer, n string) {
454455

455456
// Reference: https://github.com/golang/protobuf/blob/c823c79ea1570fb5ff454033735a8e68575d1d0f/protoc-gen-go/descriptor/descriptor.proto#L136
456457
func toType(field *descriptor.FieldDescriptorProto, prefix string, suffix string) string {
457-
ex, _ := proto.GetExtension(field.Options, E_Type)
458+
ex, _ := proto.GetExtension(field.Options, haskell.E_Type)
458459
if ex != nil {
459460
return *ex.(*string)
460461
}
@@ -614,7 +615,7 @@ func toModuleName(file *descriptor.FileDescriptorProto) string {
614615
pkgName := file.GetPackage()
615616

616617
parts := []string{}
617-
haskellPackage := getHaskellPackageOption(file)
618+
haskellPackage := haskell.GetHaskellPackageOption(file)
618619
if haskellPackage != "" {
619620
parts = strings.Split(haskellPackage, ".")
620621
} else {
@@ -630,7 +631,7 @@ func toModuleName(file *descriptor.FileDescriptorProto) string {
630631
}
631632

632633
func getHaskellPackageOption(file *descriptor.FileDescriptorProto) string {
633-
ex, _ := proto.GetExtension(file.Options, E_HaskellPackage)
634+
ex, _ := proto.GetExtension(file.Options, haskell.E_HaskellPackage)
634635

635636
if ex != nil {
636637
asString := *ex.(*string)

protoc-gen-twirp_haskell/haskell.pb.go

-74
This file was deleted.

protoc-gen-twirp_haskell/main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/golang/protobuf/proto"
2222
"github.com/golang/protobuf/protoc-gen-go/descriptor"
2323
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
24+
"github.com/tclem/twirp-haskell/internal/gen/haskell"
2425
"github.com/twitchtv/protogen/typemap"
2526
)
2627

@@ -182,7 +183,7 @@ func toModuleName(file *descriptor.FileDescriptorProto) string {
182183
pkgName := file.GetPackage()
183184

184185
parts := []string{}
185-
haskellPackage := getHaskellPackageOption(file)
186+
haskellPackage := haskell.GetHaskellPackageOption(file)
186187
if haskellPackage != "" {
187188
parts = strings.Split(haskellPackage, ".")
188189
} else {
@@ -198,7 +199,7 @@ func toModuleName(file *descriptor.FileDescriptorProto) string {
198199
}
199200

200201
func getHaskellPackageOption(file *descriptor.FileDescriptorProto) string {
201-
ex, _ := proto.GetExtension(file.Options, E_HaskellPackage)
202+
ex, _ := proto.GetExtension(file.Options, haskell.E_HaskellPackage)
202203

203204
if ex != nil {
204205
asString := *ex.(*string)

script/protoc

+16-16
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
set -e
77
cd "$(dirname "$0")/.."
88

9-
protoc --proto_path=./proto \
10-
--plugin=protoc-gen-twirp_haskell=./script/run-twirp_haskell --twirp_haskell_out=./app/Twirp/Example/Haberdasher \
11-
--plugin=protoc-gen-haskell=./script/run-haskell --haskell_out=./app/Twirp/Example/Haberdasher \
12-
./proto/haberdasher.proto
9+
# Compile haskell.proto (extensions to protoc)
10+
protoc --proto_path=./internal/gen/haskell \
11+
--go_out=./internal/gen/haskell \
12+
haskell.proto
1313

1414
protoc --proto_path=./proto \
1515
--plugin=protoc-gen-twirp_haskell=./script/run-twirp_haskell --twirp_haskell_out=./app/Twirp/Example/Haberdasher \
1616
--plugin=protoc-gen-haskell=./script/run-haskell --haskell_out=./app/Twirp/Example/Haberdasher \
17-
./proto/wrapping.proto
18-
19-
# stack exec compile-proto-file -- --out . --includeDir ./proto --proto haberdasher.proto
20-
21-
# Generate a python client
22-
PYTHON_CLIENT_PATH=./clients/python/haberdasher
23-
protoc --proto_path=./proto \
24-
--python_out="$PYTHON_CLIENT_PATH" \
25-
--twirp_python_out="$PYTHON_CLIENT_PATH" \
2617
./proto/haberdasher.proto
2718

28-
# Show how to compile haskell.proto
2919
# protoc --proto_path=./proto \
30-
# --go_out=./protoc-gen-haskell \
31-
# ./proto/twirp/protobuf/haskell.proto
20+
# --plugin=protoc-gen-twirp_haskell=./script/run-twirp_haskell --twirp_haskell_out=./app/Twirp/Example/Haberdasher \
21+
# --plugin=protoc-gen-haskell=./script/run-haskell --haskell_out=./app/Twirp/Example/Haberdasher \
22+
# ./proto/wrapping.proto
23+
#
24+
# # stack exec compile-proto-file -- --out . --includeDir ./proto --proto haberdasher.proto
25+
#
26+
# # Generate a python client
27+
# PYTHON_CLIENT_PATH=./clients/python/haberdasher
28+
# protoc --proto_path=./proto \
29+
# --python_out="$PYTHON_CLIENT_PATH" \
30+
# --twirp_python_out="$PYTHON_CLIENT_PATH" \
31+
# ./proto/haberdasher.proto

script/run-haskell

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
cd "$(dirname "$0")/.."
44

5-
go run protoc-gen-haskell/main.go protoc-gen-haskell/haskell.pb.go protoc-gen-haskell/version.go
5+
go run protoc-gen-haskell/main.go protoc-gen-haskell/version.go

script/run-twirp_haskell

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
cd "$(dirname "$0")/.."
44

5-
go run protoc-gen-twirp_haskell/main.go protoc-gen-twirp_haskell/haskell.pb.go protoc-gen-twirp_haskell/version.go
5+
go run protoc-gen-twirp_haskell/main.go protoc-gen-twirp_haskell/version.go

0 commit comments

Comments
 (0)