Skip to content

Commit

Permalink
[WIP]filter index parameters
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Valdron <[email protected]>
  • Loading branch information
michael-valdron committed Mar 19, 2024
1 parent 98e5043 commit e07cbc0
Show file tree
Hide file tree
Showing 8 changed files with 1,544 additions and 153 deletions.
60 changes: 14 additions & 46 deletions index/server/pkg/server/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,16 +481,6 @@ func ServeUI(c *gin.Context) {
func buildIndexAPIResponse(c *gin.Context, indexType string, wantV1Index bool, params IndexParams) {

iconType := ""
archs := []string{}
var result util.FilterResult

if params.Icon != nil {
iconType = *params.Icon
}

if params.Arch != nil {
archs = append(archs, *params.Arch...)
}

var bytes []byte
var responseIndexPath, responseBase64IndexPath string
Expand Down Expand Up @@ -571,39 +561,24 @@ func buildIndexAPIResponse(c *gin.Context, indexType string, wantV1Index bool, p
}
}

result = util.FilterDevfileSchemaVersion(index, minSchemaVersion, maxSchemaVersion)
result.Eval()
if !result.IsEval {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply schema version filter: %v", "unevaluated filter"),
})
return
} else if result.Error != nil {
index, err = util.FilterDevfileSchemaVersion(index, minSchemaVersion, maxSchemaVersion)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply schema version filter: %v", result.Error),
"status": fmt.Sprintf("failed to apply schema version filter: %v", err),
})
return
}
index = result.Index
}
}
// Filter the index if archs has been requested
if len(archs) > 0 {
result = util.FilterDevfileStrArrayField(index, util.ARRAY_PARAM_ARCHITECTURES, archs, wantV1Index)
result.Eval()
if !result.IsEval {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply schema version filter: %v", "unevaluated filter"),
})
return
} else if result.Error != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply archs filter: %v", result.Error),
})
return
}
index = result.Index

// Filter the fields of the index
index, err = filterFieldsByParams(index, wantV1Index, params)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to perform field filtering: %v", err),
})

Check warning on line 579 in index/server/pkg/server/endpoint.go

View check run for this annotation

Codecov / codecov/patch

index/server/pkg/server/endpoint.go#L577-L579

Added lines #L577 - L579 were not covered by tests
}

bytes, err = json.MarshalIndent(&index, "", " ")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
Expand Down Expand Up @@ -701,20 +676,13 @@ func fetchDevfile(c *gin.Context, name string, version string) ([]byte, indexSch
}
}

result := util.FilterDevfileSchemaVersion(index, minSchemaVersion, maxSchemaVersion)
result.Eval()
if !result.IsEval {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply schema version filter: %v", "unevaluated filter"),
})
return []byte{}, indexSchema.Schema{}
} else if result.Error != nil {
index, err = util.FilterDevfileSchemaVersion(index, minSchemaVersion, maxSchemaVersion)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"status": fmt.Sprintf("failed to apply schema version filter: %v", result.Error),
"status": fmt.Sprintf("failed to apply schema version filter: %v", err),
})
return []byte{}, indexSchema.Schema{}
}
index = result.Index
}

for _, devfileIndex := range index {
Expand Down
81 changes: 81 additions & 0 deletions index/server/pkg/server/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// Copyright Red Hat
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"fmt"

indexSchema "github.com/devfile/registry-support/index/generator/schema"
"github.com/devfile/registry-support/index/server/pkg/util"
)

const checkEvalRecursively = true

func checkChildrenEval(result *util.FilterResult) error {
if !result.IsChildrenEval(checkEvalRecursively) {
errMsg := "there was a problem evaluating and logically filters"

if result.Error != nil {
errMsg += ", see error for details: %v"
return fmt.Errorf(errMsg, result.Error)
}

Check warning on line 34 in index/server/pkg/server/filter.go

View check run for this annotation

Codecov / codecov/patch

index/server/pkg/server/filter.go#L32-L34

Added lines #L32 - L34 were not covered by tests

return fmt.Errorf(errMsg)
}

return nil
}

func filterFieldbyParam(index []indexSchema.Schema, wantV1Index bool, paramName string, paramValue any) util.FilterResult {
switch typedValue := paramValue.(type) {
case string:
return util.FilterDevfileStrField(index, paramName, typedValue, wantV1Index)
default:
return util.FilterDevfileStrField(index, paramName, fmt.Sprintf("%v", typedValue), wantV1Index)
}
}

func filterFieldsByParams(index []indexSchema.Schema, wantV1Index bool, params IndexParams) ([]indexSchema.Schema, error) {
paramsMap := util.StructToMap(params)
results := []*util.FilterResult{}
var andResult util.FilterResult

if len(paramsMap) == 0 {
return index, nil
}

for paramName, paramValue := range paramsMap {
var result util.FilterResult

if util.IsFieldParameter(paramName) {
result = filterFieldbyParam(index, wantV1Index, paramName, paramValue)

Check warning on line 64 in index/server/pkg/server/filter.go

View check run for this annotation

Codecov / codecov/patch

index/server/pkg/server/filter.go#L64

Added line #L64 was not covered by tests
} else if util.IsArrayParameter(paramName) {
typedValues := paramValue.([]string)
result = util.FilterDevfileStrArrayField(index, paramName, typedValues, wantV1Index)
}

results = append(results, &result)
}

andResult = util.AndFilter(results...)
andResult.Eval()

if err := checkChildrenEval(&andResult); err != nil {
return []indexSchema.Schema{}, err
}

Check warning on line 78 in index/server/pkg/server/filter.go

View check run for this annotation

Codecov / codecov/patch

index/server/pkg/server/filter.go#L77-L78

Added lines #L77 - L78 were not covered by tests

return andResult.Index, andResult.Error
}
Loading

0 comments on commit e07cbc0

Please sign in to comment.