forked from arsham/expipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
120 lines (107 loc) · 2.8 KB
/
helper.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2016 Arsham Shirvani <arshamshirvani@gmail.com>. All rights reserved.
// Use of this source code is governed by the Apache 2.0 license
// License that can be found in the LICENSE file.
package reader
import (
"time"
"github.com/arsham/expipe/datatype"
"github.com/arsham/expipe/tools"
"github.com/pkg/errors"
)
// This file contains the construction functions required for instantiating
// a Reader object. Input variables are sanitised here.
// Constructor is a Reader object that will accept configurations.
type Constructor interface {
SetLogger(logger tools.FieldLogger)
SetName(name string)
SetTypeName(typeName string)
SetEndpoint(endpoint string)
SetMapper(mapper datatype.Mapper)
SetInterval(interval time.Duration)
SetTimeout(timeout time.Duration)
SetBackoff(backoff int)
}
// WithLogger sets the log of the reader.
func WithLogger(log tools.FieldLogger) func(Constructor) error {
return func(e Constructor) error {
if log == nil {
return ErrNillLogger
}
e.SetLogger(log)
return nil
}
}
// WithName sets the name of the reader.
func WithName(name string) func(Constructor) error {
return func(e Constructor) error {
if name == "" {
return ErrEmptyName
}
e.SetName(name)
return nil
}
}
// WithEndpoint sets the endpoint of the reader.
func WithEndpoint(endpoint string) func(Constructor) error {
return func(e Constructor) error {
if endpoint == "" {
return ErrEmptyEndpoint
}
url, err := tools.SanitiseURL(endpoint)
if err != nil {
return InvalidEndpointError(endpoint)
}
e.SetEndpoint(url)
return nil
}
}
// WithMapper sets the mapper of the reader.
func WithMapper(mapper datatype.Mapper) func(Constructor) error {
return func(e Constructor) error {
if mapper == nil {
return errors.New("nil mapper")
}
e.SetMapper(mapper)
return nil
}
}
// WithTypeName sets the typeName of the reader.
func WithTypeName(typeName string) func(Constructor) error {
return func(e Constructor) error {
if typeName == "" {
return ErrEmptyTypeName
}
e.SetTypeName(typeName)
return nil
}
}
// WithInterval sets the interval of the reader.
func WithInterval(interval time.Duration) func(Constructor) error {
return func(e Constructor) error {
if interval == time.Duration(0) {
return LowIntervalError(interval)
}
e.SetInterval(interval)
return nil
}
}
// WithTimeout sets the timeout of the reader.
func WithTimeout(timeout time.Duration) func(Constructor) error {
return func(e Constructor) error {
if timeout < time.Second {
return LowTimeoutError(timeout)
}
e.SetTimeout(timeout)
return nil
}
}
// WithBackoff sets the backoff of the reader.
func WithBackoff(backoff int) func(Constructor) error {
return func(e Constructor) error {
if backoff < 5 {
return LowBackoffValueError(backoff)
}
e.SetBackoff(backoff)
return nil
}
}