...
1
2
3
4
5
6
7
8
9
10 package mongocrypt
11
12
13 import "C"
14 import (
15 "go.mongodb.org/mongo-driver/x/bsonx/bsoncore"
16 )
17
18
19 type Context struct {
20 wrapped *C.mongocrypt_ctx_t
21 }
22
23
24 func newContext(wrapped *C.mongocrypt_ctx_t) *Context {
25 return &Context{
26 wrapped: wrapped,
27 }
28 }
29
30
31 func (c *Context) State() State {
32 return State(int(C.mongocrypt_ctx_state(c.wrapped)))
33 }
34
35
36 func (c *Context) NextOperation() (bsoncore.Document, error) {
37 opDocBinary := newBinary()
38 defer opDocBinary.close()
39
40 if ok := C.mongocrypt_ctx_mongo_op(c.wrapped, opDocBinary.wrapped); !ok {
41 return nil, c.createErrorFromStatus()
42 }
43 return opDocBinary.toBytes(), nil
44 }
45
46
47 func (c *Context) AddOperationResult(result bsoncore.Document) error {
48 resultBinary := newBinaryFromBytes(result)
49 defer resultBinary.close()
50
51 if ok := C.mongocrypt_ctx_mongo_feed(c.wrapped, resultBinary.wrapped); !ok {
52 return c.createErrorFromStatus()
53 }
54 return nil
55 }
56
57
58 func (c *Context) CompleteOperation() error {
59 if ok := C.mongocrypt_ctx_mongo_done(c.wrapped); !ok {
60 return c.createErrorFromStatus()
61 }
62 return nil
63 }
64
65
66 func (c *Context) NextKmsContext() *KmsContext {
67 ctx := C.mongocrypt_ctx_next_kms_ctx(c.wrapped)
68 if ctx == nil {
69 return nil
70 }
71 return newKmsContext(ctx)
72 }
73
74
75 func (c *Context) FinishKmsContexts() error {
76 if ok := C.mongocrypt_ctx_kms_done(c.wrapped); !ok {
77 return c.createErrorFromStatus()
78 }
79 return nil
80 }
81
82
83 func (c *Context) Finish() (bsoncore.Document, error) {
84 docBinary := newBinary()
85 defer docBinary.close()
86
87 if ok := C.mongocrypt_ctx_finalize(c.wrapped, docBinary.wrapped); !ok {
88 return nil, c.createErrorFromStatus()
89 }
90 return docBinary.toBytes(), nil
91 }
92
93
94 func (c *Context) Close() {
95 C.mongocrypt_ctx_destroy(c.wrapped)
96 }
97
98
99 func (c *Context) createErrorFromStatus() error {
100 status := C.mongocrypt_status_new()
101 defer C.mongocrypt_status_destroy(status)
102 C.mongocrypt_ctx_status(c.wrapped, status)
103 return errorFromStatus(status)
104 }
105
106
107 func (c *Context) ProvideKmsProviders(kmsProviders bsoncore.Document) error {
108 kmsProvidersBinary := newBinaryFromBytes(kmsProviders)
109 defer kmsProvidersBinary.close()
110
111 if ok := C.mongocrypt_ctx_provide_kms_providers(c.wrapped, kmsProvidersBinary.wrapped); !ok {
112 return c.createErrorFromStatus()
113 }
114 return nil
115 }
116
View as plain text