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