1package 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 (
23	"context"
24	"fmt"
25	"strconv"
26
27	"github.com/apache/thrift/tutorial/go/gen-go/shared"
28	"github.com/apache/thrift/tutorial/go/gen-go/tutorial"
29)
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
39func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
40	fmt.Print("ping()\n")
41	return nil
42}
43
44func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
45	fmt.Print("add(", num1, ",", num2, ")\n")
46	return num1 + num2, nil
47}
48
49func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
50	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	case tutorial.Operation_SUBTRACT:
55		val = w.Num1 - w.Num2
56	case tutorial.Operation_MULTIPLY:
57		val = w.Num1 * w.Num2
58	case tutorial.Operation_DIVIDE:
59		if w.Num2 == 0 {
60			ouch := tutorial.NewInvalidOperation()
61			ouch.WhatOp = int32(w.Op)
62			ouch.Why = "Cannot divide by 0"
63			err = ouch
64			return
65		}
66		val = w.Num1 / w.Num2
67	default:
68		ouch := tutorial.NewInvalidOperation()
69		ouch.WhatOp = int32(w.Op)
70		ouch.Why = "Unknown operation"
71		err = ouch
72		return
73	}
74	entry := shared.NewSharedStruct()
75	entry.Key = logid
76	entry.Value = strconv.Itoa(int(val))
77	k := int(logid)
78	/*
79	   oldvalue, exists := p.log[k]
80	   if exists {
81	     fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
82	   } else {
83	     fmt.Print("Adding ", entry, " for key ", k, "\n")
84	   }
85	*/
86	p.log[k] = entry
87	return val, err
88}
89
90func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
91	fmt.Print("getStruct(", key, ")\n")
92	v := p.log[int(key)]
93	return v, nil
94}
95
96func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
97	fmt.Print("zip()\n")
98	return nil
99}
100