/
converters.go
72 lines (59 loc) · 1.76 KB
/
converters.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
// Copyright 2023 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package converters
import (
"context"
"cloudeng.io/file/content"
"cloudeng.io/glean/crawlindex/config"
"cloudeng.io/glean/gleansdk"
)
const (
GleanDocumentType = content.Type("glean/document")
GleanUserType = content.Type("glean/user")
)
type ignoreError struct {
ctype content.Type
}
func (e ignoreError) Error() string {
return "ignore content type: " + string(e.ctype)
}
func (e ignoreError) Is(target error) bool {
_, ok := target.(ignoreError)
return ok
}
func IgnoreContentType(ctype content.Type) error {
return ignoreError{ctype: ctype}
}
func IsIgnoreContentType(err error) bool {
_, ok := err.(ignoreError)
return ok
}
type Document interface {
Type() content.Type
Convert(ctx context.Context, datasource string, cfg config.Conversion, ctype content.Type, data []byte) (gleansdk.DocumentDefinition, error)
}
func CreateDocumentRegistry(converters ...Document) (*content.Registry[Document], error) {
reg := content.NewRegistry[Document]()
for _, cnv := range converters {
from := cnv.Type()
if err := reg.RegisterConverters(from, GleanDocumentType, cnv); err != nil {
return nil, err
}
}
return reg, nil
}
type User interface {
Type() content.Type
Convert(ctx context.Context, datasource string, cfg config.Conversion, ctype content.Type, data []byte) (gleansdk.DatasourceUserDefinition, error)
}
func CreateUserRegistry(converters ...User) (*content.Registry[User], error) {
reg := content.NewRegistry[User]()
for _, cnv := range converters {
from := cnv.Type()
if err := reg.RegisterConverters(from, GleanUserType, cnv); err != nil {
return nil, err
}
}
return reg, nil
}