-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from aadedamola/add-optional-field-support
proto gen: support optionals for proto syntax 3
- Loading branch information
Showing
11 changed files
with
180 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
include ../env.mk | ||
|
||
$(shell go get github.com/googleapis/googleapis) | ||
GOOGLEAPIS_PKG:=$(shell go list -m all | grep github.com/googleapis/googleapis | awk '{print ($$4 != "" ? $$4 : $$1)}') | ||
GOOGLEAPIS_VERSION:=$(shell go list -m all | grep github.com/googleapis/googleapis | awk '{print ($$5 != "" ? $$5 : $$2)}') | ||
GOOGLEAPIS_PATH:=${FIRST_GOPATH}/pkg/mod/${GOOGLEAPIS_PKG}@${GOOGLEAPIS_VERSION} | ||
|
||
pwd: | ||
@pwd | ||
|
||
clean: | ||
rm -f ./pb/strings.pb.go | ||
rm -f ./pb/strings_grpc.pb.go | ||
rm -f ./pb/strings.pb.goclay.go | ||
rm -f ./strings/strings.go | ||
rm -f main | ||
|
||
protoc: .protoc_pb | ||
|
||
build: .build | ||
|
||
test: pwd clean protoc build | ||
go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
"github.com/utrack/clay/integration/binding_with_optional_field/strings" | ||
) | ||
|
||
func main() { | ||
r := chi.NewMux() | ||
desc := strings.NewStrings().GetDescription() | ||
desc.RegisterHTTP(r) | ||
|
||
r.Handle("/swagger.json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Set("Content-Type", "application/javascript") | ||
w.Write(desc.SwaggerDef()) | ||
})) | ||
|
||
http.ListenAndServe(":8080", r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
|
||
strings_pb "github.com/utrack/clay/integration/binding_with_optional_field/pb" | ||
strings_srv "github.com/utrack/clay/integration/binding_with_optional_field/strings" | ||
) | ||
|
||
func TestToUpper(t *testing.T) { | ||
ts := testServer() | ||
defer ts.Close() | ||
|
||
t.Run("GET nullable string in request and an object in response", func(t *testing.T) { | ||
httpClient := ts.Client() | ||
client := strings_pb.NewStringsHTTPClient(httpClient, ts.URL) | ||
|
||
strVal := "foo" | ||
req := &strings_pb.String{ | ||
Str: &strVal, | ||
} | ||
|
||
resp, err := client.ToUpper(context.Background(), req) | ||
if err != nil { | ||
t.Fatalf("expected err <nil>, got: %q", err) | ||
} | ||
|
||
got := resp.GetStr() | ||
expected := strings.ToUpper(req.GetStr()) | ||
if got != expected { | ||
t.Fatalf("expected %q, got: %q", expected, got) | ||
} | ||
}) | ||
|
||
t.Run("GET nil in request and nil in response", func(t *testing.T) { | ||
httpClient := ts.Client() | ||
client := strings_pb.NewStringsHTTPClient(httpClient, ts.URL) | ||
|
||
req := &strings_pb.String{ | ||
Str: nil, | ||
} | ||
resp, err := client.ToUpper(context.Background(), req) | ||
if err != nil { | ||
t.Fatalf("expected err <nil>, got: %q", err) | ||
} | ||
|
||
got := resp.Str | ||
if got != nil { | ||
t.Fatalf("expected nil, got: %s", *got) | ||
} | ||
|
||
}) | ||
} | ||
|
||
func testServer() *httptest.Server { | ||
mux := http.NewServeMux() | ||
desc := strings_srv.NewStrings().GetDescription() | ||
desc.RegisterHTTP(mux) | ||
mux.Handle("/swagger.json", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
w.Header().Set("Content-Type", "application/javascript") | ||
w.Write(desc.SwaggerDef()) | ||
})) | ||
ts := httptest.NewServer(mux) | ||
return ts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package strings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
syntax = "proto3"; | ||
|
||
option go_package = "github.com/utrack/clay/integration/binding_with_optional_field/pb;strings"; | ||
// or just | ||
//option go_package = "./pb;strings"; | ||
|
||
import "google/api/annotations.proto"; | ||
|
||
service Strings { | ||
rpc ToUpper (String) returns (String) { | ||
option (google.api.http) = { | ||
get: "/strings/to_upper/v2" | ||
}; | ||
} | ||
} | ||
|
||
message String { | ||
optional string str = 1; | ||
} |
22 changes: 22 additions & 0 deletions
22
integration/binding_with_optional_field/strings/strings.to_upper.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters