forked from project-alvarium/alvarium-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk.go
140 lines (127 loc) · 3.73 KB
/
sdk.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*******************************************************************************
* Copyright 2021 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package pkg
import (
"context"
"fmt"
"github.com/DyrellC/alvarium-sdk-go/internal/annotators"
"github.com/DyrellC/alvarium-sdk-go/pkg/config"
"github.com/DyrellC/alvarium-sdk-go/pkg/contracts"
"github.com/DyrellC/alvarium-sdk-go/pkg/factories"
"github.com/DyrellC/alvarium-sdk-go/pkg/interfaces"
"github.com/DyrellC/alvarium-sdk-go/pkg/message"
logInterface "github.com/project-alvarium/provider-logging/pkg/interfaces"
"github.com/project-alvarium/provider-logging/pkg/logging"
"sync"
)
type sdk struct {
annotators []interfaces.Annotator
cfg config.SdkInfo
stream interfaces.StreamProvider
logger logInterface.Logger
}
func NewSdk(annotators []interfaces.Annotator, cfg config.SdkInfo, logger logInterface.Logger) interfaces.Sdk {
instance := sdk{
annotators: annotators,
cfg: cfg,
logger: logger,
}
return &instance
}
func (s *sdk) BootstrapHandler(ctx context.Context, wg *sync.WaitGroup) bool {
stream, err := factories.NewStreamProvider(s.cfg.Stream, s.logger)
if err != nil {
s.logger.Error(err.Error())
return false
}
s.stream = stream
//Connect to stream provider
err = s.stream.Connect()
if err != nil {
s.logger.Error(err.Error())
return false
}
s.logger.Write(logging.DebugLevel, "stream provider connection successful")
wg.Add(1)
go func() { // Graceful shutdown
defer wg.Done()
<-ctx.Done()
s.logger.Write(logging.InfoLevel, "shutdown received")
s.stream.Close()
}()
return true
}
func (s *sdk) Create(ctx context.Context, data []byte) {
var list contracts.AnnotationList
for _, a := range s.annotators {
annotation, err := a.Do(ctx, data)
if err != nil {
s.logger.Error(err.Error())
return
}
list.Items = append(list.Items, annotation)
}
wrap := message.PublishWrapper{
Action: message.ActionCreate,
MessageType: fmt.Sprintf("%T", list),
Content: list,
}
err := s.stream.Publish(wrap)
if err != nil {
s.logger.Error(err.Error())
}
}
func (s *sdk) Mutate(ctx context.Context, old, new []byte) {
src := annotators.NewSourceAnnotator(s.cfg)
a, err := src.Do(ctx, old)
var list contracts.AnnotationList
list.Items = append(list.Items, a)
for _, a := range s.annotators {
annotation, err := a.Do(ctx, new)
if err != nil {
s.logger.Error(err.Error())
return
}
list.Items = append(list.Items, annotation)
}
wrap := message.PublishWrapper{
Action: message.ActionMutate,
MessageType: fmt.Sprintf("%T", list),
Content: list,
}
err = s.stream.Publish(wrap)
if err != nil {
s.logger.Error(err.Error())
}
}
func (s *sdk) Transit(ctx context.Context, data []byte) {
var list contracts.AnnotationList
for _, a := range s.annotators {
annotation, err := a.Do(ctx, data)
if err != nil {
s.logger.Error(err.Error())
return
}
list.Items = append(list.Items, annotation)
}
wrap := message.PublishWrapper{
Action: message.ActionTransit,
MessageType: fmt.Sprintf("%T", list),
Content: list,
}
err := s.stream.Publish(wrap)
if err != nil {
s.logger.Error(err.Error())
}
}