-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathdocument_add.go
More file actions
110 lines (99 loc) · 3.09 KB
/
Copy pathdocument_add.go
File metadata and controls
110 lines (99 loc) · 3.09 KB
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
// Copyright 2026 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package cbindings
/*
#include <stdlib.h>
#include "defra_structs.h"
*/
import "C"
import (
"context"
"encoding/json"
"strings"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/client/options"
"github.com/sourcenetwork/defradb/internal/encryption"
acpIdentity "github.com/sourcenetwork/defradb/internal/identity"
)
//export AddDocument
func AddDocument(
nodePtr C.uintptr_t,
jsonData *C.char,
isEncrypted C.int,
encryptedFields *C.char,
opts C.CollectionOptions,
identityPtr C.uintptr_t,
) C.Result {
ctx := context.Background()
ctx, err := contextWithIdentity(ctx, identityPtr)
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
colOptions := parseCollectionOptionsToGetCollectionsOptions(opts)
ident := acpIdentity.FromContext(ctx)
if ident.HasValue() {
colOptions.SetIdentity(ident.Value())
}
store, err := getStoreFromPointer(nodePtr)
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
col, err := getCollection(store, ctx, colOptions)
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
var encryptFields []string
encryptFieldsStr := C.GoString(encryptedFields)
if encryptFieldsStr != "" {
for _, f := range strings.Split(encryptFieldsStr, ",") {
if trimmed := strings.TrimSpace(f); trimmed != "" {
encryptFields = append(encryptFields, trimmed)
}
}
}
ctx = encryption.SetContextConfigFromParams(ctx, isEncrypted != 0, encryptFields)
addOpt := options.WithIdentity(options.AddDocument(), acpIdentity.FromContext(ctx))
if opts.enableSigning != 0 {
addOpt.SetEnableSigning(opts.enableSigning > 0)
}
// Determine if JSON is array or object by looking for the first character being [
jsonString := strings.TrimSpace(C.GoString(jsonData))
if strings.HasPrefix(jsonString, "[") {
// Multiple documents
docs, err := client.NewDocsFromJSON(ctx, []byte(jsonString), col.Version())
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
err = col.AddManyDocuments(ctx, docs, addOpt)
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
docIDs, err := json.Marshal(client.DocumentIDs(docs))
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
return returnC(returnGoC(0, "", string(docIDs)))
} else {
// Single document
doc, err := client.NewDocFromJSON(ctx, []byte(jsonString), col.Version())
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
err = col.AddDocument(ctx, doc, addOpt)
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
docIDs, err := json.Marshal(client.DocumentIDs([]*client.Document{doc}))
if err != nil {
return returnC(returnGoC(1, err.Error(), ""))
}
return returnC(returnGoC(0, "", string(docIDs)))
}
}