forked from linuxfoundation/lfx-v2-query-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverters.go
More file actions
128 lines (112 loc) · 4.21 KB
/
converters.go
File metadata and controls
128 lines (112 loc) · 4.21 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
// 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,
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
}
// 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,
}
}