-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.go
68 lines (59 loc) · 1.33 KB
/
demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package demoapi
import (
"context"
"fmt"
"math"
"math/rand"
"time"
demo "github.com/joberly/demo-go-api/gen/demo"
"go.uber.org/zap"
)
// demo service example implementation.
// The example methods log the requests and return zero values.
type demosrvc struct {
rand *rand.Rand
logger *zap.Logger
}
// NewDemo returns the demo service implementation.
func NewDemo(logger *zap.Logger) demo.Service {
return &demosrvc{
rand: rand.New(rand.NewSource(time.Now().UnixMicro())),
logger: logger,
}
}
// Rand implements rand.
func (s *demosrvc) Rand(ctx context.Context, p *demo.RandPayload) (*demo.RandResult, error) {
s.logger.Info("demo.rand")
var min int64 = 0
var max int64 = math.MaxInt64
var numErrors = 0
if p.Max != nil {
max = *p.Max
if max <= 0 {
numErrors++
s.logger.Error("max must be greater than zero", zap.Int64("max", max))
}
}
if p.Min != nil {
min = *p.Min
if min < 0 {
numErrors++
s.logger.Error("min must be greater than or equal to zero", zap.Int64("min", min))
}
}
if max <= min {
numErrors++
s.logger.Error("max is not greater than min",
zap.Int64("min", min),
zap.Int64("max", max),
)
}
if numErrors > 0 {
return nil, fmt.Errorf("invalid argument(s)")
}
res := demo.RandResult{
Result: new(int64),
}
*res.Result = s.rand.Int63n(max-min) + min
return &res, nil
}