Skip to content

Commit 9119bd1

Browse files
authored
change: make filter in resultSelector optional (#47)
There is certainly the case where we want to e.g. use paging without filtering the data. Then it is quite cumbersome that we have to define a dummy filter which does not filter anything, instead of just omitting the filter property. Like Paging there is a reasonable default: no filter provided -> no filtering.
1 parent 0315c7b commit 9119bd1

File tree

7 files changed

+44
-11
lines changed

7 files changed

+44
-11
lines changed

pkg/dbcrypt/README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,33 @@ import "github.com/greenbone/opensight-golang-libraries/pkg/dbcrypt"
7373

7474
## Index
7575

76+
- [func Decrypt\(encrypted string, key \[\]byte\) \(string, error\)](<#Decrypt>)
77+
- [func Encrypt\(plaintext string, key \[\]byte\) \(string, error\)](<#Encrypt>)
7678
- [type DBCrypt](<#DBCrypt>)
7779
- [func \(d \*DBCrypt\[T\]\) DecryptStruct\(data \*T\) error](<#DBCrypt[T].DecryptStruct>)
7880
- [func \(d \*DBCrypt\[T\]\) EncryptStruct\(data \*T\) error](<#DBCrypt[T].EncryptStruct>)
7981

8082

83+
<a name="Decrypt"></a>
84+
## func [Decrypt](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L105>)
85+
86+
```go
87+
func Decrypt(encrypted string, key []byte) (string, error)
88+
```
89+
90+
91+
92+
<a name="Encrypt"></a>
93+
## func [Encrypt](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L83>)
94+
95+
```go
96+
func Encrypt(plaintext string, key []byte) (string, error)
97+
```
98+
99+
100+
81101
<a name="DBCrypt"></a>
82-
## type [DBCrypt](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L19-L21>)
102+
## type [DBCrypt](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L24-L26>)
83103

84104

85105

@@ -90,7 +110,7 @@ type DBCrypt[T any] struct {
90110
```
91111

92112
<a name="DBCrypt[T].DecryptStruct"></a>
93-
### func \(\*DBCrypt\[T\]\) [DecryptStruct](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L55>)
113+
### func \(\*DBCrypt\[T\]\) [DecryptStruct](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L65>)
94114

95115
```go
96116
func (d *DBCrypt[T]) DecryptStruct(data *T) error
@@ -99,7 +119,7 @@ func (d *DBCrypt[T]) DecryptStruct(data *T) error
99119
DecryptStruct decrypts all fields of a struct that are tagged with \`encrypt:"true"\`
100120

101121
<a name="DBCrypt[T].EncryptStruct"></a>
102-
### func \(\*DBCrypt\[T\]\) [EncryptStruct](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L30>)
122+
### func \(\*DBCrypt\[T\]\) [EncryptStruct](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/dbcrypt/dbcrypt.go#L41>)
103123

104124
```go
105125
func (d *DBCrypt[T]) EncryptStruct(data *T) error

pkg/openSearch/openSearchClient/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ func NewOpenSearchErrorWithStack(message string) error
134134
func NewOpenSearchProjectClient(ctx context.Context, config config.OpensearchClientConfig) (*opensearch.Client, error)
135135
```
136136

137-
NewOpenSearchProjectClient creates a new official OpenSearch client \(package github.com/opensearch\-project/opensearch\-go\) for usage in NewClient. It returns an error if the client couldn't be created or the connection couldn't be established.
137+
NewOpenSearchProjectClient creates a new official OpenSearch client \(package github.com/opensearch\-project/opensearch\-go\) for usage NewClient. It returns an error if the client couldn't be created or the connection couldn't be established.
138138

139139
ctx is the context to use for the connection. config is the configuration for the client.
140140

pkg/query/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Metadata represents the metadata used in a query.
5555

5656
```go
5757
type Metadata struct {
58-
Filter *filter.Request `json:"filter" binding:"required"`
58+
Filter *filter.Request `json:"filter,omitempty"`
5959
Paging *paging.Response `json:"paging,omitempty"`
6060
Sorting *sorting.Request `json:"sorting,omitempty"`
6161
}
@@ -101,7 +101,7 @@ ResultSelector is a type that represents the selection criteria for querying dat
101101

102102
```go
103103
type ResultSelector struct {
104-
Filter *filter.Request `json:"filter" binding:"required"`
104+
Filter *filter.Request `json:"filter" binding:"omitempty"`
105105
Sorting *sorting.Request `json:"sorting" binding:"omitempty"`
106106
Paging *paging.Request `json:"paging" binding:"omitempty"`
107107
}

pkg/query/filter/validation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515

1616
// ValidateFilter validates the filter in the request
1717
func ValidateFilter(request *Request, requestOptions []RequestOption) error {
18+
if request == nil {
19+
return nil
20+
}
21+
1822
for i, field := range request.Fields {
1923
fieldNameIsValid := false
2024
if field.Name == "tag" {

pkg/query/filter/validation_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ func TestRequestOptionValidation(t *testing.T) {
5454
}
5555
}
5656

57+
t.Run("shouldAllowUnsetFilter", func(t *testing.T) {
58+
setup(t)
59+
60+
err := ValidateFilter(nil, requestOptions)
61+
require.NoError(t, err)
62+
})
63+
5764
t.Run("shouldAllowEmptyFilter", func(t *testing.T) {
5865
setup(t)
5966

pkg/query/responseWithMetadata.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ type ResponseWithMetadata[T any] struct {
2525

2626
// Metadata represents the metadata used in a query.
2727
type Metadata struct {
28-
Filter *filter.Request `json:"filter" binding:"required"`
28+
Filter *filter.Request `json:"filter,omitempty"`
2929
Paging *paging.Response `json:"paging,omitempty"`
3030
Sorting *sorting.Request `json:"sorting,omitempty"`
3131
}
3232

3333
func NewMetadata(resultSelector ResultSelector, totalRowCount uint64) Metadata {
34-
for i, field := range resultSelector.Filter.Fields {
35-
if field.Keys == nil {
36-
resultSelector.Filter.Fields[i].Keys = []string{}
34+
if resultSelector.Filter != nil {
35+
for i, field := range resultSelector.Filter.Fields {
36+
if field.Keys == nil {
37+
resultSelector.Filter.Fields[i].Keys = []string{}
38+
}
3739
}
3840
}
3941
return Metadata{

pkg/query/resultSelector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// Sorting is a pointer to a sorting.Request struct that specifies the sorting order for the query.
1616
// Paging is a pointer to a paging.Request struct that specifies the paging configuration for the query.
1717
type ResultSelector struct {
18-
Filter *filter.Request `json:"filter" binding:"required"`
18+
Filter *filter.Request `json:"filter" binding:"omitempty"`
1919
Sorting *sorting.Request `json:"sorting" binding:"omitempty"`
2020
Paging *paging.Request `json:"paging" binding:"omitempty"`
2121
}

0 commit comments

Comments
 (0)