-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconverters.go
More file actions
193 lines (170 loc) · 6.1 KB
/
converters.go
File metadata and controls
193 lines (170 loc) · 6.1 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
184
185
186
187
188
189
190
191
192
193
// Copyright The Linux Foundation and each contributor to LFX.
// SPDX-License-Identifier: MIT
package service
import (
"context"
"log/slog"
querysvc "github.com/linuxfoundation/lfx-v2-query-service/gen/query_svc"
"github.com/linuxfoundation/lfx-v2-query-service/internal/domain/model"
"github.com/linuxfoundation/lfx-v2-query-service/pkg/constants"
"github.com/linuxfoundation/lfx-v2-query-service/pkg/global"
"github.com/linuxfoundation/lfx-v2-query-service/pkg/paging"
)
// payloadToCriteria converts the generated payload to domain search criteria
func (s *querySvcsrvc) payloadToCriteria(ctx context.Context, p *querysvc.QueryResourcesPayload) (model.SearchCriteria, error) {
criteria := model.SearchCriteria{
Name: p.Name,
Parent: p.Parent,
ResourceType: p.Type,
Tags: p.Tags,
TagsAll: p.TagsAll,
CelFilter: p.CelFilter,
SortBy: p.Sort,
PageToken: p.PageToken,
PageSize: constants.DefaultPageSize,
}
switch p.Sort {
case "name_asc":
criteria.SortBy = "sort_name"
criteria.SortOrder = "asc"
case "name_desc":
criteria.SortBy = "sort_name"
criteria.SortOrder = "desc"
case "updated_asc":
criteria.SortBy = "updated_at"
criteria.SortOrder = "asc"
case "updated_desc":
criteria.SortBy = "updated_at"
criteria.SortOrder = "desc"
}
if criteria.PageToken != nil {
pageToken, errPageToken := paging.DecodePageToken(ctx, *criteria.PageToken, global.PageTokenSecret(ctx))
if errPageToken != nil {
slog.ErrorContext(ctx, "failed to decode page token", "error", errPageToken)
return criteria, wrapError(ctx, errPageToken)
}
criteria.SearchAfter = &pageToken
slog.DebugContext(ctx, "decoded page token",
"page_token", *criteria.PageToken,
"decoded", pageToken,
)
}
return criteria, nil
}
// domainResultToResponse converts domain search result to generated response
func (s *querySvcsrvc) domainResultToResponse(result *model.SearchResult) *querysvc.QueryResourcesResult {
response := &querysvc.QueryResourcesResult{
Resources: make([]*querysvc.Resource, len(result.Resources)),
PageToken: result.PageToken,
CacheControl: result.CacheControl,
}
for i, domainResource := range result.Resources {
// Create local copies to avoid taking addresses of loop variables
resourceType := domainResource.Type
resourceID := domainResource.ID
response.Resources[i] = &querysvc.Resource{
Type: &resourceType,
ID: &resourceID,
Data: domainResource.Data,
}
}
return response
}
func (s *querySvcsrvc) payloadToCountPublicCriteria(payload *querysvc.QueryResourcesCountPayload) model.SearchCriteria {
// Parameters used for /<index>/_count search.
criteria := model.SearchCriteria{
GroupBySize: constants.DefaultBucketSize,
// Page size is not passed to this endpoint.
PageSize: -1,
// For _count, we only want public resources.
PublicOnly: true,
}
// Set the criteria from the payload
criteria.Tags = payload.Tags
criteria.TagsAll = payload.TagsAll
if payload.Name != nil {
criteria.Name = payload.Name
}
if payload.Type != nil {
criteria.ResourceType = payload.Type
}
if payload.Parent != nil {
criteria.ParentRef = payload.Parent
}
return criteria
}
func (s *querySvcsrvc) payloadToCountAggregationCriteria(payload *querysvc.QueryResourcesCountPayload) model.SearchCriteria {
// Parameters used for the "group by" aggregated /<index>/_search search.
criteria := model.SearchCriteria{
GroupBySize: constants.DefaultBucketSize,
// We only want the aggregation, not the actual results.
PageSize: 0,
// The aggregation results will only count private resources.
PrivateOnly: true,
// Set the attribute to aggregate on.
// Use .keyword subfield for aggregation on text fields
GroupBy: "access_check_query.keyword",
}
// Set the criteria from the payload
criteria.Tags = payload.Tags
criteria.TagsAll = payload.TagsAll
if payload.Name != nil {
criteria.Name = payload.Name
}
if payload.Type != nil {
criteria.ResourceType = payload.Type
}
if payload.Parent != nil {
criteria.ParentRef = payload.Parent
}
return criteria
}
func (s *querySvcsrvc) domainCountResultToResponse(result *model.CountResult) *querysvc.QueryResourcesCountResult {
return &querysvc.QueryResourcesCountResult{
Count: uint64(result.Count),
HasMore: result.HasMore,
CacheControl: result.CacheControl,
}
}
// payloadToOrganizationCriteria converts the generated payload to domain organization search criteria
func (s *querySvcsrvc) payloadToOrganizationCriteria(ctx context.Context, p *querysvc.QueryOrgsPayload) model.OrganizationSearchCriteria {
criteria := model.OrganizationSearchCriteria{
Name: p.Name,
Domain: p.Domain,
}
return criteria
}
// domainOrganizationToResponse converts domain organization result to generated response
func (s *querySvcsrvc) domainOrganizationToResponse(org *model.Organization) *querysvc.Organization {
return &querysvc.Organization{
Name: &org.Name,
Domain: &org.Domain,
Industry: &org.Industry,
Sector: &org.Sector,
Employees: &org.Employees,
}
}
// payloadToOrganizationSuggestionCriteria converts the generated payload to domain organization suggestion criteria
func (s *querySvcsrvc) payloadToOrganizationSuggestionCriteria(ctx context.Context, p *querysvc.SuggestOrgsPayload) model.OrganizationSuggestionCriteria {
criteria := model.OrganizationSuggestionCriteria{
Query: p.Query,
}
return criteria
}
// domainOrganizationSuggestionsToResponse converts domain organization suggestions result to generated response
func (s *querySvcsrvc) domainOrganizationSuggestionsToResponse(result *model.OrganizationSuggestionsResult) *querysvc.SuggestOrgsResult {
if result == nil || len(result.Suggestions) == 0 {
return &querysvc.SuggestOrgsResult{Suggestions: []*querysvc.OrganizationSuggestion{}}
}
suggestions := make([]*querysvc.OrganizationSuggestion, len(result.Suggestions))
for i, domainSuggestion := range result.Suggestions {
suggestions[i] = &querysvc.OrganizationSuggestion{
Name: domainSuggestion.Name,
Domain: domainSuggestion.Domain,
Logo: domainSuggestion.Logo,
}
}
return &querysvc.SuggestOrgsResult{
Suggestions: suggestions,
}
}