-
Notifications
You must be signed in to change notification settings - Fork 0
/
zanzigo_service_handler.go
148 lines (126 loc) · 4.54 KB
/
zanzigo_service_handler.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package server
import (
"context"
"errors"
"fmt"
"log/slog"
"github.com/gofrs/uuid/v5"
"github.com/trevex/zanzigo"
v1 "github.com/trevex/zanzigo/api/zanzigo/v1"
v1connect "github.com/trevex/zanzigo/api/zanzigo/v1/zanzigov1connect"
"connectrpc.com/connect"
)
type zanzigoServiceHandler struct {
log *slog.Logger
model *zanzigo.Model
storage zanzigo.Storage
resolver *zanzigo.Resolver
}
func NewZanzigoServiceHandler(log *slog.Logger, model *zanzigo.Model, storage zanzigo.Storage, resolver *zanzigo.Resolver) v1connect.ZanzigoServiceHandler {
return &zanzigoServiceHandler{log, model, storage, resolver}
}
func (h *zanzigoServiceHandler) Write(ctx context.Context, req *connect.Request[v1.WriteRequest]) (*connect.Response[v1.WriteResponse], error) {
tuple, err := h.isTupleValid(req.Msg.Tuple)
if err != nil {
return nil, err
}
err = h.storage.Write(ctx, tuple)
if err != nil {
// TODO: what if already exists? let's properly catch that error!
h.log.Error("failed to write tuple", slog.Any("tuple", tuple), slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed writing tuple"))
}
return connect.NewResponse(&v1.WriteResponse{}), nil
}
func (h *zanzigoServiceHandler) Read(ctx context.Context, req *connect.Request[v1.ReadRequest]) (*connect.Response[v1.ReadResponse], error) {
tuple, err := h.isTupleValid(req.Msg.Tuple)
if err != nil {
return nil, err
}
uuid, err := h.storage.Read(ctx, tuple)
if errors.Is(err, zanzigo.ErrNotFound) {
return nil, connect.NewError(connect.CodeDataLoss, fmt.Errorf("tuple not found"))
} else if err != nil {
h.log.Error("failed to read tuple", slog.Any("tuple", tuple), slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed reading tuple"))
}
return connect.NewResponse(&v1.ReadResponse{
Uuid: uuid.String(),
}), nil
}
func (h *zanzigoServiceHandler) Check(ctx context.Context, req *connect.Request[v1.CheckRequest]) (*connect.Response[v1.CheckResponse], error) {
tuple, err := h.isTupleValid(req.Msg.Tuple)
if err != nil {
return nil, err
}
result, err := h.resolver.Check(ctx, tuple)
if err != nil {
h.log.Error("failed to check tuple", slog.Any("tuple", tuple), slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to check tuple"))
}
return connect.NewResponse(&v1.CheckResponse{
Result: result,
}), nil
}
func (h *zanzigoServiceHandler) List(ctx context.Context, req *connect.Request[v1.ListRequest]) (*connect.Response[v1.ListResponse], error) {
filter := toZanzigoTuple(req.Msg.Filter) // TODO: filter tuple can be partially validated
pagination, err := toZanzigoPagination(req.Msg.Pagination)
if err != nil {
h.log.Debug("failed to parse cursor", slog.String("cursor", req.Msg.Pagination.Cursor))
return nil, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("malformed cursor"))
}
tuples, cursor, err := h.storage.List(ctx, filter, pagination)
if err != nil {
h.log.Error("failed to list tuples", slog.Any("error", err))
return nil, connect.NewError(connect.CodeInternal, fmt.Errorf("failed to list tuples"))
}
return connect.NewResponse(&v1.ListResponse{
Tuples: toProtobufTuples(tuples),
Cursor: cursor.String(),
}), nil
}
func (h *zanzigoServiceHandler) isTupleValid(t *v1.Tuple) (zanzigo.Tuple, error) {
if t == nil {
return zanzigo.EmptyTuple, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("missing tuple"))
}
tuple := toZanzigoTuple(t)
if !h.model.IsValid(tuple) {
return zanzigo.EmptyTuple, connect.NewError(connect.CodeInvalidArgument, fmt.Errorf("invalid tuple: %v", tuple))
}
return tuple, nil
}
func toZanzigoTuple(t *v1.Tuple) zanzigo.Tuple {
return zanzigo.Tuple{
ObjectType: t.ObjectType,
ObjectID: t.ObjectId,
ObjectRelation: t.ObjectRelation,
SubjectType: t.SubjectType,
SubjectID: t.SubjectId,
SubjectRelation: t.SubjectRelation,
}
}
func toProtobufTuple(t *zanzigo.Tuple) v1.Tuple {
return v1.Tuple{
ObjectType: t.ObjectType,
ObjectId: t.ObjectID,
ObjectRelation: t.ObjectRelation,
SubjectType: t.SubjectType,
SubjectId: t.SubjectID,
SubjectRelation: t.SubjectRelation,
}
}
func toProtobufTuples(ts []zanzigo.Tuple) []*v1.Tuple {
ps := make([]*v1.Tuple, 0, len(ts))
for _, t := range ts {
p := toProtobufTuple(&t)
ps = append(ps, &p)
}
return ps
}
func toZanzigoPagination(p *v1.Pagination) (zanzigo.Pagination, error) {
cursor, err := uuid.FromString(p.Cursor)
return zanzigo.Pagination{
Cursor: cursor,
Limit: int(p.Limit),
}, err
}