-
Notifications
You must be signed in to change notification settings - Fork 0
/
gutenberghub-write-api.go
86 lines (64 loc) · 1.79 KB
/
gutenberghub-write-api.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
package main
import (
"encoding/json"
"fmt"
"log"
"reflect"
"strings"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
func excludeFields(data interface{}, fields []string) ([]byte, error) {
// check if data is of type array
if reflect.TypeOf(data).Kind() != reflect.Slice {
return nil, fmt.Errorf("data should be of type slice, got %T", data)
}
// convert data to json
jsonData, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("Error while marshalling data: %v", err)
}
// Unmarshal the JSON data into a map
var dataMap []map[string]interface{}
err = json.Unmarshal(jsonData, &dataMap)
if err != nil {
return nil, fmt.Errorf("Error while unmarshalling data: %v", err)
}
// Iterate through the map and delete the specified fields
for _, item := range dataMap {
for _, field := range fields {
delete(item, field)
}
}
// Marshal the modified data back to JSON
jsonData, err = json.Marshal(dataMap)
if err != nil {
return nil, fmt.Errorf("Error while marshalling data: %v", err)
}
return jsonData, nil
}
func main() {
app := pocketbase.New()
// Adding a new query field parameter when listing view.
app.OnRecordsListRequest().Add(func(e *core.RecordsListEvent) error {
hasExclusion := e.HttpContext.Request().URL.Query().Has("excluded")
if !hasExclusion {
return nil
}
excludedFields := strings.Split(e.HttpContext.Request().URL.Query().Get("excluded"), ",")
items := e.Result.Items
excludedItems, err := excludeFields(items, excludedFields)
if err != nil {
fmt.Println(err)
}
var updatedItems struct {
Items json.RawMessage `json:"items"`
}
json.Unmarshal(excludedItems, &updatedItems)
e.Result.Items = updatedItems
return nil
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}