summaryrefslogtreecommitdiff
path: root/tutorial/go
diff options
context:
space:
mode:
authortaozle <zhangliyang26@gmail.com>2017-07-17 18:40:42 +0200
committerJens Geyer <jensg@apache.org>2017-07-22 19:42:48 +0200
commitc0d384a38c2b43ee47cef86b1cd054e3f84dc909 (patch)
tree8de48de44cc628d110ac6bee4bbd8fd74c86d833 /tutorial/go
parent0dd823580c78a79ae9696eb9b3650e400fff140f (diff)
downloadthrift-c0d384a38c2b43ee47cef86b1cd054e3f84dc909.tar.gz
THRIFT-4236 Support context in go generated code
Client: Go Patch: taozle <zhangliyang26@gmail.com> This closes #1309
Diffstat (limited to 'tutorial/go')
-rw-r--r--tutorial/go/Makefile.am17
-rw-r--r--tutorial/go/src/handler.go14
-rw-r--r--tutorial/go/src/handler_go17.go104
3 files changed, 123 insertions, 12 deletions
diff --git a/tutorial/go/Makefile.am b/tutorial/go/Makefile.am
index a707d5dd5..c328d38fe 100644
--- a/tutorial/go/Makefile.am
+++ b/tutorial/go/Makefile.am
@@ -20,27 +20,30 @@
THRIFT = $(top_builddir)/compiler/cpp/thrift
gen-go/tutorial/calculator.go gen-go/shared/shared_service.go: $(top_srcdir)/tutorial/tutorial.thrift
- $(THRIFT) --gen go -r $<
+ $(THRIFT) --gen go:legacy_context -r $<
all-local: gen-go/tutorial/calculator.go
-
-check: src/git.apache.org/thrift.git/lib/go/thrift
- $(THRIFT) -r --gen go $(top_srcdir)/tutorial/tutorial.thrift
+check: src/git.apache.org/thrift.git/lib/go/thrift thirdparty-dep
+ $(THRIFT) -r --gen go:legacy_context $(top_srcdir)/tutorial/tutorial.thrift
cp -r gen-go/* src/
- GOPATH=`pwd` $(GO) build ./...
- GOPATH=`pwd` $(GO) build -o go-tutorial src/*.go
+ GOPATH=`pwd` $(GO) build -o go-tutorial ./src
GOPATH=`pwd` $(GO) build -o calculator-remote src/tutorial/calculator-remote/calculator-remote.go
src/git.apache.org/thrift.git/lib/go/thrift:
mkdir -p src/git.apache.org/thrift.git/lib/go
ln -sf $(realpath $(top_srcdir)/lib/go/thrift) src/git.apache.org/thrift.git/lib/go/thrift
+thirdparty-dep:
+ mkdir -p src/golang.org/x/net
+ GOPATH=`pwd`/gopath $(GO) get golang.org/x/net/context
+ ln -sf `pwd`/gopath/src/golang.org/x/net/context src/golang.org/x/net/context
+
tutorialserver: all
GOPATH=`pwd` $(GO) run src/*.go -server=true
tutorialclient: all
- GOPATH=`pwd` $(GO) run src/*.go
+ GOPATH=`pwd` $(GO) run src/*.go
tutorialsecureserver: all
GOPATH=`pwd` $(GO) run src/*.go -server=true -secure=true
diff --git a/tutorial/go/src/handler.go b/tutorial/go/src/handler.go
index 17638322f..783b43267 100644
--- a/tutorial/go/src/handler.go
+++ b/tutorial/go/src/handler.go
@@ -1,3 +1,5 @@
+// +build !go1.7
+
package main
/*
@@ -24,6 +26,8 @@ import (
"shared"
"strconv"
"tutorial"
+
+ "golang.org/x/net/context"
)
type CalculatorHandler struct {
@@ -34,17 +38,17 @@ func NewCalculatorHandler() *CalculatorHandler {
return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
}
-func (p *CalculatorHandler) Ping() (err error) {
+func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
fmt.Print("ping()\n")
return nil
}
-func (p *CalculatorHandler) Add(num1 int32, num2 int32) (retval17 int32, err error) {
+func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
fmt.Print("add(", num1, ",", num2, ")\n")
return num1 + num2, nil
}
-func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32, err error) {
+func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
switch w.Op {
case tutorial.Operation_ADD:
@@ -89,13 +93,13 @@ func (p *CalculatorHandler) Calculate(logid int32, w *tutorial.Work) (val int32,
return val, err
}
-func (p *CalculatorHandler) GetStruct(key int32) (*shared.SharedStruct, error) {
+func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
fmt.Print("getStruct(", key, ")\n")
v, _ := p.log[int(key)]
return v, nil
}
-func (p *CalculatorHandler) Zip() (err error) {
+func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
fmt.Print("zip()\n")
return nil
}
diff --git a/tutorial/go/src/handler_go17.go b/tutorial/go/src/handler_go17.go
new file mode 100644
index 000000000..d6752cc78
--- /dev/null
+++ b/tutorial/go/src/handler_go17.go
@@ -0,0 +1,104 @@
+// +build go1.7
+
+package main
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import (
+ "context"
+ "fmt"
+ "shared"
+ "strconv"
+ "tutorial"
+)
+
+type CalculatorHandler struct {
+ log map[int]*shared.SharedStruct
+}
+
+func NewCalculatorHandler() *CalculatorHandler {
+ return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
+}
+
+func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
+ fmt.Print("ping()\n")
+ return nil
+}
+
+func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
+ fmt.Print("add(", num1, ",", num2, ")\n")
+ return num1 + num2, nil
+}
+
+func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
+ fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
+ switch w.Op {
+ case tutorial.Operation_ADD:
+ val = w.Num1 + w.Num2
+ break
+ case tutorial.Operation_SUBTRACT:
+ val = w.Num1 - w.Num2
+ break
+ case tutorial.Operation_MULTIPLY:
+ val = w.Num1 * w.Num2
+ break
+ case tutorial.Operation_DIVIDE:
+ if w.Num2 == 0 {
+ ouch := tutorial.NewInvalidOperation()
+ ouch.WhatOp = int32(w.Op)
+ ouch.Why = "Cannot divide by 0"
+ err = ouch
+ return
+ }
+ val = w.Num1 / w.Num2
+ break
+ default:
+ ouch := tutorial.NewInvalidOperation()
+ ouch.WhatOp = int32(w.Op)
+ ouch.Why = "Unknown operation"
+ err = ouch
+ return
+ }
+ entry := shared.NewSharedStruct()
+ entry.Key = logid
+ entry.Value = strconv.Itoa(int(val))
+ k := int(logid)
+ /*
+ oldvalue, exists := p.log[k]
+ if exists {
+ fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
+ } else {
+ fmt.Print("Adding ", entry, " for key ", k, "\n")
+ }
+ */
+ p.log[k] = entry
+ return val, err
+}
+
+func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
+ fmt.Print("getStruct(", key, ")\n")
+ v, _ := p.log[int(key)]
+ return v, nil
+}
+
+func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
+ fmt.Print("zip()\n")
+ return nil
+}