generated from greenbone/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Implement create and list endpoint (#11)
Filtering and Sorting is not yet supported. Note: The packages `errs` and `restErrorHandler` as well as the function `PrepareResultSelector` was duplicated from the VI backend repo. Ultimately this code should be moved to [greenbone/opensight-golang-libraries](https://github.com/greenbone/opensight-golang-libraries).
- Loading branch information
Showing
24 changed files
with
1,076 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
with-expecter: true | ||
dir: "{{.InterfaceDir}}/mocks" | ||
outpkg: "mocks" | ||
mockname: "{{.InterfaceName}}" | ||
filename: "{{.InterfaceNameSnake}}.go" | ||
|
||
packages: | ||
github.com/greenbone/opensight-notification-service/pkg/port: | ||
interfaces: | ||
NotificationService: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
all: api-docs build test | ||
|
||
SWAG = github.com/swaggo/swag/cmd/[email protected] | ||
MOCKERY = github.com/vektra/mockery/[email protected] | ||
GOLANGCI-LINT = github.com/golangci/golangci-lint/cmd/golangci-lint@latest | ||
|
||
.PHONY: lint | ||
|
@@ -10,6 +11,11 @@ lint: | |
.PHONY: install-code-generation-tools | ||
install-code-generation-tools: | ||
go install $(SWAG) | ||
go install $(MOCKERY) | ||
|
||
.PHONY: generate-code | ||
generate-code: # create mocks | ||
go run $(MOCKERY) | ||
|
||
.PHONY: api-docs | ||
api-docs: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package errs | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// ErrItemNotFound is the error used when looking up an item by ID, e.g. | ||
// OID for VTs, fails because the item cannot be found. | ||
var ErrItemNotFound = errors.New("item not found") | ||
|
||
// embed this error to mark an error as retryable | ||
var ErrRetryable = errors.New("(retryable error)") | ||
|
||
// ErrConflict indicates a conflict. If there are certain fields conflicting which are meaningful to the client, | ||
// set the individual error message for a property via `Errors`, otherwise just set `Message`. | ||
type ErrConflict struct { | ||
Message string | ||
Errors map[string]string // maps property to specific error message | ||
} | ||
|
||
func (e *ErrConflict) Error() string { | ||
message := e.Message | ||
if len(e.Errors) > 0 { | ||
message += fmt.Sprintf(", specific errors: %v", e.Errors) | ||
} | ||
return message | ||
} | ||
|
||
type ErrValidation struct { | ||
Message string | ||
Errors map[string]string // maps property to specific error message | ||
} | ||
|
||
func (e *ErrValidation) Error() string { | ||
message := e.Message | ||
if len(e.Errors) > 0 { | ||
message += fmt.Sprintf(", specific errors: %v", e.Errors) | ||
} | ||
return message | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package helper | ||
|
||
func ToPtr[T any](val T) *T { | ||
return &val | ||
} | ||
|
||
// SafeDereference return the value ptr points to. If ptr is nil, it returns the default value if the type instead. | ||
func SafeDereference[T any](ptr *T) T { | ||
var zeroT T | ||
if ptr == nil { | ||
return zeroT | ||
} | ||
return *ptr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-FileCopyrightText: 2024 Greenbone AG <https://greenbone.net> | ||
// | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
|
||
package helper | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSafeDereference(t *testing.T) { | ||
type someStruct struct { | ||
foo, bar string | ||
} | ||
|
||
zeroValue := someStruct{} | ||
nonZeroValue := someStruct{foo: "aa", bar: "bb"} | ||
|
||
tests := []struct { | ||
name string | ||
input *someStruct | ||
want someStruct | ||
}{ | ||
{ | ||
name: "yields zero value with nil pointer", | ||
input: nil, | ||
want: zeroValue, | ||
}, | ||
{ | ||
name: "yields object pointed on for non-nil pointer", | ||
input: &nonZeroValue, | ||
want: nonZeroValue, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := SafeDereference(tt.input) | ||
assert.Equal(t, tt.want, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.