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