-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoding.go
48 lines (41 loc) · 1.07 KB
/
encoding.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
package dagobert
import (
"fmt"
"github.com/jina-ai/client-go/docarray"
"github.com/jina-ai/client-go/jina"
"github.com/samber/mo"
)
func (c *Client) Encode(docs []*Document) ([]*docarray.DocumentProto, error) {
requests := make(chan *jina.DataRequestProto)
defer close(requests)
go func() {
req := &jina.DataRequestProto{
Data: &jina.DataRequestProto_DataContentProto{
Documents: &jina.DataRequestProto_DataContentProto_Docs{
Docs: &docarray.DocumentArrayProto{
Docs: documentsToArray(docs),
},
},
},
}
requests <- req
}()
return mo.
NewFuture(func(resolve func([]*docarray.DocumentProto), reject func(error)) {
onDone := func(result *jina.DataRequestProto) {
docs := result.GetData().GetDocs().GetDocs()
resolve(docs)
}
onError := func(result *jina.DataRequestProto) {
err := NewDagobertError(result.Header.GetStatus())
reject(fmt.Errorf("💥 failed to encode: %w", err))
}
err := c.client.POST(requests, onDone, onError, nil)
if err != nil {
reject(err)
}
}).
Either().
Swap().
Unpack()
}