blob: 783b43267d495a54eff3e7c7594fc5d33475fd42 [file] [log] [blame]
taozlec0d384a2017-07-17 18:40:42 +02001// +build !go1.7
2
Jens Geyer0e87c462013-06-18 22:25:07 +02003package 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 "fmt"
26 "shared"
27 "strconv"
28 "tutorial"
taozlec0d384a2017-07-17 18:40:42 +020029
30 "golang.org/x/net/context"
Jens Geyer0e87c462013-06-18 22:25:07 +020031)
32
33type CalculatorHandler struct {
34 log map[int]*shared.SharedStruct
35}
36
37func NewCalculatorHandler() *CalculatorHandler {
38 return &CalculatorHandler{log: make(map[int]*shared.SharedStruct)}
39}
40
taozlec0d384a2017-07-17 18:40:42 +020041func (p *CalculatorHandler) Ping(ctx context.Context) (err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020042 fmt.Print("ping()\n")
43 return nil
44}
45
taozlec0d384a2017-07-17 18:40:42 +020046func (p *CalculatorHandler) Add(ctx context.Context, num1 int32, num2 int32) (retval17 int32, err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020047 fmt.Print("add(", num1, ",", num2, ")\n")
48 return num1 + num2, nil
49}
50
taozlec0d384a2017-07-17 18:40:42 +020051func (p *CalculatorHandler) Calculate(ctx context.Context, logid int32, w *tutorial.Work) (val int32, err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020052 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 Geyer38b1a042014-02-04 23:56:39 +010065 ouch := tutorial.NewInvalidOperation()
Jens Geyer04fdd3a2015-05-20 22:35:54 +020066 ouch.WhatOp = int32(w.Op)
Jens Geyer0e87c462013-06-18 22:25:07 +020067 ouch.Why = "Cannot divide by 0"
Jens Geyer38b1a042014-02-04 23:56:39 +010068 err = ouch
Jens Geyer0e87c462013-06-18 22:25:07 +020069 return
70 }
71 val = w.Num1 / w.Num2
72 break
73 default:
Jens Geyer38b1a042014-02-04 23:56:39 +010074 ouch := tutorial.NewInvalidOperation()
Jens Geyer04fdd3a2015-05-20 22:35:54 +020075 ouch.WhatOp = int32(w.Op)
Jens Geyer0e87c462013-06-18 22:25:07 +020076 ouch.Why = "Unknown operation"
Jens Geyer38b1a042014-02-04 23:56:39 +010077 err = ouch
Jens Geyer0e87c462013-06-18 22:25:07 +020078 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 Geyer38b1a042014-02-04 23:56:39 +010093 return val, err
Jens Geyer0e87c462013-06-18 22:25:07 +020094}
95
taozlec0d384a2017-07-17 18:40:42 +020096func (p *CalculatorHandler) GetStruct(ctx context.Context, key int32) (*shared.SharedStruct, error) {
Jens Geyer0e87c462013-06-18 22:25:07 +020097 fmt.Print("getStruct(", key, ")\n")
98 v, _ := p.log[int(key)]
99 return v, nil
100}
101
taozlec0d384a2017-07-17 18:40:42 +0200102func (p *CalculatorHandler) Zip(ctx context.Context) (err error) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200103 fmt.Print("zip()\n")
104 return nil
105}