blob: 7645fc2a56df9cadbf0b209f4b0028a14a2d91fd [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001package main
2
3/*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22import (
John Boiles57852792018-01-05 14:37:05 -080023 "context"
Jens Geyer0e87c462013-06-18 22:25:07 +020024 "fmt"
Jens Geyer0e87c462013-06-18 22:25:07 +020025 "strconv"
Yuxuan 'fishy' Wangb71f11e2021-03-22 15:01:00 -070026
27 "github.com/apache/thrift/tutorial/go/gen-go/shared"
28 "github.com/apache/thrift/tutorial/go/gen-go/tutorial"
Jens Geyer0e87c462013-06-18 22:25:07 +020029)
30
31type CalculatorHandler struct {
32 log map[int]*shared.SharedStruct
33}
34
35func NewCalculatorHandler() *CalculatorHandler {
36 return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
37}
38
taozlec0d384a2017-07-17 18:40:42 +020039func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020040 fmt.Print("ping()\n")
41 return nil
42}
43
taozlec0d384a2017-07-17 18:40:42 +020044func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020045 fmt.Print("add(", num1, ",", num2, ")\n")
46 return num1 + num2, nil
47}
48
taozlec0d384a2017-07-17 18:40:42 +020049func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020050 fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
51 switch w.Op {
52 case tutorial.Operation_ADD:
53 val = w.Num1 + w.Num2
54 break
55 case tutorial.Operation_SUBTRACT:
56 val = w.Num1 - w.Num2
57 break
58 case tutorial.Operation_MULTIPLY:
59 val = w.Num1 * w.Num2
60 break
61 case tutorial.Operation_DIVIDE:
62 if w.Num2 == 0 {
Jens Geyer38b1a042014-02-04 23:56:39 +010063 ouch := tutorial.NewInvalidOperation()
Jens Geyer04fdd3a2015-05-20 22:35:54 +020064 ouch.WhatOp = int32(w.Op)
Jens Geyer0e87c462013-06-18 22:25:07 +020065 ouch.Why = "Cannot divide by 0"
Jens Geyer38b1a042014-02-04 23:56:39 +010066 err = ouch
Jens Geyer0e87c462013-06-18 22:25:07 +020067 return
68 }
69 val = w.Num1 / w.Num2
70 break
71 default:
Jens Geyer38b1a042014-02-04 23:56:39 +010072 ouch := tutorial.NewInvalidOperation()
Jens Geyer04fdd3a2015-05-20 22:35:54 +020073 ouch.WhatOp = int32(w.Op)
Jens Geyer0e87c462013-06-18 22:25:07 +020074 ouch.Why = "Unknown operation"
Jens Geyer38b1a042014-02-04 23:56:39 +010075 err = ouch
Jens Geyer0e87c462013-06-18 22:25:07 +020076 return
77 }
78 entry := shared.NewSharedStruct()
79 entry.Key = logid
80 entry.Value = strconv.Itoa(int(val))
81 k := int(logid)
82 /*
83 oldvalue, exists := p.log[k]
84 if exists {
85 fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
86 } else {
87 fmt.Print("Adding ", entry, " for key ", k, "\n")
88 }
89 */
90 p.log[k] = entry
Jens Geyer38b1a042014-02-04 23:56:39 +010091 return val, err
Jens Geyer0e87c462013-06-18 22:25:07 +020092}
93
taozlec0d384a2017-07-17 18:40:42 +020094func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020095 fmt.Print("getStruct(", key, ")\n")
96 v, _ := p.log[int(key)]
97 return v, nil
98}
99
taozlec0d384a2017-07-17 18:40:42 +0200100func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200101 fmt.Print("zip()\n")
102 return nil
103}