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