blob: d6752cc783e690e2cb43de39594bf84ab28bbf1b [file] [log] [blame]
taozlec0d384a2017-07-17 18:40:42 +02001// +build go1.7
2
3package main
4
5/*
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
23
24import (
25 "context"
26 "fmt"
27 "shared"
28 "strconv"
29 "tutorial"
30)
31
32type CalculatorHandler struct {
33 log map[int]*shared.SharedStruct
34}
35
36func NewCalculatorHandler() *CalculatorHandler {
37 return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
38}
39
40func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
41 fmt.Print("ping()\n")
42 return nil
43}
44
45func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
46 fmt.Print("add(", num1, ",", num2, ")\n")
47 return num1 + num2, nil
48}
49
50func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
51 fmt.Print("calculate(", logid, ", {", w.Op, ",", w.Num1, ",", w.Num2, "})\n")
52 switch w.Op {
53 case tutorial.Operation_ADD:
54 val = w.Num1 + w.Num2
55 break
56 case tutorial.Operation_SUBTRACT:
57 val = w.Num1 - w.Num2
58 break
59 case tutorial.Operation_MULTIPLY:
60 val = w.Num1 * w.Num2
61 break
62 case tutorial.Operation_DIVIDE:
63 if w.Num2 == 0 {
64 ouch := tutorial.NewInvalidOperation()
65 ouch.WhatOp = int32(w.Op)
66 ouch.Why = "Cannot divide by 0"
67 err = ouch
68 return
69 }
70 val = w.Num1 / w.Num2
71 break
72 default:
73 ouch := tutorial.NewInvalidOperation()
74 ouch.WhatOp = int32(w.Op)
75 ouch.Why = "Unknown operation"
76 err = ouch
77 return
78 }
79 entry := shared.NewSharedStruct()
80 entry.Key = logid
81 entry.Value = strconv.Itoa(int(val))
82 k := int(logid)
83 /*
84 oldvalue, exists := p.log[k]
85 if exists {
86 fmt.Print("Replacing ", oldvalue, " with ", entry, " for key ", k, "\n")
87 } else {
88 fmt.Print("Adding ", entry, " for key ", k, "\n")
89 }
90 */
91 p.log[k] = entry
92 return val, err
93}
94
95func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
96 fmt.Print("getStruct(", key, ")\n")
97 v, _ := p.log[int(key)]
98 return v, nil
99}
100
101func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
102 fmt.Print("zip()\n")
103 return nil
104}