-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommittee_reader.go
More file actions
183 lines (146 loc) · 6.12 KB
/
Copy pathcommittee_reader.go
File metadata and controls
183 lines (146 loc) · 6.12 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
package service
import (
"context"
"errors"
"log/slog"
"github.com/linuxfoundation/lfx-v2-committee-service/internal/domain/model"
"github.com/linuxfoundation/lfx-v2-committee-service/internal/domain/port"
errs "github.com/linuxfoundation/lfx-v2-committee-service/pkg/errors"
"github.com/linuxfoundation/lfx-v2-committee-service/pkg/fields"
)
// CommitteeReader defines the interface for committee read operations
type CommitteeReader interface {
CommitteeDataReader
CommitteeMemberDataReader
}
// CommitteeDataReader defines the interface for committee-specific read operations
type CommitteeDataReader interface {
// GetBase retrieves committee base information by UID and returns the revision
GetBase(ctx context.Context, uid string) (*model.CommitteeBase, uint64, error)
// GetSettings retrieves committee settings by UID and returns the revision
GetSettings(ctx context.Context, uid string) (*model.CommitteeSettings, uint64, error)
// GetBaseAttributeValue retrieves an attribute value by UID and returns the revision
GetBaseAttributeValue(ctx context.Context, uid string, attributeName string) (any, error)
}
// CommitteeMemberDataReader defines the interface for committee member read operations
type CommitteeMemberDataReader interface {
// GetMember retrieves a committee member by committee UID and member UID
GetMember(ctx context.Context, committeeUID, memberUID string) (*model.CommitteeMember, uint64, error)
// GetMemberRevision retrieves the current KV revision for a committee member by UID
GetMemberRevision(ctx context.Context, memberUID string) (uint64, error)
// ListMembers retrieves all members for a given committee UID
ListMembers(ctx context.Context, committeeUID string) ([]*model.CommitteeMember, error)
}
// committeeReaderOrchestratorOption defines a function type for setting options
type committeeReaderOrchestratorOption func(*committeeReaderOrchestrator)
// WithCommitteeReader sets the committee reader
func WithCommitteeReader(reader port.CommitteeReader) committeeReaderOrchestratorOption {
return func(r *committeeReaderOrchestrator) {
r.committeeReader = reader
}
}
// committeeReaderOrchestrator orchestrates the committee reading process
type committeeReaderOrchestrator struct {
committeeReader port.CommitteeReader
}
// GetBase retrieves committee base information by UID
func (rc *committeeReaderOrchestrator) GetBase(ctx context.Context, uid string) (*model.CommitteeBase, uint64, error) {
slog.DebugContext(ctx, "executing get committee base use case",
"committee_uid", uid,
)
// Get committee base from storage
committeeBase, revision, err := rc.committeeReader.GetBase(ctx, uid)
if err != nil {
return nil, 0, err
}
slog.DebugContext(ctx, "committee base retrieved successfully",
"committee_uid", uid,
"revision", revision,
)
return committeeBase, revision, nil
}
// GetSettings retrieves committee settings by UID
func (rc *committeeReaderOrchestrator) GetSettings(ctx context.Context, uid string) (*model.CommitteeSettings, uint64, error) {
slog.DebugContext(ctx, "executing get committee settings use case",
"committee_uid", uid,
)
// Get committee settings from storage
committeeSettings, revision, err := rc.committeeReader.GetSettings(ctx, uid)
if err != nil {
return nil, 0, err
}
slog.DebugContext(ctx, "committee settings retrieved successfully",
"committee_uid", uid,
"revision", revision,
)
return committeeSettings, revision, nil
}
// GetAttributeValue retrieves an attribute value by UID and returns the revision
func (rc *committeeReaderOrchestrator) GetBaseAttributeValue(ctx context.Context, uid string, attributeName string) (any, error) {
committeeBase, _, err := rc.committeeReader.GetBase(ctx, uid)
if err != nil {
return nil, err
}
field, ok := fields.LookupByTag(committeeBase, "json", attributeName)
if !ok {
return nil, errors.New("attribute not found")
}
return field, nil
}
// GetMemberRevision retrieves the current KV revision for a committee member by UID
func (rc *committeeReaderOrchestrator) GetMemberRevision(ctx context.Context, memberUID string) (uint64, error) {
return rc.committeeReader.GetMemberRevision(ctx, memberUID)
}
// GetMember retrieves a committee member by committee UID and member UID
func (rc *committeeReaderOrchestrator) GetMember(ctx context.Context, committeeUID, memberUID string) (*model.CommitteeMember, uint64, error) {
slog.DebugContext(ctx, "executing get committee member use case",
"committee_uid", committeeUID,
"member_uid", memberUID,
)
// First, verify that the committee exists
_, _, err := rc.committeeReader.GetBase(ctx, committeeUID)
if err != nil {
return nil, 0, err
}
// Get committee member from storage
committeeMember, revision, err := rc.committeeReader.GetMember(ctx, memberUID)
if err != nil {
return nil, 0, err
}
// Verify that the member belongs to the requested committee
if committeeMember.CommitteeUID != committeeUID {
return nil, 0, errs.NewValidation("committee member does not belong to the requested committee")
}
slog.DebugContext(ctx, "committee member retrieved successfully",
"committee_uid", committeeUID,
"member_uid", memberUID,
"revision", revision,
)
return committeeMember, revision, nil
}
// ListMembers retrieves all members for a given committee UID
func (rc *committeeReaderOrchestrator) ListMembers(ctx context.Context, committeeUID string) ([]*model.CommitteeMember, error) {
slog.DebugContext(ctx, "executing list committee members use case",
"committee_uid", committeeUID,
)
// Get all committee members from storage
members, err := rc.committeeReader.ListMembers(ctx, committeeUID)
if err != nil {
return nil, err
}
slog.DebugContext(ctx, "committee members retrieved successfully",
"committee_uid", committeeUID,
"member_count", len(members),
)
return members, nil
}
// NewCommitteeReaderOrchestrator creates a new committee reader use case using the option pattern
func NewCommitteeReaderOrchestrator(opts ...committeeReaderOrchestratorOption) CommitteeReader {
rc := &committeeReaderOrchestrator{}
for _, opt := range opts {
opt(rc)
}
return rc
}