/
parser.go
137 lines (118 loc) · 4.34 KB
/
parser.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
package airbyte
import (
"encoding/json"
"fmt"
"github.com/jitsucom/jitsu/server/airbyte"
"github.com/jitsucom/jitsu/server/drivers/base"
"github.com/jitsucom/jitsu/server/schema"
)
const (
syncModeIncremental = "incremental"
syncModeFullRefresh = "full_refresh"
)
// dbDockerImages db sources doesn't support increment sync mode yet
var dbDockerImages = map[string]bool{
"source-postgres": true,
"source-mssql": true,
"source-oracle": true,
"source-mysql": true,
}
// reformatCatalog reformat raw Airbyte catalog (Airbyte discovers and consumes on read command different catalogs formats)
// returns reformatted catalog bytes, streams representation and err if occurred
func reformatCatalog(dockerImage string, rawCatalog *airbyte.CatalogRow) ([]byte, map[string]*base.StreamRepresentation, error) {
formattedCatalog := &airbyte.Catalog{}
streamsRepresentation := map[string]*base.StreamRepresentation{}
for _, stream := range rawCatalog.Streams {
syncMode := getSyncMode(dockerImage, stream)
var cursorField []string = nil
if stream.SourceDefinedCursor {
cursorField = stream.DefaultCursorField
} else if len(stream.SelectedCursorField) > 0 {
cursorField = stream.SelectedCursorField
}
//formatted catalog
formattedCatalog.Streams = append(formattedCatalog.Streams, &airbyte.WrappedStream{
SyncMode: syncMode,
//isn't used because Jitsu doesn't use airbyte destinations. Just should be a valid option.
DestinationSyncMode: "overwrite",
Stream: stream,
CursorField: cursorField,
})
//streams schema representation
streamSchema := schema.Fields{}
base.ParseProperties(base.AirbyteType, "", stream.JsonSchema.Properties, streamSchema)
var keyFields []string
for _, sourceDefinedPrimaryKeys := range stream.SourceDefinedPrimaryKey {
if len(sourceDefinedPrimaryKeys) > 0 {
keyFields = append(keyFields, sourceDefinedPrimaryKeys...)
}
}
streamsRepresentation[stream.Name] = &base.StreamRepresentation{
Namespace: stream.Namespace,
StreamName: stream.Name,
BatchHeader: &schema.BatchHeader{
TableName: stream.Name,
Fields: streamSchema,
},
KeyFields: keyFields,
Objects: []map[string]interface{}{},
//Set need clean only if full refresh => table will be truncated before data storing
NeedClean: syncMode == syncModeFullRefresh,
}
}
b, _ := json.MarshalIndent(formattedCatalog, "", " ")
return b, streamsRepresentation, nil
}
// parseFormattedCatalog parses formatted catalog from (UI/input)
func parseFormattedCatalog(catalogIface interface{}) (map[string]*base.StreamRepresentation, error) {
b, _ := json.Marshal(catalogIface)
catalog := &airbyte.Catalog{}
if err := json.Unmarshal(b, catalog); err != nil {
return nil, fmt.Errorf("can't unmarshal into airbyte.Catalog{}: %v", err)
}
streamsRepresentation := map[string]*base.StreamRepresentation{}
for _, stream := range catalog.Streams {
var keyFields []string
for _, sourceDefinedPrimaryKeys := range stream.Stream.SourceDefinedPrimaryKey {
if len(sourceDefinedPrimaryKeys) > 0 {
keyFields = append(keyFields, sourceDefinedPrimaryKeys...)
}
}
//streams schema representation
streamSchema := schema.Fields{}
base.ParseProperties(base.AirbyteType, "", stream.Stream.JsonSchema.Properties, streamSchema)
streamsRepresentation[stream.Stream.Name] = &base.StreamRepresentation{
Namespace: stream.Stream.Namespace,
StreamName: stream.Stream.Name,
BatchHeader: &schema.BatchHeader{
TableName: stream.Stream.Name,
Fields: streamSchema,
},
KeyFields: keyFields,
Objects: []map[string]interface{}{},
//Set need clean only if full refresh => table will be truncated before data storing
NeedClean: stream.SyncMode == syncModeFullRefresh,
}
}
return streamsRepresentation, nil
}
// getSyncMode returns incremental if supported
// otherwise returns first
// for DB source returns not incremental
func getSyncMode(dockerImage string, stream *airbyte.Stream) string {
if stream.SyncMode != "" {
return stream.SyncMode
}
if _, ok := dbDockerImages[dockerImage]; ok {
return syncModeFullRefresh
}
if len(stream.SupportedSyncModes) == 0 {
return syncModeIncremental
}
for _, supportedSyncMode := range stream.SupportedSyncModes {
if supportedSyncMode == syncModeIncremental {
return syncModeIncremental
}
}
return stream.SupportedSyncModes[0]
}